Class: Wiq::Client
- Inherits:
-
Object
- Object
- Wiq::Client
- Defined in:
- lib/wiq/client.rb
Overview
Thin wrapper around Faraday that:
- Adds Authorization: Bearer <pat>
- Parses JSON requests and responses
- Retries on 429 and 5xx (Retry-After honored)
- Translates non-2xx responses into Wiq::APIError
- Exposes paginate(path, params) that walks the Link: rel=next chain
Constant Summary collapse
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
Instance Method Summary collapse
-
#collect_all(path, params = {}, key:) ⇒ Object
Collects every page of an index into a single array.
- #delete(path, params = {}) ⇒ Object
- #get(path, params = {}) ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
-
#paginate(path, params = {}, key:) ⇒ Object
Yields each page’s records until rel=next runs out.
- #post(path, body = {}) ⇒ Object
- #put(path, body = {}) ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 |
# File 'lib/wiq/client.rb', line 20 def initialize(config) @config = config config.require_host! @host = config.host @token = config.token end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
18 19 20 |
# File 'lib/wiq/client.rb', line 18 def host @host end |
Instance Method Details
#collect_all(path, params = {}, key:) ⇒ Object
Collects every page of an index into a single array.
63 64 65 66 67 68 69 70 71 |
# File 'lib/wiq/client.rb', line 63 def collect_all(path, params = {}, key:) all = [] total = nil paginate(path, params, key: key) do |records, _resp, t| total ||= t all.concat(records) end [all, total] end |
#delete(path, params = {}) ⇒ Object
39 40 41 |
# File 'lib/wiq/client.rb', line 39 def delete(path, params = {}) request(:delete, path, params: params) end |
#get(path, params = {}) ⇒ Object
27 28 29 |
# File 'lib/wiq/client.rb', line 27 def get(path, params = {}) request(:get, path, params: params) end |
#paginate(path, params = {}, key:) ⇒ Object
Yields each page’s records until rel=next runs out. All /api/v1 index endpoints wrap their array under the resource name (e.g. ‘[…]`) — callers must pass `key:` so we can unwrap. Yields (records_array, response, total_count).
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/wiq/client.rb', line 47 def paginate(path, params = {}, key:) url = absolute(path) first = true loop do resp = first ? request(:get, path, params: params, raw: true) : request(:get, url, raw: true) first = false records = extract_records(resp.body, key) total = Pagination.total_count(resp.headers) yield records, resp, total url = Pagination.next_url(resp.headers["Link"] || resp.headers["link"]) break unless url end end |
#post(path, body = {}) ⇒ Object
31 32 33 |
# File 'lib/wiq/client.rb', line 31 def post(path, body = {}) request(:post, path, body: body) end |
#put(path, body = {}) ⇒ Object
35 36 37 |
# File 'lib/wiq/client.rb', line 35 def put(path, body = {}) request(:put, path, body: body) end |