Class: GarageClient::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/garage_client/response.rb,
lib/garage_client/response/cacheable.rb,
lib/garage_client/response/raise_http_exception.rb

Defined Under Namespace

Classes: Cacheable, RaiseHttpException

Constant Summary collapse

MIME_DICT =
%r{application/vnd\.cookpad\.dictionary\+(json|x-msgpack)}
ACCEPT_BODY_TYPES =
[Array, Hash, NilClass]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, response) ⇒ Response

Returns a new instance of Response.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/garage_client/response.rb', line 10

def initialize(client, response)
  @client = client
  @response = response

  # faraday-net_http adapter converts the response body from nil to '' to ensure the body is a string.
  # https://github.com/lostisland/faraday/commit/f41ffaabb72d3700338296c79a2084880e6a9843
  # https://github.com/lostisland/faraday-net_http/blob/11953160f75dd488133a74857c2b07d41be8995d/lib/faraday/adapter/net_http.rb#L186
  response.env[:body] = nil if response.env[:body] == ''

  unless ACCEPT_BODY_TYPES.any? {|type| type === response.body }
    raise GarageClient::InvalidResponseType, "Invalid response type (#{response.body.class}): #{response.body}"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



105
106
107
# File 'lib/garage_client/response.rb', line 105

def method_missing(name, *args, &block)
  body.send(name, *args, &block)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/garage_client/response.rb', line 8

def client
  @client
end

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/garage_client/response.rb', line 8

def response
  @response
end

Instance Method Details

#bodyObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/garage_client/response.rb', line 36

def body
  @body ||= case response.body
            when Array
              response.body.map {|res| GarageClient::Resource.new(client, res) }
            when Hash
              if dictionary_response?
                Hash[response.body.map {|id, res| [id, GarageClient::Resource.new(client, res)] }]
              else
                GarageClient::Resource.new(client, response.body)
              end
            when NilClass
              nil
            end
end


91
92
93
# File 'lib/garage_client/response.rb', line 91

def first_page_link
  parsed_link_header.try(:find_link, %w[rel first])
end

#first_page_pathObject



59
60
61
# File 'lib/garage_client/response.rb', line 59

def first_page_path
  first_page_link.try(:href)
end

#has_first_page?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/garage_client/response.rb', line 75

def has_first_page?
  !!first_page_link
end

#has_last_page?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/garage_client/response.rb', line 79

def has_last_page?
  !!last_page_link
end

#has_next_page?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/garage_client/response.rb', line 67

def has_next_page?
  !!next_page_link
end

#has_prev_page?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/garage_client/response.rb', line 71

def has_prev_page?
  !!prev_page_link
end


95
96
97
# File 'lib/garage_client/response.rb', line 95

def last_page_link
  parsed_link_header.try(:find_link, %w[rel last])
end

#last_page_pathObject



63
64
65
# File 'lib/garage_client/response.rb', line 63

def last_page_path
  last_page_link.try(:href)
end


24
25
26
# File 'lib/garage_client/response.rb', line 24

def link
  @link ||= response.headers['Link']
end


83
84
85
# File 'lib/garage_client/response.rb', line 83

def next_page_link
  parsed_link_header.try(:find_link, %w[rel next])
end

#next_page_pathObject



51
52
53
# File 'lib/garage_client/response.rb', line 51

def next_page_path
  next_page_link.try(:href)
end


87
88
89
# File 'lib/garage_client/response.rb', line 87

def prev_page_link
  parsed_link_header.try(:find_link, %w[rel prev])
end

#prev_page_pathObject



55
56
57
# File 'lib/garage_client/response.rb', line 55

def prev_page_path
  prev_page_link.try(:href)
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/garage_client/response.rb', line 99

def respond_to_missing?(name, include_private)
  body.respond_to?(name, include_private)
end

#total_countObject



28
29
30
31
32
33
34
# File 'lib/garage_client/response.rb', line 28

def total_count
  unless @total_count
    @total_count = response.headers['X-List-TotalCount']
    @total_count = @total_count.to_i if @total_count
  end
  @total_count
end