Class: Etsy::Response
- Inherits:
-
Object
- Object
- Etsy::Response
- Defined in:
- lib/etsy/response.rb
Overview
Response
Basic wrapper around the Etsy JSON response data
Instance Method Summary collapse
- #body ⇒ Object
- #code ⇒ Object
-
#count ⇒ Object
Number of records in the response results.
-
#initialize(raw_response) ⇒ Response
constructor
Create a new response based on the raw HTTP response.
- #paginated? ⇒ Boolean
-
#result ⇒ Object
Results of the API request.
- #success? ⇒ Boolean
-
#to_hash ⇒ Object
Convert the raw JSON data to a hash.
-
#total ⇒ Object
Total number of results of the request.
Constructor Details
#initialize(raw_response) ⇒ Response
Create a new response based on the raw HTTP response
25 26 27 |
# File 'lib/etsy/response.rb', line 25 def initialize(raw_response) @raw_response = raw_response end |
Instance Method Details
#body ⇒ Object
35 36 37 |
# File 'lib/etsy/response.rb', line 35 def body @raw_response.body end |
#code ⇒ Object
39 40 41 |
# File 'lib/etsy/response.rb', line 39 def code @raw_response.code end |
#count ⇒ Object
Number of records in the response results
44 45 46 47 48 49 50 |
# File 'lib/etsy/response.rb', line 44 def count if paginated? to_hash['results'].nil? ? 0 : to_hash['results'].size else to_hash['count'] end end |
#paginated? ⇒ Boolean
71 72 73 |
# File 'lib/etsy/response.rb', line 71 def paginated? !!to_hash['pagination'] end |
#result ⇒ Object
Results of the API request
53 54 55 56 57 58 59 60 |
# File 'lib/etsy/response.rb', line 53 def result if success? results = to_hash['results'] || [] count == 1 ? results.first : results else Etsy.silent_errors ? [] : validate! end end |
#success? ⇒ Boolean
67 68 69 |
# File 'lib/etsy/response.rb', line 67 def success? !!(code =~ /2\d\d/) end |
#to_hash ⇒ Object
Convert the raw JSON data to a hash
30 31 32 33 |
# File 'lib/etsy/response.rb', line 30 def to_hash validate! @hash ||= json end |
#total ⇒ Object
Total number of results of the request
63 64 65 |
# File 'lib/etsy/response.rb', line 63 def total @total ||= to_hash['count'] end |