Class: RestClient::AbstractResponse
- Inherits:
-
Object
- Object
- RestClient::AbstractResponse
show all
- Extended by:
- Forwardable
- Defined in:
- lib/rest_client/response.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(client, request, raw_response) ⇒ AbstractResponse
Returns a new instance of AbstractResponse.
22
23
24
25
26
27
|
# File 'lib/rest_client/response.rb', line 22
def initialize(client, request, raw_response)
@client = client
@request = request
@raw_response = raw_response
@raw_body = raw_response.body.to_s
end
|
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
12
13
14
|
# File 'lib/rest_client/response.rb', line 12
def client
@client
end
|
#raw_body ⇒ Object
Returns the value of attribute raw_body.
11
12
13
|
# File 'lib/rest_client/response.rb', line 11
def raw_body
@raw_body
end
|
#raw_response ⇒ Object
Returns the value of attribute raw_response.
10
11
12
|
# File 'lib/rest_client/response.rb', line 10
def raw_response
@raw_response
end
|
#request ⇒ Object
Returns the value of attribute request.
13
14
15
|
# File 'lib/rest_client/response.rb', line 13
def request
@request
end
|
Instance Method Details
#assert_ok! ⇒ Object
71
72
73
74
75
|
# File 'lib/rest_client/response.rb', line 71
def assert_ok!
return self if code >= 200 && code < 300
raise ApiError[code], self
end
|
#body ⇒ Object
Also known as:
to_json
17
18
19
|
# File 'lib/rest_client/response.rb', line 17
def body
@body ||= JSON.parse(raw_body)
end
|
#collection_response? ⇒ Boolean
37
38
39
|
# File 'lib/rest_client/response.rb', line 37
def collection_response?
page_items.nil? ? false : true
end
|
#count ⇒ Object
33
34
35
|
# File 'lib/rest_client/response.rb', line 33
def count
throw new NotImplementedError, 'Please implement "count" parsing in response class'
end
|
#each_page(&block) ⇒ Object
This method has a bit of complexity that is better kept in one location
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/rest_client/response.rb', line 78
def each_page(&block)
raise ArgumentError, 'Not a collection response' unless collection_response?
ret =
Enumerator.new do |y|
y << self
if next_page?
next_page.each_page.each do |page|
y << page
end
end
end
if block_given?
ret.map(&block)
else
ret.lazy
end
end
|
#error_message ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'lib/rest_client/response.rb', line 45
def error_message
parsed_message =
begin
body.dig('error', 'message') || body.dig('message')
rescue JSON::ParserError
nil
end
parsed_message || "#{code}: #{raw_response.body}"
end
|
#first ⇒ Object
105
106
107
108
109
|
# File 'lib/rest_client/response.rb', line 105
def first
raise ArgumentError, 'Not a collection response' unless collection_response?
page_items.first
end
|
#items ⇒ Object
99
100
101
102
103
|
# File 'lib/rest_client/response.rb', line 99
def items
return unless collection_response?
each_page.flat_map(&:page_items)
end
|
#next_page ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/rest_client/response.rb', line 61
def next_page
return unless next_page?
@next_page ||= @client.get(
@request[:url],
(@request[:query] || {}).merge(next_page_query)
)
@next_page.assert_ok!
end
|
#next_page? ⇒ Boolean
55
56
57
58
59
|
# File 'lib/rest_client/response.rb', line 55
def next_page?
return false unless collection_response?
page_items.length + skip < count
end
|
#next_page_query ⇒ Object
111
112
113
114
115
116
117
|
# File 'lib/rest_client/response.rb', line 111
def next_page_query
return unless collection_response?
{
skip: page_items.length + skip,
}
end
|
#page_items ⇒ Object
41
42
43
|
# File 'lib/rest_client/response.rb', line 41
def page_items
throw new NotImplementedError, 'Please implement "page_items" parsing in response class'
end
|
#skip ⇒ Object
29
30
31
|
# File 'lib/rest_client/response.rb', line 29
def skip
throw new NotImplementedError, 'Please implement "skip" parsing in response class'
end
|