Class: Jet::Client

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

Defined Under Namespace

Classes: Orders

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.



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

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

Instance Method Details

#get_tokenObject



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

def get_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

#ordersObject



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

def orders
  Orders.new(self)
end