Class: Jet::Client

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

Defined Under Namespace

Classes: Files, Orders, Products, Refunds, Returns, Taxonomy

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

#decode_json(json) ⇒ Object



17
18
19
# File 'lib/jet/client.rb', line 17

def decode_json(json)
  Oj.load(json)
end

#encode_json(data) ⇒ Object



13
14
15
# File 'lib/jet/client.rb', line 13

def encode_json(data)
  Oj.dump(data, mode: :compat)
end

#filesObject



72
73
74
# File 'lib/jet/client.rb', line 72

def files
  Files.new(self)
end

#ordersObject



56
57
58
# File 'lib/jet/client.rb', line 56

def orders
  Orders.new(self)
end

#productsObject



64
65
66
# File 'lib/jet/client.rb', line 64

def products
  Products.new(self)
end

#refundsObject



76
77
78
# File 'lib/jet/client.rb', line 76

def refunds
  Refunds.new(self)
end

#rest_get_with_token(path, query_params = {}) ⇒ Object



37
38
39
40
41
42
# File 'lib/jet/client.rb', line 37

def rest_get_with_token(path, query_params = {})
  headers = token
  headers.merge!({ params: query_params }) unless query_params.empty?
  response = RestClient.get("#{API_URL}#{path}", headers)
  decode_json(response.body) if response.code == 200
end

#rest_post_with_token(path, body = {}) ⇒ Object



50
51
52
53
54
# File 'lib/jet/client.rb', line 50

def rest_post_with_token(path, body = {})
  headers = token
  response = RestClient.post("#{API_URL}#{path}", body.to_json, headers)
  decode_json(response.body) if response.code == 201
end

#rest_put_with_token(path, body = {}) ⇒ Object



44
45
46
47
48
# File 'lib/jet/client.rb', line 44

def rest_put_with_token(path, body = {})
  headers = token
  response = RestClient.put("#{API_URL}#{path}", body.to_json, headers)
  decode_json(response.body) if response.code == 200
end

#returnsObject



60
61
62
# File 'lib/jet/client.rb', line 60

def returns
  Returns.new(self)
end

#taxonomyObject



68
69
70
# File 'lib/jet/client.rb', line 68

def taxonomy
  Taxonomy.new(self)
end

#tokenObject



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

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", encode_json(body))
    parsed_response = decode_json(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