Class: Jet::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jet/client.rb,
lib/jet/client/files.rb,
lib/jet/client/orders.rb,
lib/jet/client/refunds.rb,
lib/jet/client/returns.rb,
lib/jet/client/products.rb,
lib/jet/client/taxonomy.rb

Overview

Jet API Client

Defined Under Namespace

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, raw_token = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
# File 'lib/jet/client.rb', line 9

def initialize(config = {}, raw_token = {})
  @api_user = config[:api_user]
  @secret = config[:secret]
  @merchant_id = config[:merchant_id]
  @id_token = raw_token[:id_token]
  @token_type = raw_token[:token_type]
  @expires_on = Time.parse(raw_token[:expires_on].to_s) if raw_token[:expires_on]
end

Instance Method Details

#decode_json(json) ⇒ Object



22
23
24
# File 'lib/jet/client.rb', line 22

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

#encode_json(data) ⇒ Object



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

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

#filesObject



100
101
102
# File 'lib/jet/client.rb', line 100

def files
  Files.new(self)
end

#generate_tokenObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jet/client.rb', line 26

def generate_token
  unless token_valid?
    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
  raw_token
end

#ordersObject



84
85
86
# File 'lib/jet/client.rb', line 84

def orders
  Orders.new(self)
end

#productsObject



92
93
94
# File 'lib/jet/client.rb', line 92

def products
  Products.new(self)
end

#raw_tokenObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jet/client.rb', line 43

def raw_token
  if token_valid?
    {
      id_token: @id_token,
      token_type: @token_type,
      expires_on: @expires_on.iso8601
    }
  else
    {}
  end
end

#refundsObject



104
105
106
# File 'lib/jet/client.rb', line 104

def refunds
  Refunds.new(self)
end

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



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

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

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



78
79
80
81
82
# File 'lib/jet/client.rb', line 78

def rest_patch_with_token(path, body = {})
  headers = token
  response = RestClient.patch("#{API_URL}#{path}", encode_json(body), headers)
  decode_json(response.body) if response.code == 200
end

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



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

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

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



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

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

#returnsObject



88
89
90
# File 'lib/jet/client.rb', line 88

def returns
  Returns.new(self)
end

#taxonomyObject



96
97
98
# File 'lib/jet/client.rb', line 96

def taxonomy
  Taxonomy.new(self)
end

#tokenObject



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

def token
  generate_token unless token_valid?
  { Authorization: "#{@token_type} #{@id_token}" }
end

#token_valid?Boolean

Returns:

  • (Boolean)


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

def token_valid?
  @id_token && @token_type && @expires_on > Time.now
end