Class: VerticalResponse::API::Response
- Inherits:
-
Object
- Object
- VerticalResponse::API::Response
- Defined in:
- lib/verticalresponse/api/response.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#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, access_token = nil) ⇒ Response
constructor
A new instance of Response.
-
#success? ⇒ Boolean
Determines if the response is a successful one or not.
Constructor Details
#initialize(response, access_token = nil) ⇒ Response
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/verticalresponse/api/response.rb', line 12 def initialize(response, access_token = nil) @url = response['url'] @items = response['items'] @attributes = response['attributes'] @links = response['links'] @success = response['success'] @error = response['error'] @raw_response = response @access_token = access_token handle_error unless success? end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
9 10 11 |
# File 'lib/verticalresponse/api/response.rb', line 9 def access_token @access_token end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
9 10 11 |
# File 'lib/verticalresponse/api/response.rb', line 9 def attributes @attributes end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
9 10 11 |
# File 'lib/verticalresponse/api/response.rb', line 9 def error @error end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
9 10 11 |
# File 'lib/verticalresponse/api/response.rb', line 9 def items @items end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
9 10 11 |
# File 'lib/verticalresponse/api/response.rb', line 9 def links @links end |
#raw_response ⇒ Object (readonly)
Returns the value of attribute raw_response.
9 10 11 |
# File 'lib/verticalresponse/api/response.rb', line 9 def raw_response @raw_response end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
9 10 11 |
# File 'lib/verticalresponse/api/response.rb', line 9 def success @success end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
9 10 11 |
# File 'lib/verticalresponse/api/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
28 29 30 31 32 |
# File 'lib/verticalresponse/api/response.rb', line 28 def handle_collection items.map do |item| yield(Response.new(item, @access_token)) end end |
#handle_error ⇒ Object
Handles the case of an error response
40 41 42 43 44 45 46 47 48 |
# File 'lib/verticalresponse/api/response.rb', line 40 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
35 36 37 |
# File 'lib/verticalresponse/api/response.rb', line 35 def success? !error end |