Class: Stattleship::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/stattleship/client/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(path:, query: nil, token: Stattleship.configuration.api_token) ⇒ Client

Returns a new instance of Client.


3
4
5
6
7
8
9
10
# 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

Instance Method Details

#fetchObject


22
23
24
25
26
27
# 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

#headersObject


12
13
14
15
16
17
18
19
20
# 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:) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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