Class: Jet::Client
- Inherits:
-
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
|
#files ⇒ Object
72
73
74
|
# File 'lib/jet/client.rb', line 72
def files
Files.new(self)
end
|
#orders ⇒ Object
56
57
58
|
# File 'lib/jet/client.rb', line 56
def orders
Orders.new(self)
end
|
#products ⇒ Object
64
65
66
|
# File 'lib/jet/client.rb', line 64
def products
Products.new(self)
end
|
#refunds ⇒ Object
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 = {})
= token
.merge!({ params: query_params }) unless query_params.empty?
response = RestClient.get("#{API_URL}#{path}", )
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 = {})
= token
response = RestClient.post("#{API_URL}#{path}", body.to_json, )
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 = {})
= token
response = RestClient.put("#{API_URL}#{path}", body.to_json, )
decode_json(response.body) if response.code == 200
end
|
#returns ⇒ Object
60
61
62
|
# File 'lib/jet/client.rb', line 60
def returns
Returns.new(self)
end
|
#taxonomy ⇒ Object
68
69
70
|
# File 'lib/jet/client.rb', line 68
def taxonomy
Taxonomy.new(self)
end
|
#token ⇒ Object
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
|