Class: HornOfPlenty::Response
- Inherits:
-
Object
- Object
- HornOfPlenty::Response
show all
- Defined in:
- lib/horn_of_plenty/response.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(raw_response:) ⇒ Response
9
10
11
|
# File 'lib/horn_of_plenty/response.rb', line 9
def initialize(raw_response:)
self.raw_response = raw_response
end
|
Instance Attribute Details
#raw_response ⇒ Object
Returns the value of attribute raw_response.
7
8
9
|
# File 'lib/horn_of_plenty/response.rb', line 7
def raw_response
@raw_response
end
|
Class Method Details
.parse(raw_response) ⇒ Object
22
23
24
25
26
|
# File 'lib/horn_of_plenty/response.rb', line 22
def self.parse(raw_response)
new(raw_response: raw_response).tap do |response|
fail response.error unless response.successful?
end
end
|
Instance Method Details
#body ⇒ Object
28
29
30
|
# File 'lib/horn_of_plenty/response.rb', line 28
def body
@body ||= JSON.parse(raw_response.body) || ''
end
|
#items ⇒ Object
18
19
20
|
# File 'lib/horn_of_plenty/response.rb', line 18
def items
@items ||= body.is_a?(Array) ? body : [body]
end
|
#result ⇒ Object
13
14
15
16
|
# File 'lib/horn_of_plenty/response.rb', line 13
def result
@result ||= ::HornOfPlenty::Collection.new(items: items,
parser: parser_class)
end
|
#status_code ⇒ Object
32
33
34
|
# File 'lib/horn_of_plenty/response.rb', line 32
def status_code
@status_code ||= raw_response.status
end
|
#status_message ⇒ Object
36
37
38
|
# File 'lib/horn_of_plenty/response.rb', line 36
def status_message
@status_message ||= body['message']
end
|
#successful? ⇒ Boolean
40
41
42
|
# File 'lib/horn_of_plenty/response.rb', line 40
def successful?
(200..299).cover? status_code
end
|