Class: WCC::JsonAPI::BaseClient::BaseResponse
- Inherits:
-
Object
- Object
- WCC::JsonAPI::BaseClient::BaseResponse
- Extended by:
- Forwardable
- Defined in:
- lib/wcc/json_api/base_client/base_response.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#raw_body ⇒ Object
readonly
Returns the value of attribute raw_body.
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
- #assert_ok! ⇒ Object
- #body ⇒ Object (also: #to_json)
- #collection_response? ⇒ Boolean
- #count ⇒ Object
- #data ⇒ Object
-
#each_page(&block) ⇒ Object
This method has a bit of complexity that is better kept in one location.
- #error_message ⇒ Object
- #first ⇒ Object
-
#initialize(client, request, raw_response) ⇒ BaseResponse
constructor
A new instance of BaseResponse.
- #items ⇒ Object
- #links ⇒ Object
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #next_page_query ⇒ Object
- #skip ⇒ Object
Constructor Details
#initialize(client, request, raw_response) ⇒ BaseResponse
Returns a new instance of BaseResponse.
24 25 26 27 28 29 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 24 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 (readonly)
Returns the value of attribute client.
13 14 15 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 13 def client @client end |
#raw_body ⇒ Object (readonly)
Returns the value of attribute raw_body.
12 13 14 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 12 def raw_body @raw_body end |
#raw_response ⇒ Object (readonly)
Returns the value of attribute raw_response.
11 12 13 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 11 def raw_response @raw_response end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
14 15 16 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 14 def request @request end |
Instance Method Details
#assert_ok! ⇒ Object
78 79 80 81 82 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 78 def assert_ok! return self if code >= 200 && code < 300 raise ApiError[code], self end |
#body ⇒ Object Also known as: to_json
19 20 21 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 19 def body @body ||= ::JSON.parse(raw_body) end |
#collection_response? ⇒ Boolean
47 48 49 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 47 def collection_response? data.is_a?(Array) ? true : false end |
#count ⇒ Object
35 36 37 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 35 def count body.dig('meta', 'total') end |
#data ⇒ Object
39 40 41 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 39 def data body['data'] end |
#each_page(&block) ⇒ Object
This method has a bit of complexity that is better kept in one location
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 85 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
51 52 53 54 55 56 57 58 59 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 51 def = begin body.dig('error', 'message') || body.dig('message') rescue ::JSON::ParserError nil end || "#{code}: #{raw_response.body}" end |
#first ⇒ Object
112 113 114 115 116 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 112 def first raise ArgumentError, 'Not a collection response' unless collection_response? page_items.first end |
#items ⇒ Object
106 107 108 109 110 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 106 def items return unless collection_response? each_page.flat_map(&:page_items) end |
#links ⇒ Object
43 44 45 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 43 def links @links ||= OpenStruct.new(body['links']) end |
#next_page ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 68 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
61 62 63 64 65 66 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 61 def next_page? return false unless collection_response? return false if count.nil? page_items.length + skip < count end |
#next_page_query ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 118 def next_page_query return unless collection_response? { skip: page_items.length + skip } end |
#skip ⇒ Object
31 32 33 |
# File 'lib/wcc/json_api/base_client/base_response.rb', line 31 def skip body.dig('meta', 'skip') end |