Class: Etsy::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/etsy/response.rb

Overview

Response

Basic wrapper around the Etsy JSON response data

Instance Method Summary collapse

Constructor Details

#initialize(raw_response) ⇒ Response

Create a new response based on the raw HTTP response



16
17
18
# File 'lib/etsy/response.rb', line 16

def initialize(raw_response)
  @raw_response = raw_response
end

Instance Method Details

#bodyObject



26
27
28
# File 'lib/etsy/response.rb', line 26

def body
  @raw_response.body
end

#codeObject



30
31
32
# File 'lib/etsy/response.rb', line 30

def code
  @raw_response.code
end

#countObject

Number of records in the response results



35
36
37
38
39
40
41
# File 'lib/etsy/response.rb', line 35

def count
  if paginated?
    to_hash['results'].nil? ? 0 : to_hash['results'].size
  else
    to_hash['count']
  end
end

#paginated?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/etsy/response.rb', line 57

def paginated?
  !!to_hash['pagination']
end

#resultObject

Results of the API request



44
45
46
47
48
49
50
51
# File 'lib/etsy/response.rb', line 44

def result
  if success?
    results = to_hash['results'] || []
    count == 1 ? results.first : results
  else
    []
  end
end

#success?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/etsy/response.rb', line 53

def success?
  !!(code =~ /2\d\d/)
end

#to_hashObject

Convert the raw JSON data to a hash



21
22
23
24
# File 'lib/etsy/response.rb', line 21

def to_hash
  validate!
  @hash ||= json
end