Sha256: 5ad038cf2e177cbf4a89f6d798b51ae2076d48c4435500bf0ebfb81a79401bde
Contents?: true
Size: 1.75 KB
Versions: 4
Compression:
Stored size: 1.75 KB
Contents
module QuizApiClient class HttpClient include HTTParty class RequestFailed < StandardError; end attr_reader :jwt, :uri def initialize(uri:, jwt:) @uri = uri @jwt = jwt end def get(path, all: false, query: {}) return make_request :get, path, query: query unless all make_paginated_request :get, path, query: query end def post(path, body = {}) make_request :post, path, body: body.to_json end def patch(path, body = {}) make_request :patch, path, body: body.to_json end def put(path, body = {}) make_request :put, path, body: body.to_json end def delete(path) make_request :delete, path end private def make_request(method, path, request_options = {}) self.class.send( method, "#{uri}#{path}", default_request_data.merge(request_options) ) rescue HTTParty::Error, Errno::ECONNREFUSED, Net::ReadTimeout => e raise RequestFailed, e.message end def make_paginated_request(method, path, request_options) entities = [] request_path = path until request_path.nil? do resp = make_request(method, request_path, request_options) entities.concat(resp.parsed_response) request_path = next_page(resp) end entities end def default_request_data { headers: { 'Authorization' => jwt, 'AuthType' => 'Signature', 'Accept' => 'application/json', 'Content-Type' => 'application/json' } } end def next_page(response) links = LinkHeader.parse(response.headers['link']).links next_link = links.find { |link| link['rel'] == 'next' } next_link.href if next_link end end end
Version data entries
4 entries across 4 versions & 1 rubygems