class Stattleship::Client
Attributes
base_uri[R]
path[R]
query[R]
token[R]
Public Class Methods
new(path:, query: nil, token: Stattleship.configuration.api_token)
click to toggle source
# File lib/stattleship/client/client.rb, line 3 def initialize(path:, query: nil, token: Stattleship.configuration.api_token) @base_uri = Stattleship.configuration.base_uri.freeze @path = path @query = query @token = token end
Public Instance Methods
fetch()
click to toggle source
# File lib/stattleship/client/client.rb, line 22 def fetch Stattleship.configuration.http.request(endpoint) rescue StandardError => e puts "HTTP Request failed (#{e.message})" raise e end
headers()
click to toggle source
# File lib/stattleship/client/client.rb, line 12 def headers { 'Accept' => 'application/vnd.stattleship.com; version=1', 'Authorization' => "Token token=#{token}", 'Content-Type' => 'application/json', 'User-Agent' => "Stattleship-Ruby/#{Stattleship::Ruby::VERSION} (#{RUBY_PLATFORM})" } end
paginate(model:)
click to toggle source
# File lib/stattleship/client/client.rb, line 29 def paginate(model:) data = [] response = fetch while keep_fetching?(response: response) data << build(model: model, response: response) if next_url(response: response) response = fetch_next_url end end data.flatten end
Private Instance Methods
build(model:, response:)
click to toggle source
# File lib/stattleship/client/client.rb, line 48 def build(model:, response:) builder = model.new builder.extend(Kernel.const_get("#{builder.class.name}Representer")) builder.from_json(response.body).data end
endpoint()
click to toggle source
# File lib/stattleship/client/client.rb, line 97 def endpoint Net::HTTP::Get.new(url, headers) end
fetch_next_url()
click to toggle source
# File lib/stattleship/client/client.rb, line 58 def fetch_next_url sleep(0.25) response = fetch unless response.is_a?(Net::HTTPSuccess) @url = nil end response end
keep_fetching?(response:)
click to toggle source
# File lib/stattleship/client/client.rb, line 54 def keep_fetching?(response:) response.is_a?(Net::HTTPSuccess) && @url end
next_url(response:)
click to toggle source
# File lib/stattleship/client/client.rb, line 70 def next_url(response:) @url = nil headers = response.header.to_hash links = (headers['link'] || []).first LinkHeader.parse(links).links.map do |link| if link.attrs['rel'] == 'next' @url = link.href return true end end false end
url()
click to toggle source
# File lib/stattleship/client/client.rb, line 86 def url @url ||= begin if query template = Addressable::Template.new("#{base_uri}/#{path}{?query*}") template.partial_expand(query).pattern else "#{base_uri}/#{path}" end end end