hubrise_client 2.0.1 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6a54133261ba20f49174cc2a975fccdbfec6b7d7d5d802288e454197cbfb48c
4
- data.tar.gz: 9537533c2c6e1f211662f4f92f861bde82b26132a447421216cb21fbad87ae62
3
+ metadata.gz: 58aebdf2e9ba790c1303eacf547aaaf5ccdf92f360a63195e6a6e9db1e200b42
4
+ data.tar.gz: 3776550d4ef358b832c1e4f02094da4ef352b8fddb5ce2d174ae092c6f6ce504
5
5
  SHA512:
6
- metadata.gz: 5ae23ce6d5418c2311044814906d73280fb414f2a48401d0620584dfa5b1242b149bf41723ef49a2c43a8543b24622dbdd017e8b4aa3113f7ef9c9837f71a09d
7
- data.tar.gz: dbe91900ae7fc4871752775547e6e8cfe4d68841e494872c02c362fc648840ae5809f2472715525cc608f46704961b52d46528e13a0c5738917ab5996f1f9316
6
+ metadata.gz: 38787fe30bacac00daae63823a52d69f10347380276afb7b8e9c87386560aed7d97300c66a9abe29dcc03c3ccb3de43f11b9cfca14b3c2d432fd8426ab8ffe4c
7
+ data.tar.gz: c8304210fb9fcb47b574d131b690f3e3ac11e7be4ce3633e37ef64aa320a3fcf3a1615066789558aa331bd18b5e61c54651d2e2ecbb6fd0fc681e9e6fa777934
data/README.md CHANGED
@@ -1,8 +1,98 @@
1
- # hubrise_ruby_lib
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 publish the latest version to RubyGems.org:
3
+ # Installation
4
4
 
5
- - Increase version in `lib/hubrise_client/version.rb` and commit
5
+ Add `hubrise_client` to your `Gemfile`:
6
6
 
7
- - `gem push`
7
+ ```ruby
8
+ gem 'hubrise_client'
9
+ ```
8
10
 
11
+ Or install via [gem](http://rubygems.org/)
12
+
13
+ ```bash
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,
@@ -13,21 +14,23 @@ module HubriseClient
13
14
  :location_id,
14
15
  :catalog_id,
15
16
  :customer_list_id,
16
- :logger
17
+ :logger,
18
+ :request_callback
17
19
 
18
- def initialize(app_id, app_secret, params = {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
19
- @app_id = app_id
20
+ def initialize(app_id, app_secret, params = {})
21
+ @app_id = app_id
20
22
  @app_secret = app_secret
21
- @api_host = params[:api_host] || DEFAULT_API_HOST
22
- @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
23
25
  @oauth_host = params[:oauth_host] || DEFAULT_OAUTH_HOST
24
26
  @oauth_port = params[:oauth_port] || DEFAULT_OAUTH_PORT
25
- @use_https = !!params.fetch(:use_https, USE_HTTPS)
27
+ @use_https = !!params.fetch(:use_https, USE_HTTPS)
28
+ @request_callback = params[:request_callback]
26
29
 
27
30
  initialize_scope_params(params)
28
31
 
29
32
  @verbous = !!params[:verbous]
30
- unless (@logger = params[:logger]) # rubocop:disable Style/GuardClause
33
+ unless (@logger = params[:logger])
31
34
  @logger = Logger.new(STDOUT)
32
35
  @logger.level = @verbous ? Logger::DEBUG : Logger::WARN
33
36
  end
@@ -44,10 +47,13 @@ module HubriseClient
44
47
  end
45
48
 
46
49
  def authorize!(authorization_code)
47
- api_response = api_request(oauth2_hubrise_hostname_with_version).perform(:post, "/token",
48
- client_id: @app_id,
49
- client_secret: @app_secret,
50
- 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
51
57
 
52
58
  case api_response.code
53
59
  when "200"
@@ -63,29 +69,38 @@ module HubriseClient
63
69
 
64
70
  protected
65
71
 
66
- def initialize_scope_params(params) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize, Metrics/LineLength
67
- @access_token = params[:access_token] || params["access_token"]
68
- @app_instance_id = params[:app_instance_id] || params["app_instance_id"]
69
- @user_id = params[:user_id] || params["user_id"]
70
- @account_id = params[:account_id] || params["account_id"]
71
- @location_id = params[:location_id] || params["location_id"]
72
- @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"]
73
79
  @customer_list_id = params[:customer_list_id] || params["customer_list_id"]
74
80
  end
75
81
 
76
82
  def call_api(path, method = :get, data: {}, headers: {}, json: true)
77
83
  raise(HubriseAccessTokenMissing) if @access_token.nil?
78
84
 
79
- api_request("#{@api_host}:#{@api_port}/#{version}", @access_token).perform(method, path, data, json: json,
80
- headers: headers)
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
81
92
  end
82
93
 
83
- def api_request(hostname, access_token = nil)
84
- Request.new(
85
- hostname: hostname,
86
- access_token: access_token,
87
- use_https: @use_https,
88
- 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)
89
104
  )
90
105
  end
91
106
 
@@ -1,52 +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: {})
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
30
+
31
+ @http_response = perform_request(@http_request)
32
+ @response = Response.new(@http_response, self)
22
33
 
23
- case http_response
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"
44
+ ensure
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))
33
53
  end
34
54
 
35
55
  protected
36
56
 
37
- def build_request(uri, method, data, json:, headers:)
38
- headers["X-Access-Token"] = @access_token if @access_token
57
+ def protocol
58
+ use_https ? "https" : "http"
59
+ end
60
+
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
39
68
 
40
69
  if method == :get
41
- uri = add_params_to_uri(uri, data)
42
- data = nil
70
+ request_uri = add_params_to_uri(request_uri, data) if data && !data.empty?
43
71
  elsif json
44
- headers["Content-Type"] ||= "application/json"
45
- 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
46
76
  end
47
77
 
48
- REQUESTS_HASH[method].new(uri, headers).tap do |request|
49
- request.body = data
78
+ REQUESTS_HASH[method].new(request_uri, request_headers).tap do |request|
79
+ request.body = request_body_data
50
80
  end
51
81
  end
52
82
 
@@ -55,10 +85,12 @@ module HubriseClient
55
85
  uri
56
86
  end
57
87
 
58
- def perform_request(uri, request)
59
- http = Net::HTTP.new(uri.host, uri.port)
60
- http.use_ssl = @use_https
61
- 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
62
94
  http.request(request)
63
95
  end
64
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
  # --------------------
@@ -150,8 +151,8 @@ module HubriseClient
150
151
  # --------------------
151
152
  def create_image(data, mime_type, catalog_id = nil)
152
153
  call_api("/catalogs/#{catalog_id_fallback(catalog_id)}/images", :post, data: data,
153
- headers: { "Content-Type" => mime_type },
154
- json: false)
154
+ headers: { "Content-Type" => mime_type },
155
+ json: false)
155
156
  end
156
157
 
157
158
  def get_image(image_id, catalog_id = nil)
@@ -162,6 +163,49 @@ module HubriseClient
162
163
  call_api("/catalogs/#{catalog_id_fallback(catalog_id)}/images/#{image_id}/data")
163
164
  end
164
165
 
166
+ # --------------------
167
+ # Inventory
168
+ # --------------------
169
+ def get_inventory(catalog_id = nil, location_id = nil)
170
+ endpoint = inventory_endpoint(catalog_id, location_id)
171
+ call_api(endpoint)
172
+ end
173
+
174
+ def update_inventory(params, catalog_id = nil, location_id = nil)
175
+ endpoint = inventory_endpoint(catalog_id, location_id)
176
+ call_api(endpoint, :put, data: params)
177
+ end
178
+
179
+ def patch_inventory(params, catalog_id = nil, location_id = nil)
180
+ endpoint = inventory_endpoint(catalog_id, location_id)
181
+ call_api(endpoint, :patch, data: params)
182
+ end
183
+
184
+ def inventory_endpoint(catalog_id = nil, location_id = nil)
185
+ if location_id
186
+ "/catalogs/#{catalog_id_fallback(catalog_id)}/locations/#{location_id}/inventory"
187
+ else
188
+ "/catalogs/#{catalog_id_fallback(catalog_id)}/location/inventory"
189
+ end
190
+ end
191
+
192
+ private :inventory_endpoint
193
+
194
+ # --------------------
195
+ # Loyalty cards
196
+ # --------------------
197
+ def create_loyalty_card(params, customer_list_id = nil)
198
+ call_api("/customer_lists/#{customer_list_id_fallback(customer_list_id)}/loyalty_cards", :post, data: params)
199
+ end
200
+
201
+ def create_loyalty_operation(loyalty_card_id, params, customer_list_id = nil)
202
+ call_api(
203
+ "/customer_lists/#{customer_list_id_fallback(customer_list_id)}/loyalty_cards/#{loyalty_card_id}/operations",
204
+ :post,
205
+ data: params
206
+ )
207
+ end
208
+
165
209
  private
166
210
 
167
211
  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.1".freeze
3
+ VERSION = "2.0.4"
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.1
4
+ version: 2.0.4
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: 2019-09-29 00:00:00.000000000 Z
12
+ date: 2022-04-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.5
105
+ rubygems_version: 3.1.2
106
106
  signing_key:
107
107
  specification_version: 4
108
108
  summary: HubRise Ruby client