Class: Bixby::CommandResponse

Inherits:
Object
  • Object
show all
Includes:
Jsonify
Defined in:
lib/bixby-common/command_response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Jsonify

included, #to_json

Methods included from Hashify

#to_hash

Constructor Details

#initialize(params = nil) ⇒ CommandResponse

Returns a new instance of CommandResponse.



30
31
32
33
34
35
36
37
38
39
# File 'lib/bixby-common/command_response.rb', line 30

def initialize(params = nil)
  if params.kind_of? Hash then
    params.each{ |k,v| self.send("#{k}=", v) if self.respond_to? "#{k}=" }

  elsif params.class.to_s == "Mixlib::ShellOut" then
    @status = params.exitstatus
    @stdout = params.stdout
    @stderr = params.stderr
  end
end

Instance Attribute Details

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/bixby-common/command_response.rb', line 7

def status
  @status
end

#stderrObject

Returns the value of attribute stderr.



7
8
9
# File 'lib/bixby-common/command_response.rb', line 7

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



7
8
9
# File 'lib/bixby-common/command_response.rb', line 7

def stdout
  @stdout
end

Class Method Details

.from_json_response(res) ⇒ CommandResponse

Create a new CommandResponse from the given from_json_response

Parameters:

Returns:



14
15
16
17
18
19
20
21
# File 'lib/bixby-common/command_response.rb', line 14

def self.from_json_response(res)
  cr = CommandResponse.new(res.data)
  if res.message then
    cr.status ||= 255
    cr.stderr ||= res.message
  end
  return cr
end

Instance Method Details

#decodeObject

:nocov:



59
60
61
# File 'lib/bixby-common/command_response.rb', line 59

def decode # :nocov:
  MultiJson.load(@stdout)
end

#decode_stderrObject

:nocov:



63
64
65
# File 'lib/bixby-common/command_response.rb', line 63

def decode_stderr # :nocov:
  MultiJson.load(@stderr)
end

#fail?Boolean Also known as: error?

Returns:

  • (Boolean)


45
46
47
# File 'lib/bixby-common/command_response.rb', line 45

def fail?
  not success?
end

#raise!Object



50
51
52
53
54
55
56
57
# File 'lib/bixby-common/command_response.rb', line 50

def raise!
  if fail? then
    msg = stdout || ""
    msg += "\n" if !(stdout.nil? or stdout.empty?)
    msg += stderr || ""
    raise CommandException.new(msg, msg)
  end
end

#success?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/bixby-common/command_response.rb', line 41

def success?
  @status.to_i == 0
end

#to_json_responseJsonResponse

Create a JsonResponse from this CommandResponse

Returns:



26
27
28
# File 'lib/bixby-common/command_response.rb', line 26

def to_json_response
  return JsonResponse.new((status == 0 ? "success" : "fail"), nil, self.to_hash)
end

#to_sString

Convert object to String, useful for debugging

Returns:

  • (String)


70
71
72
73
74
75
76
77
# File 'lib/bixby-common/command_response.rb', line 70

def to_s # :nocov:
  s = []
  s << "CommandResponse:#{self.object_id}"
  s << "  status:   #{self.status}"
  s << "  stdout:   " + Debug.pretty_str(stdout)
  s << "  stderr:   " + Debug.pretty_str(stderr)
  s.join("\n")
end