Class: Bixby::JsonResponse

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

Overview

Wraps a JSON Response

Constant Summary collapse

SUCCESS =
"success"
FAIL =
"fail"

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(status = nil, message = nil, data = nil, code = nil) ⇒ JsonResponse

Create a new JsonResponse

Parameters:

  • status (String) (defaults to: nil)

    Status of operaiton (“success” or “fail”)

  • message (String) (defaults to: nil)

    Response message

  • data (Hash) (defaults to: nil)

    Response data as key/value pairs

  • code (FixNum) (defaults to: nil)

    Response code



25
26
27
28
29
30
# 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

Instance Attribute Details

#codeFixNum

Response code

Returns:

  • (FixNum)

    the current value of code



10
11
12
# File 'lib/bixby-common/api/json_response.rb', line 10

def code
  @code
end

#dataHash

Response data as key/value pairs

Returns:

  • (Hash)

    the current value of data



10
11
12
# File 'lib/bixby-common/api/json_response.rb', line 10

def data
  @data
end

#messageString

Response message

Returns:

  • (String)

    the current value of message



10
11
12
# File 'lib/bixby-common/api/json_response.rb', line 10

def message
  @message
end

#statusString

Status of operaiton (“success” or “fail”)

Returns:

  • (String)

    the current value of status



10
11
12
# File 'lib/bixby-common/api/json_response.rb', line 10

def status
  @status
end

Class Method Details

.bundle_not_found(bundle) ⇒ Object

Create a JsonResponse indicating “bundle not found”

Parameters:

  • bundle (String)

    Name of bundle



57
58
59
# 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) ⇒ Object

Create a JsonResponse indicating “command not found”

Parameters:

  • command (String)

    Name of command



64
65
66
# 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) ⇒ Object

Create a JsonResponse representing an invalid request

Parameters:

  • msg (String) (defaults to: nil)

    Optional message (default: “invalid request”)



50
51
52
# 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

Instance Method Details

#fail?Boolean Also known as: error?

Was operation unsuccessful?

Returns:

  • (Boolean)

    True if @status != “success”



42
43
44
# File 'lib/bixby-common/api/json_response.rb', line 42

def fail?
  @status && @status == FAIL
end

#success?Boolean

Was operation successful?

Returns:

  • (Boolean)

    True if @status == “success”



35
36
37
# File 'lib/bixby-common/api/json_response.rb', line 35

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

#to_sString

Convert object to String, useful for debugging

Returns:

  • (String)


71
72
73
74
75
76
77
78
79
# 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_wireObject

:nocov:



81
82
83
# File 'lib/bixby-common/api/json_response.rb', line 81

def to_wire
  MultiJson.dump(self)
end