hubrise_client 2.0.2 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7763aa9e092db7309780e3217dc381191e49141d8120a3dfd38791ff4af47e9c
4
- data.tar.gz: 3e8288142e201c52adf9ca05b3607f0f37c8a6ffad4a2df6c8c271395e226fff
3
+ metadata.gz: 0d034f11701442126bbfd68d2adf7dc0979e38299c1e8947fcc66ced1b5d7381
4
+ data.tar.gz: 1da665c2e8f419fa7d4b630a778ac1497966b7dbbe838e24c8b2614b615348cd
5
5
  SHA512:
6
- metadata.gz: f683839ae7f97389a8ff392d386587f6c47df0ab1b68da3c733dfd35516be74bdb20d51da1770b15b83bc08eee9bc234c09410bb845f8729f842996d9d3641e7
7
- data.tar.gz: fcefcc27355f4df841b4624506dafc25d2abd0399ef14779bb72b3c39d25ed27a922f4cd1a4d415533659104c6c4f27585b9f35a0a5266a537e8fa11d5e1ef44
6
+ metadata.gz: 65c1b26b077c0719143dfce199aca471ced0551439594f7233188e11eb413127f770a37a7250aeec4e45dfd7db8f65579edbd40d3a52da89ea509f3f19f9c174
7
+ data.tar.gz: c897012884c98b5cfd901c277543794b9e42e45cb8188d7c2adcba2100713646554a3e4e85bf512eea7777187621e9b692b0138bdbd4ad3cd02c224a4fbe0b0e
data/README.md CHANGED
@@ -1,12 +1,98 @@
1
- # hubrise_client
1
+ This gem is a Ruby SDK for [Hubrise API](https://www.hubrise.com/developers). It is maintained by the HubRise development team.
2
2
 
3
- To upload the latest version to RubyGems.org:
3
+ # Installation
4
4
 
5
- 1. Increase version in `lib/hubrise_client/version.rb` and commit
5
+ Add `hubrise_client` to your `Gemfile`:
6
6
 
7
- 2. Build & publish (assuming the new version is `2.0.1`):
7
+ ```ruby
8
+ gem 'hubrise_client'
9
+ ```
10
+
11
+ Or install via [gem](http://rubygems.org/)
8
12
 
9
13
  ```bash
10
- gem build hubrise_client
11
- gem push hubrise_client-2.0.1.gem
12
- ```
14
+ gem install hubrise_client
15
+ ```
16
+
17
+
18
+ # Prerequisites
19
+
20
+ 1) Read the [docs](https://www.hubrise.com/developers)
21
+
22
+ 2) [Create a Hubrise Client](https://www.hubrise.com/developers/quick-start#create-the-oauth-client) to get your `CLIENT_ID` and `CLIENT_SECRET`.
23
+
24
+
25
+ # Get an access token
26
+
27
+ 1. Initialize anonymous `HubriseClient`
28
+ ```ruby
29
+ client = HubriseClient::V1.new(CLIENT_ID, CLIENT_SECRET)
30
+ ```
31
+
32
+ 2. Prepare a route to handle `REDIRECT_URI` callback
33
+ ```ruby
34
+ # routes.rb
35
+ namespace :hubrise_oauth do
36
+ get :authorize_callback
37
+ # => creates hubrise_oauth_authorize_callback_url helper
38
+ end
39
+ ```
40
+
41
+
42
+ 3. Initiate an OAuth flow by redirecting a user to [OAuth Authorization URL](https://www.hubrise.com/developers/authentication#request-authorisation)
43
+ ```ruby
44
+ oauth_scope = "location[orders.read]" # adjust to your needs
45
+ authorization_url = client.build_authorization_url(hubrise_oauth_authorize_callback_url, oauth_scope)
46
+ ...
47
+ redirect_to(authorization_url)
48
+ ```
49
+
50
+ 4. Complete the OAuth flow
51
+
52
+ Once the user accepts your request he or she will be redirected to the REDIRECT_URI
53
+ At this step you need to [exchange](https://www.hubrise.com/developers/authentication#get-an-access-token) the `authorization_code` (received as a query param) for a new `access_token`
54
+
55
+
56
+ ```ruby
57
+ # controllers/hubrise_oauth_controller.rb
58
+
59
+ def authorize_callback
60
+ client.authorize!(params[:code])
61
+
62
+ current_user.update!(hubrise_access_token: client.access_token)
63
+ # account_id = client.account_id
64
+ # location_id = client.location_id
65
+ end
66
+ ```
67
+
68
+ The `access_token` reffers to this specific user's permission so it should be securely persisted and not shared with other users.
69
+
70
+
71
+ # Use the access token
72
+
73
+ ```ruby
74
+ client = HubriseClient::V1.new(CLIENT_ID, CLIENT_SECRET, access_token: current_user.hubrise_access_token)
75
+ ```
76
+
77
+ Now you can call the [helper methods](https://github.com/HubRise/ruby-client/blob/master/V1_ENDPOINTS.md) on behalf of the user
78
+ ```ruby
79
+ client.get_account
80
+ ```
81
+
82
+ ## Pagination
83
+
84
+ `#next_page?`, `#next_page` and `#each_page` might be helpful when working with paginated [endpoints](https://www.hubrise.com/developers/api/general-concepts/#pagination)
85
+
86
+ ```ruby
87
+ response = client.get_all_customers
88
+
89
+ response.next_page?
90
+ # => true
91
+
92
+ response.next_page.data
93
+ # => [{ first_name:... }]
94
+
95
+ response.each_page do |page_response|
96
+ page_response.data
97
+ # => [{ first_name:... }]
98
+ end
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "rake"
2
3
  require "rake/testtask"
3
4
 
@@ -7,4 +8,4 @@ Rake::TestTask.new do |t|
7
8
  t.verbose = false
8
9
  end
9
10
 
10
- task default: :test
11
+ task(default: :test)
data/V1_ENDPOINTS.md CHANGED
@@ -1,256 +1,403 @@
1
+ # Full list of available methods to access the API
2
+
3
+ ```ruby
4
+ client = HubriseClient::V1.new(CLIENT_ID, CLIENT_SECRET, client_attrs)
5
+ ```
6
+
1
7
  ### GET_ACCOUNT
2
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
3
- ```
4
- Hubrise::APIClients::V1.new(client_attrs).get_account("zrn61")
5
- // => [GET] /accounts/zrn61 with {:headers=>{"X-Access-Token"=>"access_token1"}}
6
- ```
7
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
8
- ```
9
- Hubrise::APIClients::V1.new(client_attrs).get_account()
10
- // => [GET] /account with {:headers=>{"X-Access-Token"=>"access_token1"}}
11
- ```
8
+
9
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
10
+ ```ruby
11
+ client.get_account("zrn61")
12
+ # [GET] /accounts/zrn61 with { headers: { "X-Access-Token": "access_token1" }}
13
+ ```
14
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
15
+ ```ruby
16
+ client.get_account
17
+ # [GET] /account with { headers: { "X-Access-Token": "access_token1" }}
18
+ ```
19
+
12
20
  ### GET_ACCOUNTS
13
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
14
- ```
15
- Hubrise::APIClients::V1.new(client_attrs).get_accounts()
16
- // => [GET] /accounts with {:headers=>{"X-Access-Token"=>"access_token1"}}
17
- ```
21
+
22
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
23
+ ```ruby
24
+ client.get_accounts
25
+ # [GET] /accounts with { headers: { "X-Access-Token": "access_token1" }}
26
+ ```
27
+
18
28
  ### GET_USER
19
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
20
- ```
21
- Hubrise::APIClients::V1.new(client_attrs).get_user()
22
- // => [GET] /user with {:headers=>{"X-Access-Token"=>"access_token1"}}
23
- ```
29
+
30
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
31
+ ```ruby
32
+ client.get_user
33
+ # [GET] /user with { headers: { "X-Access-Token": "access_token1" }}
34
+ ```
35
+
24
36
  ### GET_LOCATIONS
25
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
26
- ```
27
- Hubrise::APIClients::V1.new(client_attrs).get_locations()
28
- // => [GET] /locations with {:headers=>{"X-Access-Token"=>"access_token1"}}
29
- ```
37
+
38
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
39
+ ```ruby
40
+ client.get_locations
41
+ # [GET] /locations with { headers: { "X-Access-Token": "access_token1" }}
42
+ ```
43
+
30
44
  ### GET_LOCATION
31
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
32
- ```
33
- Hubrise::APIClients::V1.new(client_attrs).get_location("zrn61")
34
- // => [GET] /locations/zrn61 with {:headers=>{"X-Access-Token"=>"access_token1"}}
35
- ```
36
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
37
- ```
38
- Hubrise::APIClients::V1.new(client_attrs).get_location()
39
- // => [GET] /location with {:headers=>{"X-Access-Token"=>"access_token1"}}
40
- ```
45
+
46
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
47
+ ```ruby
48
+ client.get_location("zrn61")
49
+ # [GET] /locations/zrn61 with { headers: { "X-Access-Token": "access_token1" }}
50
+ ```
51
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
52
+ ```ruby
53
+ client.get_location
54
+ # [GET] /location with { headers: { "X-Access-Token": "access_token1" }}
55
+ ```
56
+
41
57
  ### GET_ORDERS
42
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
43
- ```
44
- Hubrise::APIClients::V1.new(client_attrs).get_orders("zrn61", {:status=>"new"})
45
- // => [GET] /locations/zrn61/orders?status=new with {:headers=>{"X-Access-Token"=>"access_token1"}}
46
- ```
58
+
59
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
60
+ ```ruby
61
+ client.get_orders("zrn61", { status: "new" })
62
+ # [GET] /locations/zrn61/orders?status=new with { headers: { "X-Access-Token": "access_token1" }}
63
+ ```
64
+
47
65
  ### GET_ORDER
48
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
49
- ```
50
- Hubrise::APIClients::V1.new(client_attrs).get_order("zrn61", "wy3xz")
51
- // => [GET] /locations/zrn61/orders/wy3xz with {:headers=>{"X-Access-Token"=>"access_token1"}}
52
- ```
66
+
67
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
68
+ ```ruby
69
+ client.get_order("zrn61", "wy3xz")
70
+ # [GET] /locations/zrn61/orders/wy3xz with { headers: { "X-Access-Token": "access_token1" }}
71
+ ```
72
+
53
73
  ### CREATE_ORDER
54
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
55
- ```
56
- Hubrise::APIClients::V1.new(client_attrs).create_order("zrn61", {:status=>"new"})
57
- // => [POST] /locations/zrn61/orders with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"status\":\"new\"}"}
58
- ```
74
+
75
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
76
+ ```ruby
77
+ client.create_order("zrn61", { status: "new" })
78
+ # [POST] /locations/zrn61/orders with { headers: { "Content-Type": "application/json" }, body: "{\"status\":\"new\" }" }
79
+ ```
80
+
59
81
  ### UPDATE_ORDER
60
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
61
- ```
62
- Hubrise::APIClients::V1.new(client_attrs).update_order("zrn61", "wy3xz", {:status=>"delivered"})
63
- // => [PUT] /locations/zrn61/orders/wy3xz with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"status\":\"delivered\"}"}
64
- ```
82
+
83
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
84
+ ```ruby
85
+ client.update_order("zrn61", "wy3xz", { status: "delivered" })
86
+ # [PUT] /locations/zrn61/orders/wy3xz with { headers: { "Content-Type": "application/json" }, body: "{\"status\":\"delivered\" }" }
87
+ ```
88
+
65
89
  ### GET_CALLBACK
66
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
67
- ```
68
- Hubrise::APIClients::V1.new(client_attrs).get_callback()
69
- // => [GET] /callback with {:headers=>{"X-Access-Token"=>"access_token1"}}
70
- ```
90
+
91
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
92
+ ```ruby
93
+ client.get_callback
94
+ # [GET] /callback with { headers: { "X-Access-Token": "access_token1" }}
95
+ ```
96
+
71
97
  ### GET_CALLBACK_EVENTS
72
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
73
- ```
74
- Hubrise::APIClients::V1.new(client_attrs).get_callback_events()
75
- // => [GET] /callback/events with {:headers=>{"X-Access-Token"=>"access_token1"}}
76
- ```
98
+
99
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
100
+ ```ruby
101
+ client.get_callback_events
102
+ # [GET] /callback/events with { headers: { "X-Access-Token": "access_token1" }}
103
+ ```
104
+
77
105
  ### DELETE_EVENT
78
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
79
- ```
80
- Hubrise::APIClients::V1.new(client_attrs).delete_event("zrn61")
81
- // => [DELETE] /callback/events/zrn61 with {:headers=>{"X-Access-Token"=>"access_token1"}}
82
- ```
106
+
107
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
108
+ ```ruby
109
+ client.delete_event("zrn61")
110
+ # [DELETE] /callback/events/zrn61 with { headers: { "X-Access-Token": "access_token1" }}
111
+ ```
112
+
83
113
  ### UPDATE_CALLBACK
84
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
85
- ```
86
- Hubrise::APIClients::V1.new(client_attrs).update_callback({"order"=>["create"]})
87
- // => [POST] /callback with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"order\":[\"create\"]}"}
88
- ```
114
+
115
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
116
+ ```ruby
117
+ client.update_callback({ "order": ["create"]})
118
+ # [POST] /callback with { headers: { "Content-Type": "application/json" }, body: "{\"order\":[\"create\"]}" }
119
+ ```
120
+
89
121
  ### DELETE_CALLBACK
90
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
91
- ```
92
- Hubrise::APIClients::V1.new(client_attrs).delete_callback()
93
- // => [DELETE] /callback with {:headers=>{"X-Access-Token"=>"access_token1"}}
94
- ```
122
+
123
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
124
+ ```ruby
125
+ client.delete_callback
126
+ # [DELETE] /callback with { headers: { "X-Access-Token": "access_token1" }}
127
+ ```
128
+
95
129
  ### GET_LOCATION_CUSTOMER_LISTS
96
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
97
- ```
98
- Hubrise::APIClients::V1.new(client_attrs).get_location_customer_lists("zrn61")
99
- // => [GET] /locations/zrn61/customer_lists with {:headers=>{"X-Access-Token"=>"access_token1"}}
100
- ```
130
+
131
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
132
+ ```ruby
133
+ client.get_location_customer_lists("zrn61")
134
+ # [GET] /locations/zrn61/customer_lists with { headers: { "X-Access-Token": "access_token1" }}
135
+ ```
136
+
101
137
  ### GET_ACCOUNT_CUSTOMER_LISTS
102
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
103
- ```
104
- Hubrise::APIClients::V1.new(client_attrs).get_account_customer_lists("zrn61")
105
- // => [GET] /accounts/zrn61/customer_lists with {:headers=>{"X-Access-Token"=>"access_token1"}}
106
- ```
138
+
139
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
140
+ ```ruby
141
+ client.get_account_customer_lists("zrn61")
142
+ # [GET] /accounts/zrn61/customer_lists with { headers: { "X-Access-Token": "access_token1" }}
143
+ ```
144
+
107
145
  ### GET_CUSTOMER_LIST
108
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
109
- ```
110
- Hubrise::APIClients::V1.new(client_attrs).get_customer_list("zrn61")
111
- // => [GET] /customer_lists/zrn61 with {:headers=>{"X-Access-Token"=>"access_token1"}}
112
- ```
113
- - Initialized with `client_attrs = {:access_token=>"access_token1", :customer_list_id=>"wy3xz"}`
114
- ```
115
- Hubrise::APIClients::V1.new(client_attrs).get_customer_list()
116
- // => [GET] /customer_lists/wy3xz with {:headers=>{"X-Access-Token"=>"access_token1"}}
117
- ```
146
+
147
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
148
+ ```ruby
149
+ client.get_customer_list("zrn61")
150
+ # [GET] /customer_lists/zrn61 with { headers: { "X-Access-Token": "access_token1" }}
151
+ ```
152
+ - Initialized with `client_attrs = { access_token: "access_token1", customer_list_id: "wy3xz" }`
153
+ ```ruby
154
+ client.get_customer_list
155
+ # [GET] /customer_lists/wy3xz with { headers: { "X-Access-Token": "access_token1" }}
156
+ ```
157
+
118
158
  ### GET_ALL_CUSTOMERS
119
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
120
- ```
121
- Hubrise::APIClients::V1.new(client_attrs).get_all_customers("zrn61")
122
- // => [GET] /customer_lists/zrn61/customers with {:headers=>{"X-Access-Token"=>"access_token1"}}
123
- ```
124
- - Initialized with `client_attrs = {:access_token=>"access_token1", :customer_list_id=>"wy3xz"}`
125
- ```
126
- Hubrise::APIClients::V1.new(client_attrs).get_all_customers()
127
- // => [GET] /customer_lists/wy3xz/customers with {:headers=>{"X-Access-Token"=>"access_token1"}}
128
- ```
159
+
160
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
161
+ ```ruby
162
+ client.get_all_customers("zrn61")
163
+ # [GET] /customer_lists/zrn61/customers with { headers: { "X-Access-Token": "access_token1" }}
164
+ ```
165
+ - Initialized with `client_attrs = { access_token: "access_token1", customer_list_id: "wy3xz" }`
166
+ ```ruby
167
+ client.get_all_customers
168
+ # [GET] /customer_lists/wy3xz/customers with { headers: { "X-Access-Token": "access_token1" }}
169
+ ```
170
+
129
171
  ### SEARCH_CUSTOMERS
130
- - Initialized with `client_attrs = {:access_token=>"access_token1", :customer_list_id=>"zrn61"}`
131
- ```
132
- Hubrise::APIClients::V1.new(client_attrs).search_customers({:email=>"nsave@*"})
133
- // => [GET] /customer_lists/zrn61/customers?email=nsave@* with {:headers=>{"X-Access-Token"=>"access_token1"}}
134
- ```
135
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
136
- ```
137
- Hubrise::APIClients::V1.new(client_attrs).search_customers({:email=>"nsave@*"}, "wy3xz")
138
- // => [GET] /customer_lists/wy3xz/customers?email=nsave@* with {:headers=>{"X-Access-Token"=>"access_token1"}}
139
- ```
172
+
173
+ - Initialized with `client_attrs = { access_token: "access_token1", customer_list_id: "zrn61" }`
174
+ ```ruby
175
+ client.search_customers({ email: "nsave@*" })
176
+ # [GET] /customer_lists/zrn61/customers?email=nsave@* with { headers: { "X-Access-Token": "access_token1" }}
177
+ ```
178
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
179
+ ```ruby
180
+ client.search_customers({ email: "nsave@*" }, "wy3xz")
181
+ # [GET] /customer_lists/wy3xz/customers?email=nsave@* with { headers: { "X-Access-Token": "access_token1" }}
182
+ ```
183
+
140
184
  ### GET_CUSTOMER
141
- - Initialized with `client_attrs = {:access_token=>"access_token1", :customer_list_id=>"zrn61"}`
142
- ```
143
- Hubrise::APIClients::V1.new(client_attrs).get_customer("zrk6b")
144
- // => [GET] /customer_lists/zrn61/customers/zrk6b with {:headers=>{"X-Access-Token"=>"access_token1"}}
145
- ```
146
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
147
- ```
148
- Hubrise::APIClients::V1.new(client_attrs).get_customer("zrk6b", "wy3xz")
149
- // => [GET] /customer_lists/wy3xz/customers/zrk6b with {:headers=>{"X-Access-Token"=>"access_token1"}}
150
- ```
185
+
186
+ - Initialized with `client_attrs = { access_token: "access_token1", customer_list_id: "zrn61" }`
187
+ ```ruby
188
+ client.get_customer("zrk6b")
189
+ # [GET] /customer_lists/zrn61/customers/zrk6b with { headers: { "X-Access-Token": "access_token1" }}
190
+ ```
191
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
192
+ ```ruby
193
+ client.get_customer("zrk6b", "wy3xz")
194
+ # [GET] /customer_lists/wy3xz/customers/zrk6b with { headers: { "X-Access-Token": "access_token1" }}
195
+ ```
196
+
151
197
  ### CREATE_CUSTOMER
152
- - Initialized with `client_attrs = {:access_token=>"access_token1", :customer_list_id=>"zrn61"}`
153
- ```
154
- Hubrise::APIClients::V1.new(client_attrs).create_customer({:first_name=>"nsave"})
155
- // => [POST] /customer_lists/zrn61/customers with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"first_name\":\"nsave\"}"}
156
- ```
157
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
158
- ```
159
- Hubrise::APIClients::V1.new(client_attrs).create_customer({:first_name=>"nsave"}, "wy3xz")
160
- // => [POST] /customer_lists/wy3xz/customers with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"first_name\":\"nsave\"}"}
161
- ```
198
+
199
+ - Initialized with `client_attrs = { access_token: "access_token1", customer_list_id: "zrn61" }`
200
+ ```ruby
201
+ client.create_customer({ first_name: "nsave" })
202
+ # [POST] /customer_lists/zrn61/customers with { headers: { "Content-Type": "application/json" }, body: "{\"first_name\":\"nsave\" }" }
203
+ ```
204
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
205
+ ```ruby
206
+ client.create_customer({ first_name: "nsave" }, "wy3xz")
207
+ # [POST] /customer_lists/wy3xz/customers with { headers: { "Content-Type": "application/json" }, body: "{\"first_name\":\"nsave\" }" }
208
+ ```
209
+
162
210
  ### UPDATE_CUSTOMER
163
- - Initialized with `client_attrs = {:access_token=>"access_token1", :customer_list_id=>"zrn61"}`
164
- ```
165
- Hubrise::APIClients::V1.new(client_attrs).update_customer("zrk6b", {:first_name=>"nsave"})
166
- // => [PUT] /customer_lists/zrn61/customers/zrk6b with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"first_name\":\"nsave\"}"}
167
- ```
168
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
169
- ```
170
- Hubrise::APIClients::V1.new(client_attrs).update_customer("zrk6b", {:first_name=>"nsave"}, "wy3xz")
171
- // => [PUT] /customer_lists/wy3xz/customers/zrk6b with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"first_name\":\"nsave\"}"}
172
- ```
211
+
212
+ - Initialized with `client_attrs = { access_token: "access_token1", customer_list_id: "zrn61" }`
213
+ ```ruby
214
+ client.update_customer("zrk6b", { first_name: "nsave" })
215
+ # [PUT] /customer_lists/zrn61/customers/zrk6b with { headers: { "Content-Type": "application/json" }, body: "{\"first_name\":\"nsave\" }" }
216
+ ```
217
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
218
+ ```ruby
219
+ client.update_customer("zrk6b", { first_name: "nsave" }, "wy3xz")
220
+ # [PUT] /customer_lists/wy3xz/customers/zrk6b with { headers: { "Content-Type": "application/json" }, body: "{\"first_name\":\"nsave\" }" }
221
+ ```
222
+
173
223
  ### GET_LOCATION_CATALOGS
174
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
175
- ```
176
- Hubrise::APIClients::V1.new(client_attrs).get_location_catalogs("zrn61")
177
- // => [GET] /locations/zrn61/catalogs with {:headers=>{"X-Access-Token"=>"access_token1"}}
178
- ```
224
+
225
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
226
+ ```ruby
227
+ client.get_location_catalogs("zrn61")
228
+ # [GET] /locations/zrn61/catalogs with { headers: { "X-Access-Token": "access_token1" }}
229
+ ```
230
+
179
231
  ### GET_ACCOUNT_CATALOGS
180
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
181
- ```
182
- Hubrise::APIClients::V1.new(client_attrs).get_account_catalogs("zrn61")
183
- // => [GET] /accounts/zrn61/catalogs with {:headers=>{"X-Access-Token"=>"access_token1"}}
184
- ```
232
+
233
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
234
+ ```ruby
235
+ client.get_account_catalogs("zrn61")
236
+ # [GET] /accounts/zrn61/catalogs with { headers: { "X-Access-Token": "access_token1" }}
237
+ ```
238
+
185
239
  ### GET_CATALOG
186
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
187
- ```
188
- Hubrise::APIClients::V1.new(client_attrs).get_catalog("zrn61")
189
- // => [GET] /catalogs/zrn61 with {:headers=>{"X-Access-Token"=>"access_token1"}}
190
- ```
191
- - Initialized with `client_attrs = {:access_token=>"access_token1", :catalog_id=>"wy3xz"}`
192
- ```
193
- Hubrise::APIClients::V1.new(client_attrs).get_catalog()
194
- // => [GET] /catalogs/wy3xz with {:headers=>{"X-Access-Token"=>"access_token1"}}
195
- ```
240
+
241
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
242
+ ```ruby
243
+ client.get_catalog("zrn61")
244
+ # [GET] /catalogs/zrn61 with { headers: { "X-Access-Token": "access_token1" }}
245
+ ```
246
+ - Initialized with `client_attrs = { access_token: "access_token1", catalog_id: "wy3xz" }`
247
+ ```ruby
248
+ client.get_catalog
249
+ # [GET] /catalogs/wy3xz with { headers: { "X-Access-Token": "access_token1" }}
250
+ ```
251
+
196
252
  ### CREATE_ACCOUNT_CATALOG
197
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
198
- ```
199
- Hubrise::APIClients::V1.new(client_attrs).create_account_catalog({:name=>"Catalog1"})
200
- // => [POST] /account/catalogs with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"name\":\"Catalog1\"}"}
201
- ```
253
+
254
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
255
+ ```ruby
256
+ client.create_account_catalog({ name: "Catalog1" })
257
+ # [POST] /account/catalogs with { headers: { "Content-Type": "application/json" }, body: "{\"name\":\"Catalog1\" }" }
258
+ ```
259
+
202
260
  ### CREATE_LOCATION_CATALOG
203
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
204
- ```
205
- Hubrise::APIClients::V1.new(client_attrs).create_location_catalog({:name=>"Catalog1"}, "zrn61")
206
- // => [POST] /locations/zrn61/catalogs with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"name\":\"Catalog1\"}"}
207
- ```
208
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
209
- ```
210
- Hubrise::APIClients::V1.new(client_attrs).create_location_catalog({:name=>"Catalog1"})
211
- // => [POST] /location/catalogs with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"name\":\"Catalog1\"}"}
212
- ```
261
+
262
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
263
+ ```ruby
264
+ client.create_location_catalog({ name: "Catalog1" }, "zrn61")
265
+ # [POST] /locations/zrn61/catalogs with { headers: { "Content-Type": "application/json" }, body: "{\"name\":\"Catalog1\" }" }
266
+ ```
267
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
268
+ ```ruby
269
+ client.create_location_catalog({ name: "Catalog1" })
270
+ # [POST] /location/catalogs with { headers: { "Content-Type": "application/json" }, body: "{\"name\":\"Catalog1\" }" }
271
+ ```
272
+
213
273
  ### UPDATE_CATALOG
214
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
215
- ```
216
- Hubrise::APIClients::V1.new(client_attrs).update_catalog({:name=>"Catalog1"}, "zrn61")
217
- // => [PUT] /catalogs/zrn61 with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"name\":\"Catalog1\"}"}
218
- ```
219
- - Initialized with `client_attrs = {:access_token=>"access_token1", :catalog_id=>"zrk6b"}`
220
- ```
221
- Hubrise::APIClients::V1.new(client_attrs).update_catalog({:name=>"Catalog1"})
222
- // => [PUT] /catalogs/zrk6b with {:headers=>{"Content-Type"=>"application/json"}, :body=>"{\"name\":\"Catalog1\"}"}
223
- ```
274
+
275
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
276
+ ```ruby
277
+ client.update_catalog({ name: "Catalog1" }, "zrn61")
278
+ # [PUT] /catalogs/zrn61 with { headers: { "Content-Type": "application/json" }, body: "{\"name\":\"Catalog1\" }" }
279
+ ```
280
+ - Initialized with `client_attrs = { access_token: "access_token1", catalog_id: "zrk6b" }`
281
+ ```ruby
282
+ client.update_catalog({ name: "Catalog1" })
283
+ # [PUT] /catalogs/zrk6b with { headers: { "Content-Type": "application/json" }, body: "{\"name\":\"Catalog1\" }" }
284
+ ```
285
+
224
286
  ### CREATE_IMAGE
225
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
226
- ```
227
- Hubrise::APIClients::V1.new(client_attrs).create_image("bin1", "image/png", "zrn61")
228
- // => [POST] /catalogs/zrn61/images with {:headers=>{"Content-Type"=>"image/png"}, :body=>"bin1"}
229
- ```
230
- - Initialized with `client_attrs = {:access_token=>"access_token1", :catalog_id=>"zrk6b"}`
231
- ```
232
- Hubrise::APIClients::V1.new(client_attrs).create_image("bin1", "image/png")
233
- // => [POST] /catalogs/zrk6b/images with {:headers=>{"Content-Type"=>"image/png"}, :body=>"bin1"}
234
- ```
287
+
288
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
289
+ ```ruby
290
+ client.create_image("bin1", "image/png", "zrn61")
291
+ # [POST] /catalogs/zrn61/images with { headers: { "Content-Type": "image/png" }, body: "bin1" }
292
+ ```
293
+ - Initialized with `client_attrs = { access_token: "access_token1", catalog_id: "zrk6b" }`
294
+ ```ruby
295
+ client.create_image("bin1", "image/png")
296
+ # [POST] /catalogs/zrk6b/images with { headers: { "Content-Type": "image/png" }, body: "bin1" }
297
+ ```
298
+
235
299
  ### GET_IMAGE
236
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
237
- ```
238
- Hubrise::APIClients::V1.new(client_attrs).get_image("zrn61", "wy3xz")
239
- // => [GET] /catalogs/wy3xz/images/zrn61 with {:headers=>{"X-Access-Token"=>"access_token1"}}
240
- ```
241
- - Initialized with `client_attrs = {:access_token=>"access_token1", :catalog_id=>"zrk6b"}`
242
- ```
243
- Hubrise::APIClients::V1.new(client_attrs).get_image("zrn61")
244
- // => [GET] /catalogs/zrk6b/images/zrn61 with {:headers=>{"X-Access-Token"=>"access_token1"}}
245
- ```
300
+
301
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
302
+ ```ruby
303
+ client.get_image("zrn61", "wy3xz")
304
+ # [GET] /catalogs/wy3xz/images/zrn61 with { headers: { "X-Access-Token": "access_token1" }}
305
+ ```
306
+ - Initialized with `client_attrs = { access_token: "access_token1", catalog_id: "zrk6b" }`
307
+ ```ruby
308
+ client.get_image("zrn61")
309
+ # [GET] /catalogs/zrk6b/images/zrn61 with { headers: { "X-Access-Token": "access_token1" }}
310
+ ```
311
+
246
312
  ### GET_IMAGE_DATA
247
- - Initialized with `client_attrs = {:access_token=>"access_token1"}`
248
- ```
249
- Hubrise::APIClients::V1.new(client_attrs).get_image_data("zrn61", "wy3xz")
250
- // => [GET] /catalogs/wy3xz/images/zrn61/data with {:headers=>{"X-Access-Token"=>"access_token1"}}
251
- ```
252
- - Initialized with `client_attrs = {:access_token=>"access_token1", :catalog_id=>"zrk6b"}`
253
- ```
254
- Hubrise::APIClients::V1.new(client_attrs).get_image_data("zrn61")
255
- // => [GET] /catalogs/zrk6b/images/zrn61/data with {:headers=>{"X-Access-Token"=>"access_token1"}}
256
- ```
313
+
314
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
315
+ ```ruby
316
+ client.get_image_data("zrn61", "wy3xz")
317
+ # [GET] /catalogs/wy3xz/images/zrn61/data with { headers: { "X-Access-Token": "access_token1" }}
318
+ ```
319
+ - Initialized with `client_attrs = { access_token: "access_token1", catalog_id: "zrk6b" }`
320
+ ```ruby
321
+ client.get_image_data("zrn61")
322
+ # [GET] /catalogs/zrk6b/images/zrn61/data with { headers: { "X-Access-Token": "access_token1" }}
323
+ ```
324
+
325
+ ### GET_INVENTORY
326
+
327
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
328
+ ```ruby
329
+ client.get_inventory("zrn61", "wy3xz")
330
+ # [GET] /catalogs/zrn61/locations/wy3xz/inventory with { headers: { "X-Access-Token": "access_token1" }}
331
+ ```
332
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
333
+ ```ruby
334
+ client.get_inventory("zrn61")
335
+ # [GET] /catalogs/zrn61/location/inventory with { headers: { "X-Access-Token": "access_token1" }}
336
+ ```
337
+ - Initialized with `client_attrs = { access_token: "access_token1", catalog_id: "zrn61" }`
338
+ ```ruby
339
+ client.get_inventory
340
+ # [GET] /catalogs/zrn61/location/inventory with { headers: { "X-Access-Token": "access_token1" }}
341
+ ```
342
+
343
+ ### UPDATE_INVENTORY
344
+
345
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
346
+ ```ruby
347
+ client.update_inventory([{sku_ref: "m9qqs"}], "zrn61", "wy3xz")
348
+ # [PUT] /catalogs/zrn61/locations/wy3xz/inventory with { headers: { "X-Access-Token": "access_token1" }, body: "[{\"sku_ref\":\"m9qqs\"}]"}
349
+ ```
350
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
351
+ ```ruby
352
+ client.update_inventory([{sku_ref: "m9qqs"}], "zrn61")
353
+ # [PUT] /catalogs/zrn61/location/inventory with { headers: { "X-Access-Token": "access_token1" }, body: "[{\"sku_ref\":\"m9qqs\"}]"}
354
+ ```
355
+ - Initialized with `client_attrs = { access_token: "access_token1", catalog_id: "zrn61" }`
356
+ ```ruby
357
+ client.update_inventory([{sku_ref: "m9qqs"}])
358
+ # [PUT] /catalogs/zrn61/location/inventory with { headers: { "X-Access-Token": "access_token1" }, body: "[{\"sku_ref\":\"m9qqs\"}]"}
359
+ ```
360
+
361
+ ### PATCH_INVENTORY
362
+
363
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
364
+ ```ruby
365
+ client.patch_inventory([{sku_ref: "m9qqs"}], "zrn61", "wy3xz")
366
+ # [PATCH] /catalogs/zrn61/locations/wy3xz/inventory with { headers: { "X-Access-Token": "access_token1" }, body: "[{\"sku_ref\":\"m9qqs\"}]"}
367
+ ```
368
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
369
+ ```ruby
370
+ client.patch_inventory([{sku_ref: "m9qqs"}], "zrn61")
371
+ # [PATCH] /catalogs/zrn61/location/inventory with { headers: { "X-Access-Token": "access_token1" }, body: "[{\"sku_ref\":\"m9qqs\"}]"}
372
+ ```
373
+ - Initialized with `client_attrs = { access_token: "access_token1", catalog_id: "zrn61" }`
374
+ ```ruby
375
+ client.patch_inventory([{sku_ref: "m9qqs"}])
376
+ # [PATCH] /catalogs/zrn61/location/inventory with { headers: { "X-Access-Token": "access_token1" }, body: "[{\"sku_ref\":\"m9qqs\"}]"}
377
+ ```
378
+
379
+ ### CREATE_LOYALTY_CARD
380
+
381
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
382
+ ```ruby
383
+ client.create_loyalty_card({ name: "bonus" }, "wy3xz")
384
+ # [POST] /customer_lists/wy3xz/loyalty_cards with { headers: { "X-Access-Token": "access_token1" }, body: { name: "bonus" } }
385
+ ```
386
+ - Initialized with `client_attrs = { access_token: "access_token1", customer_list_id: "zrk6b" }`
387
+ ```ruby
388
+ client.create_loyalty_card({ name: "bonus" })
389
+ # [POST] /customer_lists/zrk6b/loyalty_cards with { headers: { "X-Access-Token": "access_token1" }, body: { name: "bonus" } }
390
+ ```
391
+
392
+ ### CREATE_LOYALTY_OPERATION
393
+
394
+ - Initialized with `client_attrs = { access_token: "access_token1" }`
395
+ ```ruby
396
+ client.create_loyalty_operation("zrk6b", { delta: "4.2" }, "wy3xz")
397
+ # [POST] /customer_lists/wy3xz/loyalty_cards/zrk6b/operations with { headers: { "X-Access-Token": "access_token1" }, body: { delta: "4.2" } }
398
+ ```
399
+ - Initialized with `client_attrs = { access_token: "access_token1", customer_list_id: "q2brk" }`
400
+ ```ruby
401
+ client.create_loyalty_operation("zrk6b", { delta: "4.2" })
402
+ # [POST] /customer_lists/q2brk/loyalty_cards/zrk6b/operations with { headers: { "X-Access-Token": "access_token1" }, body: { delta: "4.2" } }
403
+ ```
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
1
2
  module HubriseClient
2
3
  class Base
3
4
  USE_HTTPS = true
4
- DEFAULT_API_HOST = "api.hubrise.com".freeze
5
- DEFAULT_API_PORT = "443".freeze
6
- DEFAULT_OAUTH_HOST = "manager.hubrise.com".freeze
7
- DEFAULT_OAUTH_PORT = "443".freeze
5
+ DEFAULT_API_HOST = "api.hubrise.com"
6
+ DEFAULT_API_PORT = "443"
7
+ DEFAULT_OAUTH_HOST = "manager.hubrise.com"
8
+ DEFAULT_OAUTH_PORT = "443"
8
9
 
9
10
  attr_accessor :access_token,
10
11
  :app_instance_id,
@@ -16,20 +17,20 @@ module HubriseClient
16
17
  :logger,
17
18
  :request_callback
18
19
 
19
- def initialize(app_id, app_secret, params = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
20
- @app_id = app_id
20
+ def initialize(app_id, app_secret, params = {})
21
+ @app_id = app_id
21
22
  @app_secret = app_secret
22
- @api_host = params[:api_host] || DEFAULT_API_HOST
23
- @api_port = params[:api_port] || DEFAULT_API_PORT
23
+ @api_host = params[:api_host] || DEFAULT_API_HOST
24
+ @api_port = params[:api_port] || DEFAULT_API_PORT
24
25
  @oauth_host = params[:oauth_host] || DEFAULT_OAUTH_HOST
25
26
  @oauth_port = params[:oauth_port] || DEFAULT_OAUTH_PORT
26
- @use_https = !!params.fetch(:use_https, USE_HTTPS)
27
+ @use_https = !!params.fetch(:use_https, USE_HTTPS)
27
28
  @request_callback = params[:request_callback]
28
29
 
29
30
  initialize_scope_params(params)
30
31
 
31
32
  @verbous = !!params[:verbous]
32
- unless (@logger = params[:logger]) # rubocop:disable Style/GuardClause
33
+ unless (@logger = params[:logger])
33
34
  @logger = Logger.new(STDOUT)
34
35
  @logger.level = @verbous ? Logger::DEBUG : Logger::WARN
35
36
  end
@@ -46,10 +47,13 @@ module HubriseClient
46
47
  end
47
48
 
48
49
  def authorize!(authorization_code)
49
- api_response = api_request(oauth2_hubrise_hostname_with_version).perform(:post, "/token",
50
- client_id: @app_id,
51
- client_secret: @app_secret,
52
- code: authorization_code)
50
+ api_response = api_request(
51
+ oauth2_hubrise_hostname_with_version, "/token", :post, data: {
52
+ client_id: @app_id,
53
+ client_secret: @app_secret,
54
+ code: authorization_code,
55
+ }
56
+ ).perform
53
57
 
54
58
  case api_response.code
55
59
  when "200"
@@ -65,30 +69,38 @@ module HubriseClient
65
69
 
66
70
  protected
67
71
 
68
- def initialize_scope_params(params) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize, Metrics/LineLength
69
- @access_token = params[:access_token] || params["access_token"]
70
- @app_instance_id = params[:app_instance_id] || params["app_instance_id"]
71
- @user_id = params[:user_id] || params["user_id"]
72
- @account_id = params[:account_id] || params["account_id"]
73
- @location_id = params[:location_id] || params["location_id"]
74
- @catalog_id = params[:catalog_id] || params["catalog_id"]
72
+ def initialize_scope_params(params)
73
+ @access_token = params[:access_token] || params["access_token"]
74
+ @app_instance_id = params[:app_instance_id] || params["app_instance_id"]
75
+ @user_id = params[:user_id] || params["user_id"]
76
+ @account_id = params[:account_id] || params["account_id"]
77
+ @location_id = params[:location_id] || params["location_id"]
78
+ @catalog_id = params[:catalog_id] || params["catalog_id"]
75
79
  @customer_list_id = params[:customer_list_id] || params["customer_list_id"]
76
80
  end
77
81
 
78
82
  def call_api(path, method = :get, data: {}, headers: {}, json: true)
79
83
  raise(HubriseAccessTokenMissing) if @access_token.nil?
80
84
 
81
- api_request("#{@api_host}:#{@api_port}/#{version}", @access_token).perform(method, path, data, json: json,
82
- headers: headers,
83
- &@request_callback)
85
+ api_request(
86
+ "#{@api_host}:#{@api_port}/#{version}", path, method, data: data,
87
+ json: json,
88
+ headers: headers,
89
+ access_token: @access_token,
90
+ callback: @request_callback
91
+ ).perform
84
92
  end
85
93
 
86
- def api_request(hostname, access_token = nil)
87
- Request.new(
88
- hostname: hostname,
89
- access_token: access_token,
90
- use_https: @use_https,
91
- logger: @logger
94
+ def api_request(hostname, path, method, attrs = {})
95
+ Request.from_h(
96
+ {
97
+ hostname: hostname,
98
+ path: path,
99
+ method: method,
100
+ use_https: @use_https,
101
+ logger: @verbous && @logger,
102
+ json: true,
103
+ }.merge(attrs)
92
104
  )
93
105
  end
94
106
 
@@ -1,57 +1,82 @@
1
+ # frozen_string_literal: true
1
2
  module HubriseClient
2
- class Request
3
+ class Request < Struct.new(:hostname,
4
+ :path,
5
+ :method,
6
+ :data,
7
+ :access_token,
8
+ :use_https,
9
+ :logger,
10
+ :json,
11
+ :headers,
12
+ :callback)
13
+
3
14
  REQUESTS_HASH = {
4
15
  get: Net::HTTP::Get,
5
16
  post: Net::HTTP::Post,
6
17
  put: Net::HTTP::Put,
7
- delete: Net::HTTP::Delete
18
+ patch: Net::HTTP::Patch,
19
+ delete: Net::HTTP::Delete,
8
20
  }.freeze
9
21
 
10
- def initialize(hostname:, access_token: nil, use_https: false, logger: nil)
11
- @hostname = hostname
12
- @access_token = access_token
13
- @use_https = use_https
14
- @protocol = use_https ? "https" : "http"
15
- @logger = logger
22
+ def self.from_h(hash)
23
+ new(*hash.values_at(*members))
16
24
  end
17
25
 
18
- def perform(method, path, data, json: true, headers: {}, callback: nil)
19
- uri = URI.parse(@protocol + "://" + @hostname + path)
20
- http_request = build_request(uri, method, data, json: json, headers: headers)
21
- http_response = perform_request(uri, http_request)
26
+ attr_reader :http_request
27
+
28
+ def perform
29
+ @http_request = build_request
22
30
 
23
- case http_response
31
+ @http_response = perform_request(@http_request)
32
+ @response = Response.new(@http_response, self)
33
+
34
+ case @http_response
24
35
  when Net::HTTPUnauthorized
25
36
  raise InvalidHubriseToken
26
37
  else
27
- raise(HubriseError, "Unexpected error") if http_response.code.start_with?("5")
38
+ raise(HubriseError, "Unexpected error") if @http_response.code.start_with?("5")
28
39
 
29
- Response.new(http_response)
40
+ @response
30
41
  end
31
42
  rescue Errno::ECONNREFUSED
32
43
  raise HubriseError, "API is not reachable"
33
44
  ensure
34
- yield(http_request, http_response) if http_request && block_given?
45
+ callback.call(self, @response) if @http_request && callback
46
+ end
47
+
48
+ def next_page_request(new_cursor)
49
+ new_data = data || {}
50
+ new_data[:cursor] = new_cursor
51
+
52
+ Request.from_h(to_h.merge(data: new_data))
35
53
  end
36
54
 
37
55
  protected
38
56
 
39
- def build_request(uri, method, data, json:, headers:)
40
- headers["X-Access-Token"] = @access_token if @access_token
57
+ def protocol
58
+ use_https ? "https" : "http"
59
+ end
41
60
 
42
- if method == :get
43
- if data && data.count > 0
44
- uri = add_params_to_uri(uri, data)
45
- end
61
+ def build_request
62
+ request_uri = URI.parse(protocol + "://" + hostname + path)
63
+
64
+ request_headers = headers || {}
65
+ request_headers["X-Access-Token"] = access_token if access_token
66
+
67
+ request_body_data = nil
46
68
 
47
- data = nil
69
+ if method == :get
70
+ request_uri = add_params_to_uri(request_uri, data) if data && !data.empty?
48
71
  elsif json
49
- headers["Content-Type"] ||= "application/json"
50
- data = data.to_json
72
+ request_headers["Content-Type"] ||= "application/json"
73
+ request_body_data = data.to_json
74
+ else
75
+ request_body_data = data
51
76
  end
52
77
 
53
- REQUESTS_HASH[method].new(uri, headers).tap do |request|
54
- request.body = data
78
+ REQUESTS_HASH[method].new(request_uri, request_headers).tap do |request|
79
+ request.body = request_body_data
55
80
  end
56
81
  end
57
82
 
@@ -60,10 +85,12 @@ module HubriseClient
60
85
  uri
61
86
  end
62
87
 
63
- def perform_request(uri, request)
64
- http = Net::HTTP.new(uri.host, uri.port)
65
- http.use_ssl = @use_https
66
- http.set_debug_output(@logger) if @logger
88
+ def perform_request(request)
89
+ uri = request.uri
90
+
91
+ http = Net::HTTP.new(uri.host, uri.port)
92
+ http.use_ssl = use_https
93
+ http.set_debug_output(logger) if logger
67
94
  http.request(request)
68
95
  end
69
96
  end
@@ -1,15 +1,17 @@
1
+ # frozen_string_literal: true
1
2
  module HubriseClient
2
3
  class Response
3
4
  attr_reader :code, :failed, :data, :error_type, :error_message, :errors, :http_response
4
- alias failed? failed
5
+ alias_method :failed?, :failed
5
6
 
6
- def initialize(http_response)
7
- @http_response = http_response
8
- @code = http_response.code
7
+ def initialize(http_response, request)
8
+ @http_response = http_response
9
+ @request = request
10
+ @code = http_response.code
9
11
 
10
12
  json_body = begin
11
13
  JSON.parse(http_response.body)
12
- rescue StandardError
14
+ rescue JSON::ParserError
13
15
  nil
14
16
  end
15
17
 
@@ -17,9 +19,9 @@ module HubriseClient
17
19
 
18
20
  case http_response
19
21
  when Net::HTTPSuccess
20
- @failed = false
22
+ @failed = false
21
23
  else
22
- @failed = true
24
+ @failed = true
23
25
  if json_body
24
26
  @errors = json_body["errors"]
25
27
  @error_type = json_body["error_type"]
@@ -31,5 +33,29 @@ module HubriseClient
31
33
  def retry_after
32
34
  http_response.is_a?(Net::HTTPTooManyRequests) && http_response["retry-after"].to_i
33
35
  end
36
+
37
+ def each_page
38
+ return enum_for(:each_page) unless block_given?
39
+
40
+ yield(self)
41
+
42
+ response = self
43
+ while response.next_page?
44
+ response = response.next_page
45
+ yield(response)
46
+ end
47
+ end
48
+
49
+ def next_page?
50
+ !!cursor_next
51
+ end
52
+
53
+ def next_page
54
+ @request.next_page_request(cursor_next).perform
55
+ end
56
+
57
+ def cursor_next
58
+ http_response["X-Cursor-Next"]
59
+ end
34
60
  end
35
61
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # rubocop:disable Naming/AccessorMethodName
2
3
  module HubriseClient
3
4
  class V1 < Base
@@ -111,7 +112,7 @@ module HubriseClient
111
112
 
112
113
  def update_customer(customer_id, params, customer_list_id = nil)
113
114
  call_api("/customer_lists/#{customer_list_id_fallback(customer_list_id)}/customers/#{customer_id}", :put,
114
- data: params)
115
+ data: params)
115
116
  end
116
117
 
117
118
  # --------------------
@@ -125,8 +126,9 @@ module HubriseClient
125
126
  call_api("/accounts/#{account_id}/catalogs")
126
127
  end
127
128
 
128
- def get_catalog(catalog_id = nil)
129
- call_api("/catalogs/#{catalog_id_fallback(catalog_id)}")
129
+ def get_catalog(catalog_id = nil, hide_data = false)
130
+ query_params = hide_data ? { hide_data: true } : {}
131
+ call_api("/catalogs/#{catalog_id_fallback(catalog_id)}", :get, data: query_params)
130
132
  end
131
133
 
132
134
  def create_account_catalog(params)
@@ -150,8 +152,8 @@ module HubriseClient
150
152
  # --------------------
151
153
  def create_image(data, mime_type, catalog_id = nil)
152
154
  call_api("/catalogs/#{catalog_id_fallback(catalog_id)}/images", :post, data: data,
153
- headers: { "Content-Type" => mime_type },
154
- json: false)
155
+ headers: { "Content-Type" => mime_type },
156
+ json: false)
155
157
  end
156
158
 
157
159
  def get_image(image_id, catalog_id = nil)
@@ -162,6 +164,49 @@ module HubriseClient
162
164
  call_api("/catalogs/#{catalog_id_fallback(catalog_id)}/images/#{image_id}/data")
163
165
  end
164
166
 
167
+ # --------------------
168
+ # Inventory
169
+ # --------------------
170
+ def get_inventory(catalog_id = nil, location_id = nil)
171
+ endpoint = inventory_endpoint(catalog_id, location_id)
172
+ call_api(endpoint)
173
+ end
174
+
175
+ def update_inventory(params, catalog_id = nil, location_id = nil)
176
+ endpoint = inventory_endpoint(catalog_id, location_id)
177
+ call_api(endpoint, :put, data: params)
178
+ end
179
+
180
+ def patch_inventory(params, catalog_id = nil, location_id = nil)
181
+ endpoint = inventory_endpoint(catalog_id, location_id)
182
+ call_api(endpoint, :patch, data: params)
183
+ end
184
+
185
+ def inventory_endpoint(catalog_id = nil, location_id = nil)
186
+ if location_id
187
+ "/catalogs/#{catalog_id_fallback(catalog_id)}/locations/#{location_id}/inventory"
188
+ else
189
+ "/catalogs/#{catalog_id_fallback(catalog_id)}/location/inventory"
190
+ end
191
+ end
192
+
193
+ private :inventory_endpoint
194
+
195
+ # --------------------
196
+ # Loyalty cards
197
+ # --------------------
198
+ def create_loyalty_card(params, customer_list_id = nil)
199
+ call_api("/customer_lists/#{customer_list_id_fallback(customer_list_id)}/loyalty_cards", :post, data: params)
200
+ end
201
+
202
+ def create_loyalty_operation(loyalty_card_id, params, customer_list_id = nil)
203
+ call_api(
204
+ "/customer_lists/#{customer_list_id_fallback(customer_list_id)}/loyalty_cards/#{loyalty_card_id}/operations",
205
+ :post,
206
+ data: params
207
+ )
208
+ end
209
+
165
210
  private
166
211
 
167
212
  def customer_list_id_fallback(customer_list_id)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module HubriseClient
2
- VERSION = "2.0.2".freeze
3
+ VERSION = "2.0.5"
3
4
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "net/http"
2
3
  require "json"
3
4
  require "uri"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hubrise_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antoine Monnier
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-01-31 00:00:00.000000000 Z
12
+ date: 2022-07-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  requirements: []
105
- rubygems_version: 3.0.3
105
+ rubygems_version: 3.1.2
106
106
  signing_key:
107
107
  specification_version: 4
108
108
  summary: HubRise Ruby client