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
Was operation unsuccessful?.
-
#initialize(status = nil, message = nil, data = nil, code = nil) ⇒ JsonResponse
constructor
Create a new JsonResponse.
-
#success? ⇒ Boolean
Was operation successful?.
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”
56 57 58 |
# File 'lib/bixby_common/api/json_response.rb', line 56 def self.bundle_not_found(bundle) new("fail", "bundle not found: #{bundle}", nil, 404) end |
.command_not_found(command) ⇒ Object
Create a JsonResponse indicating “command not found”
63 64 65 |
# File 'lib/bixby_common/api/json_response.rb', line 63 def self.command_not_found(command) new("fail", "command not found: #{command}", nil, 404) end |
.invalid_request(msg = nil) ⇒ Object
Create a JsonResponse representing an invalid request
49 50 51 |
# File 'lib/bixby_common/api/json_response.rb', line 49 def self.invalid_request(msg = nil) new("fail", (msg || "invalid request"), nil, 400) end |
Instance Method Details
#fail? ⇒ Boolean
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 |