Class: WCC::Contentful::SimpleClient::Response
- Inherits:
-
Object
- Object
- WCC::Contentful::SimpleClient::Response
- Defined in:
- lib/wcc/contentful/simple_client/response.rb
Direct Known Subclasses
Instance Attribute Summary collapse
- #client ⇒ Object readonly
- #raw_response ⇒ Object readonly
- #request ⇒ Object readonly
Instance Method Summary collapse
- #assert_ok! ⇒ Object
- #body ⇒ Object
- #count ⇒ Object
- #each_page(&block) ⇒ Object
- #error_message ⇒ Object
- #first ⇒ Object
- #includes ⇒ Object
-
#initialize(client, request, raw_response) ⇒ Response
constructor
A new instance of Response.
- #items ⇒ Object
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #raw ⇒ Object (also: #to_json)
Constructor Details
#initialize(client, request, raw_response) ⇒ Response
Returns a new instance of Response.
43 44 45 46 47 48 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 43 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 (readonly)
6 7 8 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 6 def client @client end |
#raw_response ⇒ Object (readonly)
5 6 7 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 5 def raw_response @raw_response end |
#request ⇒ Object (readonly)
7 8 9 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 7 def request @request end |
Instance Method Details
#assert_ok! ⇒ Object
50 51 52 53 54 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 50 def assert_ok! return self if code >= 200 && code < 300 raise ApiError[code], self end |
#body ⇒ Object
12 13 14 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 12 def body @body ||= raw_response.body.to_s end |
#count ⇒ Object
83 84 85 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 83 def count raw['total'] end |
#each_page(&block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 56 def each_page(&block) raise ArgumentError, 'Not a collection response' unless raw['items'] 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
21 22 23 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 21 def raw.dig('message') || "#{code}: #{raw_response.}" end |
#first ⇒ Object
87 88 89 90 91 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 87 def first raise ArgumentError, 'Not a collection response' unless raw['items'] raw['items'].first end |
#includes ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 93 def includes @includes ||= raw.dig('includes')&.each_with_object({}) do |(_t, entries), h| entries.each { |e| h[e.dig('sys', 'id')] = e } end || {} return @includes unless @next_page # This could be more efficient - maybe not worth worrying about @includes.merge(@next_page.includes) end |
#items ⇒ Object
77 78 79 80 81 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 77 def items each_page.flat_map do |page| page.raw['items'] end end |
#next_page ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 31 def next_page return unless next_page? @next_page ||= @client.get( @request[:url], (@request[:query] || {}).merge({ skip: raw['items'].length + raw['skip'] }) ) @next_page.assert_ok! end |
#next_page? ⇒ Boolean
25 26 27 28 29 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 25 def next_page? return unless raw.key? 'items' raw['items'].length + raw['skip'] < raw['total'] end |
#raw ⇒ Object Also known as: to_json
16 17 18 |
# File 'lib/wcc/contentful/simple_client/response.rb', line 16 def raw @raw ||= JSON.parse(body) end |