Class: WCC::Contentful::SimpleClient::Response
- Inherits:
-
Object
- Object
- WCC::Contentful::SimpleClient::Response
show all
- Includes:
- Instrumentation
- Defined in:
- lib/wcc/contentful/simple_client/response.rb
Instance Attribute Summary collapse
#_instrumentation
Instance Method Summary
collapse
#_instrumentation_event_prefix, instrument
Constructor Details
#initialize(client, request, raw_response) ⇒ Response
Returns a new instance of Response.
61
62
63
64
65
66
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 61
def initialize(client, request, raw_response)
@client = client
@request = request
@raw_response = raw_response
@body = raw_response.body.to_s
end
|
Instance Attribute Details
#client ⇒ Object
9
10
11
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 9
def client
@client
end
|
#raw_response ⇒ Object
9
10
11
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 9
def raw_response
@raw_response
end
|
#request ⇒ Object
9
10
11
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 9
def request
@request
end
|
Instance Method Details
#assert_ok! ⇒ Object
68
69
70
71
72
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 68
def assert_ok!
return self if status >= 200 && status < 300
raise ApiError[status], self
end
|
#body ⇒ Object
15
16
17
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 15
def body
@body ||= raw_response.body.to_s
end
|
#count ⇒ Object
94
95
96
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 94
def count
total
end
|
#each_page(&block) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 74
def each_page(&block)
raise ArgumentError, 'Not a collection response' unless page_items
ret = PaginatingEnumerable.new(self)
if block_given?
ret.map(&block)
else
ret.lazy
end
end
|
#error_message ⇒ Object
24
25
26
27
28
29
30
31
32
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 24
def error_message
parsed_message =
begin
raw['message']
rescue JSON::ParserError
nil
end
parsed_message || "#{code}: #{raw_response.body}"
end
|
#first ⇒ Object
98
99
100
101
102
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 98
def first
raise ArgumentError, 'Not a collection response' unless page_items
page_items.first
end
|
#includes ⇒ Object
104
105
106
107
108
109
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 104
def includes
@includes ||=
raw['includes']&.each_with_object({}) do |(_t, entries), h|
entries&.each { |e| h[e.dig('sys', 'id')] = e }
end || {}
end
|
#items ⇒ Object
86
87
88
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 86
def items
each_page.flat_map(&:page_items)
end
|
#next_page ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 48
def next_page
return unless next_page?
query = (@request[:query] || {}).merge({
skip: page_items.length + skip
})
np =
_instrument 'page', url: @request[:url], query: query do
@client.get(@request[:url], query)
end
np.assert_ok!
end
|
#next_page? ⇒ Boolean
42
43
44
45
46
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 42
def next_page?
return unless raw.key? 'items'
page_items.length + skip < total
end
|
#page_items ⇒ Object
90
91
92
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 90
def page_items
raw['items']
end
|
#raw ⇒ Object
Also known as:
to_json
19
20
21
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 19
def raw
@raw ||= JSON.parse(body)
end
|
#skip ⇒ Object
34
35
36
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 34
def skip
raw['skip']
end
|
#total ⇒ Object
38
39
40
|
# File 'lib/wcc/contentful/simple_client/response.rb', line 38
def total
raw['total']
end
|