Class: Workarea::Listrak::DataApi

Inherits:
Object
  • Object
show all
Defined in:
app/services/workarea/listrak/data_api.rb

Defined Under Namespace

Classes: Customers, Orders, Products

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, open_timeout: nil, read_timeout: nil) ⇒ DataApi

Returns a new instance of DataApi.



4
5
6
7
8
9
# File 'app/services/workarea/listrak/data_api.rb', line 4

def initialize(client_id:, client_secret:, open_timeout: nil, read_timeout: nil)
  @client_id = client_id
  @client_secret = client_secret
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Instance Method Details

#customersObject



30
31
32
# File 'app/services/workarea/listrak/data_api.rb', line 30

def customers
  Customers.new self
end

#ordersObject



34
35
36
# File 'app/services/workarea/listrak/data_api.rb', line 34

def orders
  Orders.new self
end

#productsObject



38
39
40
# File 'app/services/workarea/listrak/data_api.rb', line 38

def products
  Products.new self
end

#request(http_request) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/workarea/listrak/data_api.rb', line 11

def request(http_request)
  token = Listrak::Oauth.token(client_id: client_id, client_secret: client_secret)

  http_request.add_field 'Authorization', "Bearer #{token}"
  http_request.add_field 'Content-Type', 'application/json'
  http_request['Accept'] = 'application/json'

  response = http.start do |conn|
    conn.request http_request
  end

  case response
  when ::Net::HTTPSuccess
    response
  else
    raise Listrak::HttpError, response
  end
end