Class: Amara::Response
- Inherits:
-
Object
show all
- Defined in:
- lib/amara/response.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(response, request = {}) ⇒ Response
def object
if objects.size == 0
nil
elsif objects.size == 1
objects.first
elsif objects.size > 1
objects
end
end
17
18
19
20
21
22
|
# File 'lib/amara/response.rb', line 17
def initialize(response, request={})
@raw = response
@request = request
check_for_error(response)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Coerce any method calls for body attributes
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/amara/response.rb', line 62
def method_missing(method_name, *args, &block)
if self.has_key?(method_name.to_s)
self.[](method_name, &block)
elsif self.objects.respond_to?(method_name)
self.objects.send(method_name, *args, &block)
elsif self.request[:api].respond_to?(method_name)
self.request[:api].send(method_name, *args, &block)
else
super
end
end
|
Instance Attribute Details
#raw ⇒ Object
Returns the value of attribute raw.
5
6
7
|
# File 'lib/amara/response.rb', line 5
def raw
@raw
end
|
#request ⇒ Object
Returns the value of attribute request.
5
6
7
|
# File 'lib/amara/response.rb', line 5
def request
@request
end
|
Instance Method Details
#[](key) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/amara/response.rb', line 48
def [](key)
if self.object.is_a?(Array)
self.object[key]
else
self.object.send(:"#{key}")
end
end
|
#body ⇒ Object
36
37
38
|
# File 'lib/amara/response.rb', line 36
def body
self.raw.body
end
|
#check_for_error(response) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/amara/response.rb', line 24
def check_for_error(response)
status_code_type = response.status.to_s[0]
case status_code_type
when "2"
when "4", "5"
raise "Whoops, error back from Amara: #{response.status}"
else
raise "Unrecongized status code: #{response.status}"
end
end
|
#has_key?(key) ⇒ Boolean
56
57
58
|
# File 'lib/amara/response.rb', line 56
def has_key?(key)
self.object.is_a?(Hash) && self.object.has_key?(key)
end
|
#has_next_page? ⇒ Boolean
89
90
91
|
# File 'lib/amara/response.rb', line 89
def has_next_page?
(offset + limit) < total_count
end
|
#has_previous_page? ⇒ Boolean
93
94
95
|
# File 'lib/amara/response.rb', line 93
def has_previous_page?
offset > 0
end
|
#limit ⇒ Object
84
85
86
87
|
# File 'lib/amara/response.rb', line 84
def limit
return 0 unless self.body.meta
self.body.meta.limit.to_i
end
|
#next_page ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/amara/response.rb', line 97
def next_page
return nil unless has_next_page?
new_offset = [(offset + limit), total_count].min
new_response = request[:api].request(request[:method], request[:path], request[:params].merge({offset: new_offset, limit: limit}))
self.raw = new_response.raw
self
end
|
#object ⇒ Object
40
41
42
|
# File 'lib/amara/response.rb', line 40
def object
body.objects.nil? ? body : body.objects
end
|
#objects ⇒ Object
44
45
46
|
# File 'lib/amara/response.rb', line 44
def objects
Array(self.object)
end
|
#offset ⇒ Object
74
75
76
77
|
# File 'lib/amara/response.rb', line 74
def offset
return 0 unless self.body.meta
self.body.meta.offset.to_i
end
|
#previous_page ⇒ Object
105
106
107
108
109
110
111
|
# File 'lib/amara/response.rb', line 105
def previous_page
return nil unless has_previous_page?
new_offset = [(offset - limit), 0].max
new_response = request[:api].request(request[:method], request[:path], request[:params].merge({offset: new_offset, limit: limit}))
self.raw = new_response.raw
self
end
|
#total_count ⇒ Object
79
80
81
82
|
# File 'lib/amara/response.rb', line 79
def total_count
return 0 unless self.body.meta
self.body.meta.total_count.to_i
end
|