Class: Bluepark::Client
- Inherits:
-
Object
show all
- Defined in:
- lib/bluepark/client.rb
Defined Under Namespace
Classes: Orders, Products, Sales, Skus, Taxonomies
Constant Summary
collapse
- STATUS_CODES =
{ 200 => 'success',
304 => 'not_modified',
400 => 'bad_request',
401 => 'unauthorized',
403 => 'forbidden',
404 => 'not_found',
405 => 'method_not_allowed',
406 => 'not_acceptable',
413 => 'too_large_request',
415 => 'unsupported_mediatype',
429 => 'too_many_requests',
500 => 'internal_server_error' }.freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config = {}) ⇒ Client
Returns a new instance of Client.
20
21
22
23
24
|
# File 'lib/bluepark/client.rb', line 20
def initialize(config = {})
@user_name = config[:user_name]
@bluepark_token = config[:bluepark_token]
@bluepark_api_uri = config[:bluepark_api_uri]
end
|
Instance Attribute Details
#bluepark_api_uri ⇒ Object
Returns the value of attribute bluepark_api_uri.
6
7
8
|
# File 'lib/bluepark/client.rb', line 6
def bluepark_api_uri
@bluepark_api_uri
end
|
#bluepark_token ⇒ Object
Returns the value of attribute bluepark_token.
6
7
8
|
# File 'lib/bluepark/client.rb', line 6
def bluepark_token
@bluepark_token
end
|
#user_name ⇒ Object
Returns the value of attribute user_name.
6
7
8
|
# File 'lib/bluepark/client.rb', line 6
def user_name
@user_name
end
|
Instance Method Details
#api_call_with_token(action, path, options = {}) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/bluepark/client.rb', line 47
def api_call_with_token(action, path, options = {})
= { Authorization: "Basic #{token}" }
.merge!(options[:headers]) if options[:headers].is_a?(Hash)
[:Accept] = 'text/json'
[:"content-type"] = 'application/json'
args = ["#{@bluepark_api_uri}#{path}"]
args << encode_json(options[:body]) unless options[:body].nil?
args <<
begin
response = RestClient.send(action, *args)
decode_status(response)
rescue RestClient::ExceptionWithResponse => e
decode_status(e.response)
end
end
|
#company ⇒ Object
101
102
103
|
# File 'lib/bluepark/client.rb', line 101
def company
rest_get_with_token('company')
end
|
#decode_json(json) ⇒ Object
30
31
32
33
|
# File 'lib/bluepark/client.rb', line 30
def decode_json(json)
return Oj.load(json) if json != ''
[]
end
|
#decode_status(response) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/bluepark/client.rb', line 35
def decode_status(response)
if response.body.nil? || response.body.strip.empty? || response.code >= 300
error = {
status: STATUS_CODES[response.code],
status_code: response.code
}.merge(decode_json(response.body).to_h)
raise BlueparkError, encode_json(error)
else
decode_json(response.body)
end
end
|
#encode_json(data) ⇒ Object
26
27
28
|
# File 'lib/bluepark/client.rb', line 26
def encode_json(data)
Oj.dump(data, mode: :compat)
end
|
#orders ⇒ Object
81
82
83
|
# File 'lib/bluepark/client.rb', line 81
def orders
Orders.new(self)
end
|
#products ⇒ Object
85
86
87
|
# File 'lib/bluepark/client.rb', line 85
def products
Products.new(self)
end
|
#rest_delete_with_token(path, body = {}) ⇒ Object
77
78
79
|
# File 'lib/bluepark/client.rb', line 77
def rest_delete_with_token(path, body = {})
api_call_with_token(:delete, path, body)
end
|
#rest_get_with_token(path, query_params = {}) ⇒ Object
64
65
66
67
|
# File 'lib/bluepark/client.rb', line 64
def rest_get_with_token(path, query_params = {})
= query_params.empty? ? nil : { params: query_params }
api_call_with_token(:get, path, headers: )
end
|
#rest_post_with_token(path, body = {}) ⇒ Object
73
74
75
|
# File 'lib/bluepark/client.rb', line 73
def rest_post_with_token(path, body = {})
api_call_with_token(:post, path, body: body)
end
|
#rest_put_with_token(path, body = {}) ⇒ Object
69
70
71
|
# File 'lib/bluepark/client.rb', line 69
def rest_put_with_token(path, body = {})
api_call_with_token(:put, path, body: body)
end
|
#sales(order_id) ⇒ Object
97
98
99
|
# File 'lib/bluepark/client.rb', line 97
def sales(order_id)
Sales.new(self, order_id)
end
|
#skus ⇒ Object
89
90
91
|
# File 'lib/bluepark/client.rb', line 89
def skus
Skus.new(self)
end
|
#taxonomies ⇒ Object
93
94
95
|
# File 'lib/bluepark/client.rb', line 93
def taxonomies
Taxonomies.new(self)
end
|