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
18 19 20 |
# File 'lib/etsy/response.rb', line 18 def initialize(raw_response) @raw_response = raw_response end |
Instance Method Details
#body ⇒ Object
28 29 30 |
# File 'lib/etsy/response.rb', line 28 def body @raw_response.body end |
#code ⇒ Object
32 33 34 |
# File 'lib/etsy/response.rb', line 32 def code @raw_response.code end |
#count ⇒ Object
Number of records in the response results
37 38 39 40 41 42 43 |
# File 'lib/etsy/response.rb', line 37 def count if paginated? to_hash['results'].nil? ? 0 : to_hash['results'].size else to_hash['count'] end end |
#paginated? ⇒ Boolean
64 65 66 |
# File 'lib/etsy/response.rb', line 64 def paginated? !!to_hash['pagination'] end |
#result ⇒ Object
Results of the API request
46 47 48 49 50 51 52 53 |
# File 'lib/etsy/response.rb', line 46 def result if success? results = to_hash['results'] || [] count == 1 ? results.first : results else [] end end |
#success? ⇒ Boolean
60 61 62 |
# File 'lib/etsy/response.rb', line 60 def success? !!(code =~ /2\d\d/) end |
#to_hash ⇒ Object
Convert the raw JSON data to a hash
23 24 25 26 |
# File 'lib/etsy/response.rb', line 23 def to_hash validate! @hash ||= json end |
#total ⇒ Object
Total number of results of the request
56 57 58 |
# File 'lib/etsy/response.rb', line 56 def total @total ||= to_hash['count'] end |