Class: WCC::Blogs::Client::Response
- Inherits:
-
Struct
- Object
- Struct
- WCC::Blogs::Client::Response
- Extended by:
- Forwardable
- Defined in:
- lib/wcc/blogs/client/response.rb
Overview
rubocop:disable Metrics/BlockLength
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#raw_response ⇒ Object
Returns the value of attribute raw_response.
-
#request ⇒ Object
Returns the value of attribute request.
Instance Method Summary collapse
- #assert_ok! ⇒ Object
- #body ⇒ Object
- #count ⇒ Object
-
#each_page(&block) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #first ⇒ Object
- #items ⇒ Object
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #next_page_url ⇒ Object
- #page_items ⇒ Object
- #raw ⇒ Object (also: #to_json)
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client
7 8 9 |
# File 'lib/wcc/blogs/client/response.rb', line 7 def client @client end |
#raw_response ⇒ Object
Returns the value of attribute raw_response
7 8 9 |
# File 'lib/wcc/blogs/client/response.rb', line 7 def raw_response @raw_response end |
#request ⇒ Object
Returns the value of attribute request
7 8 9 |
# File 'lib/wcc/blogs/client/response.rb', line 7 def request @request end |
Instance Method Details
#assert_ok! ⇒ Object
38 39 40 41 42 |
# File 'lib/wcc/blogs/client/response.rb', line 38 def assert_ok! return self if status >= 200 && status < 300 raise ApiError[status], self end |
#body ⇒ Object
13 14 15 |
# File 'lib/wcc/blogs/client/response.rb', line 13 def body @body ||= raw_response.body.to_s end |
#count ⇒ Object
73 74 75 |
# File 'lib/wcc/blogs/client/response.rb', line 73 def count total end |
#each_page(&block) ⇒ Object
rubocop:disable Metrics/MethodLength
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/wcc/blogs/client/response.rb', line 44 def each_page(&block) # rubocop:disable Metrics/MethodLength raise ArgumentError, 'Not a collection response' unless page_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 |
#first ⇒ Object
77 78 79 80 81 |
# File 'lib/wcc/blogs/client/response.rb', line 77 def first raise ArgumentError, 'Not a collection response' unless page_items page_items.first end |
#items ⇒ Object
65 66 67 |
# File 'lib/wcc/blogs/client/response.rb', line 65 def items each_page.flat_map(&:page_items) end |
#next_page ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/wcc/blogs/client/response.rb', line 30 def next_page return unless next_page_url return @next_page if @next_page np = client.get(next_page_url, request[:query]) @next_page = np.assert_ok! end |
#next_page? ⇒ Boolean
26 27 28 |
# File 'lib/wcc/blogs/client/response.rb', line 26 def next_page? !!next_page_url end |
#next_page_url ⇒ Object
22 23 24 |
# File 'lib/wcc/blogs/client/response.rb', line 22 def next_page_url raw.dig('_links', 'next') end |
#page_items ⇒ Object
69 70 71 |
# File 'lib/wcc/blogs/client/response.rb', line 69 def page_items raw['items'] end |
#raw ⇒ Object Also known as: to_json
17 18 19 |
# File 'lib/wcc/blogs/client/response.rb', line 17 def raw @raw ||= JSON.parse(body) end |