Class: Bixby::JsonResponse
- Inherits:
-
Object
- Object
- Bixby::JsonResponse
- 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
-
#code ⇒ FixNum
Response code.
-
#data ⇒ Hash
Response data as key/value pairs.
-
#message ⇒ String
Response message.
-
#status ⇒ String
Status of operaiton (“success” or “fail”).
Class Method Summary collapse
-
.bundle_not_found(bundle) ⇒ Object
Create a JsonResponse indicating “bundle not found”.
-
.command_not_found(command) ⇒ Object
Create a JsonResponse indicating “command not found”.
-
.invalid_request(msg = nil) ⇒ Object
Create a JsonResponse representing an invalid request.
Instance Method Summary collapse
-
#fail? ⇒ Boolean
(also: #error?)
Was operation unsuccessful?.
-
#initialize(status = nil, message = nil, data = nil, code = nil) ⇒ JsonResponse
constructor
Create a new JsonResponse.
-
#success? ⇒ Boolean
Was operation successful?.
-
#to_s ⇒ String
Convert object to String, useful for debugging.
-
#to_wire ⇒ Object
:nocov:.
Methods included from Jsonify
Methods included from Hashify
Constructor Details
#initialize(status = nil, message = nil, data = nil, code = nil) ⇒ JsonResponse
Create a new JsonResponse
25 26 27 28 29 30 |
# File 'lib/bixby-common/api/json_response.rb', line 25 def initialize(status = nil, = nil, data = nil, code = nil) @status = status @message = @data = data @code = code end |
Instance Attribute Details
#code ⇒ FixNum
Response code
10 11 12 |
# File 'lib/bixby-common/api/json_response.rb', line 10 def code @code end |
#data ⇒ Hash
Response data as key/value pairs
10 11 12 |
# File 'lib/bixby-common/api/json_response.rb', line 10 def data @data end |
#message ⇒ String
Response message
10 11 12 |
# File 'lib/bixby-common/api/json_response.rb', line 10 def @message end |
#status ⇒ String
Status of operaiton (“success” or “fail”)
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”
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”
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
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?
42 43 44 |
# File 'lib/bixby-common/api/json_response.rb', line 42 def fail? @status && @status == FAIL end |
#success? ⇒ Boolean
Was operation successful?
35 36 37 |
# File 'lib/bixby-common/api/json_response.rb', line 35 def success? @status && @status == SUCCESS end |
#to_s ⇒ String
Convert object to String, useful for debugging
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.}" s << " data: " + MultiJson.dump(self.data) s.join("\n") end |
#to_wire ⇒ Object
:nocov:
81 82 83 |
# File 'lib/bixby-common/api/json_response.rb', line 81 def to_wire MultiJson.dump(self) end |