Class: Bixby::CommandResponse

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

Constant Summary collapse

SUCCESS =
0
UNKNOWN_FAILURE =
255

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.



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

#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 JsonResponse

Parameters:

Returns:



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.message.nil? || res.message.empty?) then
      cr.status ||= UNKNOWN_FAILURE
      cr.stderr ||= res.message
    else
      cr.status ||= UNKNOWN_FAILURE
    end
  end
  return cr
end

Instance Method Details

#decodeObject

:nocov:



66
67
68
# File 'lib/bixby-common/command_response.rb', line 66

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

#decode_stderrObject

: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?

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


48
49
50
# File 'lib/bixby-common/command_response.rb', line 48

def success?
  @status && @status.to_i == SUCCESS
end

#to_json_responseJsonResponse

Create a JsonResponse from this CommandResponse

Returns:



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_sString

Convert object to String, useful for debugging

Returns:

  • (String)


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