Module: TocDoc::Connection
- Included in:
- Client
- Defined in:
- lib/toc_doc/core/connection.rb
Overview
Faraday-based HTTP connection and request helpers.
Included into Client to provide low-level HTTP verbs
(get, post, put, patch, delete, head), a memoised
Faraday connection, pagination support, and response tracking.
#get and #paginate are public so that model classes (e.g.
Availability) can call them via TocDoc.client; all other
HTTP verb methods remain private.
Instance Attribute Summary collapse
-
#last_response ⇒ Faraday::Response?
The most-recent raw Faraday response, set after every request.
Instance Method Summary collapse
-
#get(path, options = {}) ⇒ Object
Perform a GET request.
-
#paginate(path, options = {}) {|acc, last_response| ... } ⇒ Object
Performs a paginated GET, accumulating results across pages.
Instance Attribute Details
#last_response ⇒ Faraday::Response?
The most-recent raw Faraday response, set after every request.
21 22 23 |
# File 'lib/toc_doc/core/connection.rb', line 21 def last_response @last_response end |
Instance Method Details
#get(path, options = {}) ⇒ Object
Perform a GET request.
30 31 32 |
# File 'lib/toc_doc/core/connection.rb', line 30 def get(path, = {}) request(:get, path, nil, ) end |
#paginate(path, options = {}) {|acc, last_response| ... } ⇒ Object
Performs a paginated GET, accumulating results across pages.
Behaves exactly like #get when no block is given.
When a block is provided, it is yielded after every page fetch — including the first — with +(accumulator, last_response)+. The block must:
- Detect whether it is a continuation call by comparing object identity:
acc.equal?(last_response.body)istrueonly on the first yield, when the accumulator is the first-page body. On subsequent yields the block should mergelast_response.bodyintoacc. - Return a Hash of options to pass to the next #get call (pagination
continues), or
nil/falseto halt.
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/toc_doc/core/connection.rb', line 147 def paginate(path, = {}, &) data = get(path, ) return data unless block_given? loop do = yield(data, last_response) break unless get(path, ) end data end |