Class: Klimt::GravityClient

Inherits:
Object
  • Object
show all
Defined in:
lib/klimt/clients/gravity_client.rb

Constant Summary collapse

HOSTS =
{ production: 'api.artsy.net', staging: 'stagingapi.artsy.net' }.freeze
DEFAULT_PAGE_SIZE =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:) ⇒ GravityClient

Returns a new instance of GravityClient.



13
14
15
16
# File 'lib/klimt/clients/gravity_client.rb', line 13

def initialize(env:)
  @host = host_from_environment(env)
  @token = find_or_create_token
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/klimt/clients/gravity_client.rb', line 8

def token
  @token
end

Instance Method Details

#count(type:, params: []) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/klimt/clients/gravity_client.rb', line 31

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:) ⇒ Object



18
19
20
21
22
# 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)
  response.body
end

#list(type:, params: []) ⇒ Object



24
25
26
27
28
29
# File 'lib/klimt/clients/gravity_client.rb', line 24

def list(type:, params: [])
  params = parse_params(params)
  uri = "https://#{@host}/api/v1/#{type}"
  response = Typhoeus.get(uri, headers: headers, params: params)
  response.body
end

#partner_locations(id:, params: []) ⇒ Object

partners



51
52
53
54
55
56
# File 'lib/klimt/clients/gravity_client.rb', line 51

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)
  response.body
end

#partner_locations_count(id:, params: []) ⇒ Object



58
59
60
61
62
63
# File 'lib/klimt/clients/gravity_client.rb', line 58

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: []) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
# File 'lib/klimt/clients/gravity_client.rb', line 65

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)
  response.body
end

#search(term:, params: [], indexes: nil) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/klimt/clients/gravity_client.rb', line 40

def search(term:, params: [], indexes: nil)
  params = parse_params(params)
  params[:term] = term
  params[:indexes] = indexes unless indexes.nil?
  uri = "https://#{@host}/api/v1/match"
  response = Typhoeus.get(uri, headers: headers, params: params, params_encoding: :rack) # encode arrays correctly
  response.body
end