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



33
34
35
36
37
38
39
40
# 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:) ⇒ Object



18
19
20
21
22
23
# 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: []) ⇒ Object



25
26
27
28
29
30
31
# 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: []) ⇒ Object

partners



54
55
56
57
58
59
60
# 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: []) ⇒ Object



62
63
64
65
66
67
# 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: []) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
# 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

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



42
43
44
45
46
47
48
49
50
# File 'lib/klimt/clients/gravity_client.rb', line 42

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
  raise response.body unless response.success?
  response.body
end