Class: Montage::Response
- Inherits:
-
Object
- Object
- Montage::Response
- Defined in:
- lib/montage/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#members ⇒ Object
readonly
Returns the value of attribute members.
-
#raw_body ⇒ Object
readonly
Returns the value of attribute raw_body.
-
#resource_name ⇒ Object
readonly
Returns the value of attribute resource_name.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #get_body(body) ⇒ Object
-
#initialize(status, body, resource_name = "resource") ⇒ Response
constructor
A new instance of Response.
- #method_missing(method_name, *args, &block) ⇒ Object
- #respond_to?(method_name, include_private = false) ⇒ Boolean
- #success? ⇒ Boolean
Constructor Details
#initialize(status, body, resource_name = "resource") ⇒ Response
Returns a new instance of Response.
8 9 10 11 12 13 14 |
# File 'lib/montage/response.rb', line 8 def initialize(status, body, resource_name = "resource") @status = status @resource_name = resource_name @raw_body = body.clone.freeze @body = get_body(body) @members = parse_members end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
32 33 34 35 |
# File 'lib/montage/response.rb', line 32 def method_missing(method_name, *args, &block) return super unless resource_name.to_sym == method_name || "#{resource_name}s".to_sym == method_name || method_name == "errors".to_sym members end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
6 7 8 |
# File 'lib/montage/response.rb', line 6 def body @body end |
#members ⇒ Object (readonly)
Returns the value of attribute members.
6 7 8 |
# File 'lib/montage/response.rb', line 6 def members @members end |
#raw_body ⇒ Object (readonly)
Returns the value of attribute raw_body.
6 7 8 |
# File 'lib/montage/response.rb', line 6 def raw_body @raw_body end |
#resource_name ⇒ Object (readonly)
Returns the value of attribute resource_name.
6 7 8 |
# File 'lib/montage/response.rb', line 6 def resource_name @resource_name end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
6 7 8 |
# File 'lib/montage/response.rb', line 6 def status @status end |
Instance Method Details
#get_body(body) ⇒ Object
16 17 18 |
# File 'lib/montage/response.rb', line 16 def get_body(body) resource_name == "error" ? body["errors"] : body["data"] || body end |
#respond_to?(method_name, include_private = false) ⇒ Boolean
28 29 30 |
# File 'lib/montage/response.rb', line 28 def respond_to?(method_name, include_private = false) resource_name.to_sym == method_name || "#{resource_name}s".to_sym == method_name || method_name == "errors".to_sym || super end |
#success? ⇒ Boolean
20 21 22 23 24 25 26 |
# File 'lib/montage/response.rb', line 20 def success? if raw_body["errors"] return false else (200..299).include?(status) end end |