lib/bixby-common/command_response.rb in bixby-common-0.4.11 vs lib/bixby-common/command_response.rb in bixby-common-0.4.12
- old
+ new
@@ -4,29 +4,36 @@
include Jsonify
attr_accessor :status, :stdout, :stderr
- # Create a new CommandResponse from the given from_json_response
+ SUCCESS = 0
+ UNKNOWN_FAILURE = 255
+
+ # Create a new CommandResponse from the given JsonResponse
#
# @param [JsonResponse] res
#
# @return [CommandResponse]
def self.from_json_response(res)
cr = CommandResponse.new(res.data)
- if !(res.message.nil? || res.message.empty?) then
- cr.status ||= 255
- cr.stderr ||= res.message
+ 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
# Create a JsonResponse from this CommandResponse
#
# @return [JsonResponse]
def to_json_response
- return JsonResponse.new((status == 0 ? "success" : "fail"), nil, self.to_hash)
+ return JsonResponse.new((success?() ? "success" : "fail"), nil, self.to_hash)
end
def initialize(params = nil)
if params.kind_of? Hash then
params.each{ |k,v| self.send("#{k}=", v) if self.respond_to? "#{k}=" }
@@ -37,10 +44,10 @@
@stderr = params.stderr
end
end
def success?
- @status.to_i == 0
+ @status && @status.to_i == SUCCESS
end
def fail?
not success?
end