Class: GarageClient::Response
- Inherits:
-
Object
- Object
- GarageClient::Response
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
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
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
#client ⇒ Object
Returns the value of attribute client.
8
9
10
|
# File 'lib/garage_client/response.rb', line 8
def client
@client
end
|
#response ⇒ Object
Returns the value of attribute response.
8
9
10
|
# File 'lib/garage_client/response.rb', line 8
def response
@response
end
|
Instance Method Details
#body ⇒ Object
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
|
#first_page_link ⇒ Object
91
92
93
|
# File 'lib/garage_client/response.rb', line 91
def first_page_link
.try(:find_link, %w[rel first])
end
|
#first_page_path ⇒ Object
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
75
76
77
|
# File 'lib/garage_client/response.rb', line 75
def has_first_page?
!!first_page_link
end
|
#has_last_page? ⇒ Boolean
79
80
81
|
# File 'lib/garage_client/response.rb', line 79
def has_last_page?
!!last_page_link
end
|
#has_next_page? ⇒ Boolean
67
68
69
|
# File 'lib/garage_client/response.rb', line 67
def has_next_page?
!!next_page_link
end
|
#has_prev_page? ⇒ Boolean
71
72
73
|
# File 'lib/garage_client/response.rb', line 71
def has_prev_page?
!!prev_page_link
end
|
#last_page_link ⇒ Object
95
96
97
|
# File 'lib/garage_client/response.rb', line 95
def last_page_link
.try(:find_link, %w[rel last])
end
|
#last_page_path ⇒ Object
63
64
65
|
# File 'lib/garage_client/response.rb', line 63
def last_page_path
last_page_link.try(:href)
end
|
#link ⇒ Object
24
25
26
|
# File 'lib/garage_client/response.rb', line 24
def link
@link ||= response.['Link']
end
|
#next_page_link ⇒ Object
83
84
85
|
# File 'lib/garage_client/response.rb', line 83
def next_page_link
.try(:find_link, %w[rel next])
end
|
#next_page_path ⇒ Object
51
52
53
|
# File 'lib/garage_client/response.rb', line 51
def next_page_path
next_page_link.try(:href)
end
|
#prev_page_link ⇒ Object
87
88
89
|
# File 'lib/garage_client/response.rb', line 87
def prev_page_link
.try(:find_link, %w[rel prev])
end
|
#prev_page_path ⇒ Object
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
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_count ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/garage_client/response.rb', line 28
def total_count
unless @total_count
@total_count = response.['X-List-TotalCount']
@total_count = @total_count.to_i if @total_count
end
@total_count
end
|