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
23
24
|
# 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
107
108
109
|
# File 'lib/garage_client/response.rb', line 107
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/garage_client/response.rb', line 38
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
93
94
95
|
# File 'lib/garage_client/response.rb', line 93
def first_page_link
.try(:find_link, %w[rel first])
end
|
#first_page_path ⇒ Object
61
62
63
|
# File 'lib/garage_client/response.rb', line 61
def first_page_path
first_page_link.try(:href)
end
|
#has_first_page? ⇒ Boolean
77
78
79
|
# File 'lib/garage_client/response.rb', line 77
def has_first_page?
!!first_page_link
end
|
#has_last_page? ⇒ Boolean
81
82
83
|
# File 'lib/garage_client/response.rb', line 81
def has_last_page?
!!last_page_link
end
|
#has_next_page? ⇒ Boolean
69
70
71
|
# File 'lib/garage_client/response.rb', line 69
def has_next_page?
!!next_page_link
end
|
#has_prev_page? ⇒ Boolean
73
74
75
|
# File 'lib/garage_client/response.rb', line 73
def has_prev_page?
!!prev_page_link
end
|
#last_page_link ⇒ Object
97
98
99
|
# File 'lib/garage_client/response.rb', line 97
def last_page_link
.try(:find_link, %w[rel last])
end
|
#last_page_path ⇒ Object
65
66
67
|
# File 'lib/garage_client/response.rb', line 65
def last_page_path
last_page_link.try(:href)
end
|
#link ⇒ Object
26
27
28
|
# File 'lib/garage_client/response.rb', line 26
def link
@link ||= response.['Link']
end
|
#next_page_link ⇒ Object
85
86
87
|
# File 'lib/garage_client/response.rb', line 85
def next_page_link
.try(:find_link, %w[rel next])
end
|
#next_page_path ⇒ Object
53
54
55
|
# File 'lib/garage_client/response.rb', line 53
def next_page_path
next_page_link.try(:href)
end
|
#prev_page_link ⇒ Object
89
90
91
|
# File 'lib/garage_client/response.rb', line 89
def prev_page_link
.try(:find_link, %w[rel prev])
end
|
#prev_page_path ⇒ Object
57
58
59
|
# File 'lib/garage_client/response.rb', line 57
def prev_page_path
prev_page_link.try(:href)
end
|
#respond_to_missing?(name, include_private) ⇒ Boolean
101
102
103
|
# File 'lib/garage_client/response.rb', line 101
def respond_to_missing?(name, include_private)
body.respond_to?(name, include_private)
end
|
#total_count ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/garage_client/response.rb', line 30
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
|