class Klimt::GravityClient

Constants

DEFAULT_PAGE_SIZE
HOSTS

Attributes

token[R]

Public Class Methods

new(env:) click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 13
def initialize(env:)
  @host = host_from_environment(env)
  @token = find_or_create_token
end

Public Instance Methods

count(type:, params: []) click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 33
def count(type:, params: [])
  params = parse_params(params)
  params[:size] = 0
  params[:total_count] = true
  uri = "https://#{@host}/api/v1/#{type}"
  response = Typhoeus.get(uri, headers: headers, params: params)
  response.headers['X-Total-Count']
end
find(type:, id:) click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 18
def find(type:, id:)
  uri = "https://#{@host}/api/v1/#{type}/#{id}"
  response = Typhoeus.get(uri, headers: headers)
  raise response.body unless response.success?
  response.body
end
list(type:, params: []) click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 25
def list(type:, params: [])
  params = parse_params(params)
  uri = "https://#{@host}/api/v1/#{type}"
  response = Typhoeus.get(uri, headers: headers, params: params)
  raise response.body unless response.success?
  response.body
end
partner_locations(id:, params: []) click to toggle source

partners

# File lib/klimt/clients/gravity_client.rb, line 54
def partner_locations(id:, params: [])
  params = parse_params(params)
  uri = "https://#{@host}/api/v1/partner/#{id}/locations"
  response = Typhoeus.get(uri, headers: headers, params: params)
  raise response.body unless response.success?
  response.body
end
partner_locations_count(id:, params: []) click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 62
def partner_locations_count(id:, params: [])
  params = parse_params(params).merge(size: 0, total_count: true)
  uri = "https://#{@host}/api/v1/partner/#{id}/locations"
  response = Typhoeus.get(uri, headers: headers, params: params)
  response.headers['X-Total-Count']
end
partner_near(params: []) click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 69
def partner_near(params: [])
  params = parse_params(params)
  raise ArgumentError, 'a "near=LNG,LAT" parameter is required' unless params.include? 'near'
  uri = "https://#{@host}/api/v1/partners"
  response = Typhoeus.get(uri, headers: headers, params: params)
  raise response.body unless response.success?
  response.body
end

Private Instance Methods

ask_for_credentials() click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 131
def ask_for_credentials
  cli = HighLine.new
  cli.say 'No login credentials found in .netrc'
  cli.say 'Please login now'
  cli.say '-----'
  email = cli.ask('Artsy email    : ') {}
  pass  = cli.ask('Artsy password : ') { |q| q.echo = 'x' }
  [email, pass]
end
find_or_create_token() click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 102
def find_or_create_token
  _user, token = Netrc.read[@host]
  token || generate_token
end
generate_token() click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 107
def generate_token
  email, pass = ask_for_credentials
  params = {
    client_id: ENV['KLIMT_ID'],
    client_secret: ENV['KLIMT_SECRET'],
    grant_type: 'credentials',
    email: email,
    password: pass
  }
  response = Typhoeus.get "https://#{@host}/oauth2/access_token", params: params
  body = JSON.parse(response.body)
  quit "Login raiseed: #{body['error_description']}" unless response.success?
  body['access_token'].tap do |new_token|
    netrc = Netrc.read
    netrc[@host] = email, new_token
    netrc.save
  end
end
headers() click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 89
def headers
  {
    'User-Agent' => "Klimt #{Klimt::VERSION}",
    'Content-type' => 'application/json',
    'Accept' => 'application/json',
    'X-ACCESS-TOKEN' => @token
  }
end
host_from_environment(env) click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 98
def host_from_environment(env)
  HOSTS[env.to_sym]
end
parse_params(params) click to toggle source

Turn this from the command line:

["size=10", "page=2"]

into this for Typhoeus:

{'size' => 10, 'page' => 2}
# File lib/klimt/clients/gravity_client.rb, line 85
def parse_params(params)
  Hash[params.map { |pair| pair.split('=') }]
end
quit(msg) click to toggle source
# File lib/klimt/clients/gravity_client.rb, line 126
def quit(msg)
  $stderr.puts msg
  exit 1
end