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

code[RW]
data[RW]
message[RW]
status[RW]

Public Class Methods

bundle_not_found(bundle) click to toggle source

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
command_not_found(command) click to toggle source

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
invalid_request(msg = nil) click to toggle source

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
new(status = nil, message = nil, data = nil, code = nil) click to toggle source

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

error?()
Alias for: fail?
fail?() click to toggle source

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
Also aliased as: error?
success?() click to toggle source

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
to_s() click to toggle source

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
to_wire() click to toggle source
# File lib/bixby-common/api/json_response.rb, line 81
def to_wire
  MultiJson.dump(self)
end