Class: Bixby::CommandResponse
- Inherits:
-
Object
- Object
- Bixby::CommandResponse
- Includes:
- Jsonify
- Defined in:
- lib/bixby-common/command_response.rb
Constant Summary collapse
- SUCCESS =
0
- UNKNOWN_FAILURE =
255
Instance Attribute Summary collapse
-
#status ⇒ Object
Returns the value of attribute status.
-
#stderr ⇒ Object
Returns the value of attribute stderr.
-
#stdout ⇒ Object
Returns the value of attribute stdout.
Class Method Summary collapse
-
.from_json_response(res) ⇒ CommandResponse
Create a new CommandResponse from the given JsonResponse.
Instance Method Summary collapse
-
#decode ⇒ Object
:nocov:.
-
#decode_stderr ⇒ Object
:nocov:.
- #fail? ⇒ Boolean (also: #error?)
-
#initialize(params = nil) ⇒ CommandResponse
constructor
A new instance of CommandResponse.
- #raise! ⇒ Object
- #success? ⇒ Boolean
-
#to_json_response ⇒ JsonResponse
Create a JsonResponse from this CommandResponse.
-
#to_s ⇒ String
Convert object to String, useful for debugging.
Methods included from Jsonify
Methods included from Hashify
Constructor Details
#initialize(params = nil) ⇒ CommandResponse
Returns a new instance of CommandResponse.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bixby-common/command_response.rb', line 37 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
#status ⇒ Object
Returns the value of attribute status.
7 8 9 |
# File 'lib/bixby-common/command_response.rb', line 7 def status @status end |
#stderr ⇒ Object
Returns the value of attribute stderr.
7 8 9 |
# File 'lib/bixby-common/command_response.rb', line 7 def stderr @stderr end |
#stdout ⇒ Object
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 JsonResponse
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/bixby-common/command_response.rb', line 17 def self.from_json_response(res) cr = CommandResponse.new(res.data) if res.fail? then if !(res..nil? || res..empty?) then cr.status ||= UNKNOWN_FAILURE cr.stderr ||= res. else cr.status ||= UNKNOWN_FAILURE end end return cr end |
Instance Method Details
#decode ⇒ Object
:nocov:
66 67 68 |
# File 'lib/bixby-common/command_response.rb', line 66 def decode # :nocov: MultiJson.load(@stdout) end |
#decode_stderr ⇒ Object
:nocov:
70 71 72 |
# File 'lib/bixby-common/command_response.rb', line 70 def decode_stderr # :nocov: MultiJson.load(@stderr) end |
#fail? ⇒ Boolean Also known as: error?
52 53 54 |
# File 'lib/bixby-common/command_response.rb', line 52 def fail? not success? end |
#raise! ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/bixby-common/command_response.rb', line 57 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
48 49 50 |
# File 'lib/bixby-common/command_response.rb', line 48 def success? @status && @status.to_i == SUCCESS end |
#to_json_response ⇒ JsonResponse
Create a JsonResponse from this CommandResponse
33 34 35 |
# File 'lib/bixby-common/command_response.rb', line 33 def to_json_response return JsonResponse.new((success?() ? "success" : "fail"), nil, self.to_hash) end |
#to_s ⇒ String
Convert object to String, useful for debugging
77 78 79 80 81 82 83 84 |
# File 'lib/bixby-common/command_response.rb', line 77 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 |