Class: Exponea::BaseApi

Inherits:
Object
  • Object
show all
Defined in:
lib/exponea/base_api.rb

Direct Known Subclasses

Customers, Events

Constant Summary collapse

EXPONEA_URL =
'https://api.exponea.com'.freeze

Class Method Summary collapse

Class Method Details

.batch_commands(commands) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/exponea/base_api.rb', line 21

def self.batch_commands(commands)
  responses = []
  commands.each_slice(50) do |command_batch|
    path = "track/v2/projects/#{Exponea.config.project}/batch"
    payload = { commands: command_batch }
    response = post(path, payload)
    response = post(path, payload) unless response['success'] # retry if exponea failed for some reason

    next unless response['success']

    response['results'].each { |result| responses.push(response['time'] ? result['time'] : result['success']) }
  end
  responses
end

.delete(path, payload) ⇒ Object



44
45
46
# File 'lib/exponea/base_api.rb', line 44

def self.delete(path, payload)
  request(path, 'DELETE', payload)
end

.get(path) ⇒ Object



36
37
38
# File 'lib/exponea/base_api.rb', line 36

def self.get(path)
  request(path, 'GET')
end

.post(path, payload) ⇒ Object



40
41
42
# File 'lib/exponea/base_api.rb', line 40

def self.post(path, payload)
  request(path, 'POST', payload)
end

.put(path, payload) ⇒ Object



48
49
50
# File 'lib/exponea/base_api.rb', line 48

def self.put(path, payload)
  request(path, 'PUT', payload)
end

.request(uri, method = 'POST', payload = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/exponea/base_api.rb', line 5

def self.request(uri, method = 'POST', payload = nil)
  client = Faraday.new(url: EXPONEA_URL) do |faraday|
    faraday.request :json
    faraday.response :json

    faraday.adapter Faraday.default_adapter
  end

  response = client.send(method.downcase.to_sym) do |request|
    request.headers['authorization'] = "Basic #{Exponea.config.token}"
    request.url(uri)
    request.body = payload
  end
  response.body
end