Class: VerticalResponse::API::Response
- Inherits:
-
Object
- Object
- VerticalResponse::API::Response
- Defined in:
- lib/response.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#handle_collection ⇒ Object
Iterate through the collection of items and yield a new Response object for each one of them.
-
#handle_error ⇒ Object
Handles the case of an error response.
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
-
#success? ⇒ Boolean
Determines if the response is a successful one or not.
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/response.rb', line 11 def initialize(response) @url = response['url'] @items = response['items'] @attributes = response['attributes'] @links = response['links'] @success = response['success'] @error = response['error'] @raw_response = response handle_error unless success? end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
9 10 11 |
# File 'lib/response.rb', line 9 def attributes @attributes end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
9 10 11 |
# File 'lib/response.rb', line 9 def error @error end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
9 10 11 |
# File 'lib/response.rb', line 9 def items @items end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
9 10 11 |
# File 'lib/response.rb', line 9 def links @links end |
#raw_response ⇒ Object (readonly)
Returns the value of attribute raw_response.
9 10 11 |
# File 'lib/response.rb', line 9 def raw_response @raw_response end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
9 10 11 |
# File 'lib/response.rb', line 9 def success @success end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
9 10 11 |
# File 'lib/response.rb', line 9 def url @url end |
Instance Method Details
#handle_collection ⇒ Object
Iterate through the collection of items and yield a new Response object for each one of them
25 26 27 28 29 |
# File 'lib/response.rb', line 25 def handle_collection items.map do |item| yield(Response.new(item)) end end |
#handle_error ⇒ Object
Handles the case of an error response
37 38 39 40 41 42 43 44 45 |
# File 'lib/response.rb', line 37 def handle_error api_error = Error.new(error['message'] || error.inspect) # Keep track of the original error code and response object # for clients to have access to them if they rescue the exception api_error.code = error['code'] api_error.api_response = self raise api_error end |
#success? ⇒ Boolean
Determines if the response is a successful one or not
32 33 34 |
# File 'lib/response.rb', line 32 def success? !error end |