class Bixby::JsonResponse
Wraps a JSON Response
@attr [String] status Status of operaiton (“success” or “fail”) @attr [String] message Response message @attr [Hash] data Response data as key/value pairs @attr [FixNum] code Response code
Constants
- FAIL
- SUCCESS
Attributes
Public Class Methods
Create a JsonResponse
indicating “bundle not found”
@param [String] bundle Name of bundle
# File lib/bixby-common/api/json_response.rb, line 57 def self.bundle_not_found(bundle) # :nocov: new("fail", "bundle not found: #{bundle}", nil, 404) end
Create a JsonResponse
indicating “command not found”
@param [String] command Name of command
# File lib/bixby-common/api/json_response.rb, line 64 def self.command_not_found(command) # :nocov: new("fail", "command not found: #{command}", nil, 404) end
Create a JsonResponse
representing an invalid request
@param [String] msg Optional message (default: “invalid request”)
# File lib/bixby-common/api/json_response.rb, line 50 def self.invalid_request(msg = nil) # :nocov: new("fail", (msg || "invalid request"), nil, 400) end
Create a new JsonResponse
@param [String] status Status of operaiton (“success” or “fail”) @param [String] message Response message @param [Hash] data Response data as key/value pairs @param [FixNum] code Response code
# File lib/bixby-common/api/json_response.rb, line 25 def initialize(status = nil, message = nil, data = nil, code = nil) @status = status @message = message @data = data @code = code end
Public Instance Methods
Was operation unsuccessful?
@return [Boolean] True if @status != “success”
# File lib/bixby-common/api/json_response.rb, line 42 def fail? @status && @status == FAIL end
Was operation successful?
@return [Boolean] True if @status == “success”
# File lib/bixby-common/api/json_response.rb, line 35 def success? @status && @status == SUCCESS end
Convert object to String, useful for debugging
@return [String]
# File lib/bixby-common/api/json_response.rb, line 71 def to_s # :nocov: s = [] s << "JsonResponse:#{self.object_id}" s << " status: #{self.status}" s << " code: #{self.code}" s << " message: #{self.message}" s << " data: " + MultiJson.dump(self.data) s.join("\n") end
# File lib/bixby-common/api/json_response.rb, line 81 def to_wire MultiJson.dump(self) end