Class: Jet::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jet/client.rb

Defined Under Namespace

Classes: Orders, Returns

Constant Summary collapse

API_URL =
'https://merchant-api.jet.com/api'

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/jet/client.rb', line 7

def initialize(config = {})
  @api_user = config[:api_user]
  @secret = config[:secret]
  @merchant_id = config[:merchant_id]
end

Instance Method Details

#ordersObject



29
30
31
# File 'lib/jet/client.rb', line 29

def orders
  Orders.new(self)
end

#returnsObject



33
34
35
# File 'lib/jet/client.rb', line 33

def returns
  Returns.new(self)
end

#tokenObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jet/client.rb', line 13

def token
  if not (@id_token and @token_type and @expires_on > Time.now)
    body = {
      user: @api_user,
      pass: @secret
    }
    response = RestClient.post("#{API_URL}/token", body.to_json)
    parsed_response = JSON.parse(response.body)
    @id_token = parsed_response['id_token']
    @token_type = parsed_response['token_type']
    @expires_on = Time.parse(parsed_response['expires_on'])
  end

  { Authorization: "#{@token_type} #{@id_token}" }
end