beyond_api 0.18.0.pre → 0.18.2.pre

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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +33 -0
  3. data/CHANGELOG.md +16 -0
  4. data/Gemfile +2 -0
  5. data/Gemfile.lock +29 -1
  6. data/Rakefile +4 -2
  7. data/beyond_api.gemspec +12 -8
  8. data/bin/console +2 -1
  9. data/lib/beyond_api/connection.rb +7 -4
  10. data/lib/beyond_api/error.rb +6 -6
  11. data/lib/beyond_api/ext.rb +16 -16
  12. data/lib/beyond_api/request.rb +4 -4
  13. data/lib/beyond_api/resources/carts.rb +52 -23
  14. data/lib/beyond_api/resources/categories_view.rb +16 -6
  15. data/lib/beyond_api/resources/newsletter_target.rb +10 -4
  16. data/lib/beyond_api/resources/orders.rb +152 -33
  17. data/lib/beyond_api/resources/payment_method_definitions.rb +11 -4
  18. data/lib/beyond_api/resources/product_attribute_definitions.rb +16 -4
  19. data/lib/beyond_api/resources/products/attachments.rb +0 -2
  20. data/lib/beyond_api/resources/products/availability.rb +20 -11
  21. data/lib/beyond_api/resources/products/cross_sells.rb +0 -1
  22. data/lib/beyond_api/resources/products/custom_attributes.rb +0 -1
  23. data/lib/beyond_api/resources/products/images.rb +12 -9
  24. data/lib/beyond_api/resources/products/searches.rb +2 -2
  25. data/lib/beyond_api/resources/products/variation_properties.rb +0 -1
  26. data/lib/beyond_api/resources/products/videos.rb +4 -3
  27. data/lib/beyond_api/resources/products.rb +28 -9
  28. data/lib/beyond_api/resources/products_view.rb +21 -5
  29. data/lib/beyond_api/resources/script_tags.rb +21 -5
  30. data/lib/beyond_api/resources/shipping_zones.rb +53 -12
  31. data/lib/beyond_api/resources/shop.rb +9 -2
  32. data/lib/beyond_api/resources/shops/address.rb +0 -1
  33. data/lib/beyond_api/resources/shops/attributes.rb +0 -1
  34. data/lib/beyond_api/resources/shops/images.rb +12 -9
  35. data/lib/beyond_api/resources/shops/legals.rb +14 -7
  36. data/lib/beyond_api/resources/shops/locations.rb +0 -1
  37. data/lib/beyond_api/resources/signers.rb +12 -3
  38. data/lib/beyond_api/resources/token.rb +6 -5
  39. data/lib/beyond_api/resources/users.rb +73 -15
  40. data/lib/beyond_api/resources/variations/availability.rb +17 -5
  41. data/lib/beyond_api/resources/variations/images.rb +32 -15
  42. data/lib/beyond_api/resources/variations.rb +14 -3
  43. data/lib/beyond_api/resources/webhook_subscriptions.rb +30 -8
  44. data/lib/beyond_api/session.rb +7 -1
  45. data/lib/beyond_api/utils.rb +44 -42
  46. data/lib/beyond_api/version.rb +3 -1
  47. data/lib/beyond_api.rb +3 -1
  48. data/lib/generators/beyond_api/install_generator.rb +1 -1
  49. metadata +52 -9
@@ -25,7 +25,11 @@ module BeyondApi
25
25
  # session.users.add_roles(user_id, body)
26
26
  #
27
27
  def add_roles(user_id, body)
28
- response, status = BeyondApi::Request.post(@session, "/users/#{user_id}/roles", body)
28
+ path = "/users/#{user_id}/roles"
29
+
30
+ response, status = BeyondApi::Request.post(@session,
31
+ path,
32
+ body)
29
33
 
30
34
  handle_response(response, status, respond_with_true: true)
31
35
  end
@@ -50,7 +54,9 @@ module BeyondApi
50
54
  # @users = session.users.all(size: 100, page: 0)
51
55
  #
52
56
  def all(params = {})
53
- handle_all_request("/users", :users, params)
57
+ path = "/users"
58
+
59
+ handle_all_request(path, :users, params)
54
60
  end
55
61
 
56
62
  #
@@ -77,7 +83,12 @@ module BeyondApi
77
83
  # session.users.change_password(user_id, current_password, new_password)
78
84
  #
79
85
  def change_password(user_id, current_password, new_password)
80
- response, status = BeyondApi::Request.post(@session, "/users/#{user_id}/change-password", current_password: current_password, new_password: new_password)
86
+ path = "/users/#{user_id}/change-password"
87
+
88
+ response, status = BeyondApi::Request.post(@session,
89
+ path,
90
+ current_password: current_password,
91
+ new_password: new_password)
81
92
 
82
93
  handle_response(response, status)
83
94
  end
@@ -104,7 +115,12 @@ module BeyondApi
104
115
  # session.users.change_username(user_id, new_username, current_password)
105
116
  #
106
117
  def change_username(user_id, new_username, current_password)
107
- response, status = BeyondApi::Request.post(@session, "/users/#{user_id}/change-username", new_username: new_username, current_password: current_password)
118
+ path = "/users/#{user_id}/change-username"
119
+
120
+ response, status = BeyondApi::Request.post(@session,
121
+ path,
122
+ new_username: new_username,
123
+ current_password: current_password)
108
124
 
109
125
  handle_response(response, status)
110
126
  end
@@ -137,7 +153,11 @@ module BeyondApi
137
153
  # @user = session.users.create(body)
138
154
  #
139
155
  def create(body)
140
- response, status = BeyondApi::Request.post(@session, "/users", body)
156
+ path = "/users"
157
+
158
+ response, status = BeyondApi::Request.post(@session,
159
+ path,
160
+ body)
141
161
 
142
162
  handle_response(response, status)
143
163
  end
@@ -158,7 +178,10 @@ module BeyondApi
158
178
  # session.users.enable_support_access
159
179
  #
160
180
  def enable_support_access
161
- response, status = BeyondApi::Request.post(@session, "/users/support")
181
+ path = "/users/support"
182
+
183
+ response, status = BeyondApi::Request.post(@session,
184
+ path)
162
185
 
163
186
  handle_response(response, status, respond_with_true: true)
164
187
  end
@@ -179,7 +202,10 @@ module BeyondApi
179
202
  # session.users.disable_support_access
180
203
  #
181
204
  def disable_support_access
182
- response, status = BeyondApi::Request.delete(@session, "/users/support")
205
+ path = "/users/support"
206
+
207
+ response, status = BeyondApi::Request.delete(@session,
208
+ path)
183
209
 
184
210
  handle_response(response, status, respond_with_true: true)
185
211
  end
@@ -202,7 +228,10 @@ module BeyondApi
202
228
  # @user = session.users.find("e4b528ce-bb9e-4cc5-95e1-7dadfa4cf0f3")
203
229
  #
204
230
  def find(user_id)
205
- response, status = BeyondApi::Request.get(@session, "/users/#{user_id}")
231
+ path = "/users/#{user_id}"
232
+
233
+ response, status = BeyondApi::Request.get(@session,
234
+ path)
206
235
 
207
236
  handle_response(response, status)
208
237
  end
@@ -223,7 +252,10 @@ module BeyondApi
223
252
  # @roles = session.users.roles("0d4bd0a5-94dc-498e-b6a6-305c619bb20d")
224
253
  #
225
254
  def roles(user_id)
226
- response, status = BeyondApi::Request.get(@session, "/users/#{user_id}/roles")
255
+ path = "/users/#{user_id}/roles"
256
+
257
+ response, status = BeyondApi::Request.get(@session,
258
+ path)
227
259
 
228
260
  handle_response(response, status)
229
261
  end
@@ -245,7 +277,11 @@ module BeyondApi
245
277
  # @user = session.users.search_by_username(username)
246
278
  #
247
279
  def search_by_username(username)
248
- response, status = BeyondApi::Request.get(@session, "/users/search/find-by-username", username: username)
280
+ path = "/users/search/find-by-username"
281
+
282
+ response, status = BeyondApi::Request.get(@session,
283
+ path,
284
+ username: username)
249
285
 
250
286
  handle_response(response, status)
251
287
  end
@@ -275,7 +311,12 @@ module BeyondApi
275
311
  # session.users.send_email_address_change(user_id, new_email, current_password, locale)
276
312
  #
277
313
  def send_email_address_change(user_id, new_email, current_password, locale)
278
- response, status = BeyondApi::Request.post(@session, "/users/#{user_id}/change-email-request", { new_email: new_email, current_password: current_password }, { locale: locale })
314
+ path = "/users/#{user_id}/change-email-request"
315
+
316
+ response, status = BeyondApi::Request.post(@session,
317
+ path,
318
+ { new_email: new_email, current_password: current_password },
319
+ { locale: locale })
279
320
 
280
321
  handle_response(response, status, respond_with_true: true)
281
322
  end
@@ -301,7 +342,12 @@ module BeyondApi
301
342
  # session.users.send_reset_password_email(email, locale)
302
343
  #
303
344
  def send_reset_password_email(email, locale)
304
- response, status = BeyondApi::Request.post(@session, "/users/reset-password-request", { email: email }, { locale: locale })
345
+ path = "/users/reset-password-request"
346
+
347
+ response, status = BeyondApi::Request.post(@session,
348
+ path,
349
+ { email: email },
350
+ { locale: locale })
305
351
 
306
352
  handle_response(response, status, respond_with_true: true)
307
353
  end
@@ -325,7 +371,11 @@ module BeyondApi
325
371
  # session.users.set_roles(user_id, body)
326
372
  #
327
373
  def set_roles(user_id, body)
328
- response, status = BeyondApi::Request.put(@session, "/users/#{user_id}/roles", body)
374
+ path = "/users/#{user_id}/roles"
375
+
376
+ response, status = BeyondApi::Request.put(@session,
377
+ path,
378
+ body)
329
379
 
330
380
  handle_response(response, status, respond_with_true: true)
331
381
  end
@@ -345,7 +395,10 @@ module BeyondApi
345
395
  # session.users.support_access
346
396
  #
347
397
  def support_access
348
- response, status = BeyondApi::Request.get(@session, "/users/support")
398
+ path = "/users/support"
399
+
400
+ response, status = BeyondApi::Request.get(@session,
401
+ path)
349
402
 
350
403
  handle_response(response, status)
351
404
  end
@@ -370,7 +423,12 @@ module BeyondApi
370
423
  # session.users.verify_password(password)
371
424
  #
372
425
  def verify_password(password, user_role)
373
- response, status = BeyondApi::Request.post(@session, "/users/verify-password", password: password, user_role: user_role)
426
+ path = "/users/verify-password"
427
+
428
+ response, status = BeyondApi::Request.post(@session,
429
+ path,
430
+ password: password,
431
+ user_role: user_role)
374
432
 
375
433
  handle_response(response, status, respond_with_true: true)
376
434
  end
@@ -4,7 +4,6 @@ require "beyond_api/utils"
4
4
 
5
5
  module BeyondApi
6
6
  module VariationAvailability
7
-
8
7
  #
9
8
  # A +POST+ request is used to adjust the available stock of a variation.
10
9
  #
@@ -26,7 +25,11 @@ module BeyondApi
26
25
  # @availability = session.variations.adjust_stock_level(product_id, variation_id, { relativeAmount => -1 })
27
26
  #
28
27
  def adjust_stock_level(product_id, variation_id, relative_amount)
29
- response, status = BeyondApi::Request.post(@session, "/products/#{product_id}/variations/#{variation_id}/availability/adjust-available-stock", relative_amount: relative_amount)
28
+ path = "/products/#{product_id}/variations/#{variation_id}/availability/adjust-available-stock"
29
+
30
+ response, status = BeyondApi::Request.post(@session,
31
+ path,
32
+ relative_amount: relative_amount)
30
33
 
31
34
  handle_response(response, status)
32
35
  end
@@ -50,7 +53,10 @@ module BeyondApi
50
53
  # @availability = session.variations.availability("fb22d408-00dc-47e3-ae58-e35769bdb428", "13b28149-975a-4f47-ad54-bdc4ca4a07ec")
51
54
  #
52
55
  def availability(product_id, variation_id)
53
- response, status = BeyondApi::Request.get(@session, "/products/#{product_id}/variations/#{variation_id}/availability")
56
+ path = "/products/#{product_id}/variations/#{variation_id}/availability"
57
+
58
+ response, status = BeyondApi::Request.get(@session,
59
+ path)
54
60
 
55
61
  handle_response(response, status)
56
62
  end
@@ -74,7 +80,10 @@ module BeyondApi
74
80
  # @availability = session.variations.enable_purchaability("1e3a92b-6f3b-4415-bd8f-c9c8921a5a73", "13b28149-975a-4f47-ad54-bdc4ca4a07ec")
75
81
  #
76
82
  def enable_purchasability(product_id, variation_id)
77
- response, status = BeyondApi::Request.post(@session, "/products/#{product_id}/variations/#{variation_id}/availability/enable-purchasability")
83
+ path = "/products/#{product_id}/variations/#{variation_id}/availability/enable-purchasability"
84
+
85
+ response, status = BeyondApi::Request.post(@session,
86
+ path)
78
87
 
79
88
  handle_response(response, status)
80
89
  end
@@ -97,7 +106,10 @@ module BeyondApi
97
106
  # @availability = session.variations.disable_purchasability("17e3a92b-6f3b-4415-bd8f-c9c8921a5a73", "13b28149-975a-4f47-ad54-bdc4ca4a07ec")
98
107
  #
99
108
  def disable_purchasability(product_id, variation_id)
100
- response, status = BeyondApi::Request.post(@session, "/products/#{product_id}/variations/#{variation_id}/availability/disable-purchasability")
109
+ path = "/products/#{product_id}/variations/#{variation_id}/availability/disable-purchasability"
110
+
111
+ response, status = BeyondApi::Request.post(@session,
112
+ path)
101
113
 
102
114
  handle_response(response, status)
103
115
  end
@@ -4,7 +4,6 @@ require "beyond_api/utils"
4
4
 
5
5
  module BeyondApi
6
6
  module VariationImages
7
-
8
7
  #
9
8
  # A +POST+ request is used to create an image and add it to a variation. The URL of the image will be assigned to the variation. The image URL has to be provided in body of the request.
10
9
  #
@@ -35,12 +34,15 @@ module BeyondApi
35
34
  # @image = session.variations.add_image("a08ca814-52e9-4e00-82a2-3e9b012e5f9d", "4b58cdb7-4d3d-419a-ae27-8469f8b04276", body)
36
35
  #
37
36
  def add_image(product_id, variation_id, body)
38
- response, status = BeyondApi::Request.post(@session, "/products/#{product_id}/variations/#{variation_id}/images", body)
39
-
37
+ path = "/products/#{product_id}/variations/#{variation_id}/images"
38
+
39
+ response, status = BeyondApi::Request.post(@session,
40
+ path,
41
+ body)
42
+
40
43
  handle_response(response, status)
41
44
  end
42
45
 
43
-
44
46
  #
45
47
  # A +DELETE+ request is used to delete a variation image
46
48
  #
@@ -61,7 +63,10 @@ module BeyondApi
61
63
  # session.variations.delete_image("8f5736f8-0a5f-4c08-bbac-3c524e2a6294", "86f3047c-ff29-4906-83c1-93e24ef88f3e", "193d2ba4-3cf0-4326-a655-ef46e8a97c6a")
62
64
  #
63
65
  def delete_image(product_id, variation_id, image_id)
64
- response, status = BeyondApi::Request.delete(@session, "/products/#{product_id}/variations/#{variation_id}/images/#{image_id}")
66
+ path = "/products/#{product_id}/variations/#{variation_id}/images/#{image_id}"
67
+
68
+ response, status = BeyondApi::Request.delete(@session,
69
+ path)
65
70
 
66
71
  handle_response(response, status, respond_with_true: true)
67
72
  end
@@ -86,7 +91,10 @@ module BeyondApi
86
91
  # @image = session.variations.image("8665fc36-003e-4120-8a74-a9d6449644ae", "a9163db42-92e7-418c-a3d8-651e7aaca569", "86fc2691-5dfb-47e1-aae7-4bc2f658a80b")
87
92
  #
88
93
  def image(product_id, image_id)
89
- response, status = BeyondApi::Request.get(@session, "/products/#{product_id}/variations/#{variation_id}/images/#{image_id}")
94
+ path = "/products/#{product_id}/variations/#{variation_id}/images/#{image_id}"
95
+
96
+ response, status = BeyondApi::Request.get(@session,
97
+ path)
90
98
 
91
99
  handle_response(response, status)
92
100
  end
@@ -112,7 +120,11 @@ module BeyondApi
112
120
  # @images = session.variations.images("b4948e53-05af-4e0b-8877-28bc8811f73e", "50c5bf45-5dd6-4bae-babf-813c7cdca488", size: 100, page: 0)
113
121
  #
114
122
  def images(product_id, variation_id, params = {})
115
- response, status = BeyondApi::Request.get(@session, "/products/#{product_id}/variations/#{variation_id}/images", params)
123
+ path = "/products/#{product_id}/variations/#{variation_id}/images"
124
+
125
+ response, status = BeyondApi::Request.get(@session,
126
+ path,
127
+ params)
116
128
 
117
129
  handle_response(response, status)
118
130
  end
@@ -142,17 +154,22 @@ module BeyondApi
142
154
  # session.variations.upload_image("4125b993-49fc-47c8-b9b3-76d8871e4e06", "d7fecf94-2e57-4122-8c94-a0acd840c111", "/home/epages/file.png", "file.png")
143
155
  #
144
156
  def upload_image(product_id, variation_id, image_path, image_name)
157
+ path = "/products/#{product_id}/variations/#{variation_id}/images"
145
158
  content_type = case File.extname(image_path)
146
- when ".png"
147
- "image/png"
148
- when ".jpg", ".jpeg"
149
- "image/jpeg"
150
- when ".gif"
151
- "image/gif"
152
- end
159
+ when ".png"
160
+ "image/png"
161
+ when ".jpg", ".jpeg"
162
+ "image/jpeg"
163
+ when ".gif"
164
+ "image/gif"
165
+ end
153
166
  image_binary = File.binread(image_path)
154
167
 
155
- response, status = BeyondApi::Request.upload(@session, "/products/#{product_id}/variations/#{variation_id}/images", image_binary, content_type, { file_name: image_name })
168
+ response, status = BeyondApi::Request.upload(@session,
169
+ path,
170
+ image_binary,
171
+ content_type,
172
+ { file_name: image_name })
156
173
 
157
174
  handle_response(response, status, respond_with_true: true)
158
175
  end
@@ -29,7 +29,11 @@ module BeyondApi
29
29
  # @variations = session.variations.all("dc1b5caf-51ea-4fcd-b1ba-0c5128e91d17", { size: 100, page: 0 })
30
30
  #
31
31
  def all(product_id, params = {})
32
- response, status = BeyondApi::Request.get(@session, "/products/#{product_id}/variations", params)
32
+ path = "/products/#{product_id}/variations"
33
+
34
+ response, status = BeyondApi::Request.get(@session,
35
+ path,
36
+ params)
33
37
 
34
38
  handle_response(response, status)
35
39
  end
@@ -53,7 +57,10 @@ module BeyondApi
53
57
  # @variation = session.variations.find("5f6e426e-c8d9-48ba-9b37-9a8eb6381373", "f6e5bb16-af2e-440f-acd3-a883ad3c1922")
54
58
  #
55
59
  def find(product_id, variation_id)
56
- response, status = BeyondApi::Request.get(@session, "/products/#{product_id}/variations/#{variation_id}")
60
+ path = "/products/#{product_id}/variations/#{variation_id}"
61
+
62
+ response, status = BeyondApi::Request.get(@session,
63
+ path)
57
64
 
58
65
  handle_response(response, status)
59
66
  end
@@ -143,7 +150,11 @@ module BeyondApi
143
150
  # @variation = session.variations.update("7cf4b5b1-b141-4869-96d1-4eaee8bf7563", "9f93fdd0-2d21-4ea9-b9d7-e9a53edb091b", body)
144
151
  #
145
152
  def update(product_id, variation_id, body)
146
- response, status = BeyondApi::Request.patch(@session, "/products/#{product_id}/variations/#{variation_id}", body)
153
+ path = "/products/#{product_id}/variations/#{variation_id}"
154
+
155
+ response, status = BeyondApi::Request.patch(@session,
156
+ path,
157
+ body)
147
158
 
148
159
  handle_response(response, status)
149
160
  end
@@ -3,7 +3,7 @@
3
3
  require "beyond_api/utils"
4
4
 
5
5
  module BeyondApi
6
- class WebhookSubscriptions < Base # :category: Resources
6
+ class WebhookSubscriptions < Base
7
7
  include BeyondApi::Utils
8
8
 
9
9
  #
@@ -22,7 +22,10 @@ module BeyondApi
22
22
  # session.webhook_subscriptions.activate("268a8629-55cd-4890-9013-936b9b5ea14c")
23
23
  #
24
24
  def activate(webhook_subscription_id)
25
- response, status = BeyondApi::Request.post(@session, "/webhook-subscriptions/#{webhook_subscription_id}/activate")
25
+ path = "/webhook-subscriptions/#{webhook_subscription_id}/activate"
26
+
27
+ response, status = BeyondApi::Request.post(@session,
28
+ path)
26
29
 
27
30
  handle_response(response, status, respond_with_true: true)
28
31
  end
@@ -44,7 +47,9 @@ module BeyondApi
44
47
  # @webhook_subscriptions = session.webhook_subscriptions.all(size: 100, page: 0)
45
48
  #
46
49
  def all(params = {})
47
- handle_all_request("/webhook-subscriptions", :webhook_subscriptions, params)
50
+ path = "/webhook-subscriptions"
51
+
52
+ handle_all_request(path, :webhook_subscriptions, params)
48
53
  end
49
54
 
50
55
  #
@@ -74,7 +79,11 @@ module BeyondApi
74
79
  # @webhook_subscription = session.webhook_subscriptions.create(body)
75
80
  #
76
81
  def create(body)
77
- response, status = BeyondApi::Request.post(@session, "/webhook-subscriptions", body)
82
+ path = "/webhook-subscriptions"
83
+
84
+ response, status = BeyondApi::Request.post(@session,
85
+ path,
86
+ body)
78
87
 
79
88
  handle_response(response, status)
80
89
  end
@@ -93,7 +102,10 @@ module BeyondApi
93
102
  # session.webhook_subscriptions.deactivate("a597cea4-b688-4164-8c56-b6568ea4d5aa")
94
103
  #
95
104
  def deactivate(webhook_subscription_id)
96
- response, status = BeyondApi::Request.post(@session, "/webhook-subscriptions/#{webhook_subscription_id}/deactivate")
105
+ path = "/webhook-subscriptions/#{webhook_subscription_id}/deactivate"
106
+
107
+ response, status = BeyondApi::Request.post(@session,
108
+ path)
97
109
 
98
110
  handle_response(response, status, respond_with_true: true)
99
111
  end
@@ -112,7 +124,10 @@ module BeyondApi
112
124
  # session.webhook_subscriptions.delete("c6076a5a-a8ad-443f-b20b-8a1b268b069e")
113
125
  #
114
126
  def delete(webhook_subscription_id)
115
- response, status = BeyondApi::Request.delete(@session, "/webhook-subscriptions/#{webhook_subscription_id}")
127
+ path = "/webhook-subscriptions/#{webhook_subscription_id}"
128
+
129
+ response, status = BeyondApi::Request.delete(@session,
130
+ path)
116
131
 
117
132
  handle_response(response, status, respond_with_true: true)
118
133
  end
@@ -132,7 +147,10 @@ module BeyondApi
132
147
  # @webhook_subscription = session.webhook_subscriptions.find("3d44ec71-768c-4927-9069-a96a5153e87c")
133
148
  #
134
149
  def find(webhook_subscription_id)
135
- response, status = BeyondApi::Request.get(@session, "/webhook-subscriptions/#{webhook_subscription_id}")
150
+ path = "/webhook-subscriptions/#{webhook_subscription_id}"
151
+
152
+ response, status = BeyondApi::Request.get(@session,
153
+ path)
136
154
 
137
155
  handle_response(response, status)
138
156
  end
@@ -167,7 +185,11 @@ module BeyondApi
167
185
  # @webhook_subscription = session.webhook_subscriptions.update("6f3bc033-c2d1-4f44-80e3-1b668f6bd699", body)
168
186
  #
169
187
  def update(webhook_subscription_id, body)
170
- response, status = BeyondApi::Request.put(@session, "/webhook-subscriptions/#{webhook_subscription_id}", body)
188
+ path = "/webhook-subscriptions/#{webhook_subscription_id}"
189
+
190
+ response, status = BeyondApi::Request.put(@session,
191
+ path,
192
+ body)
171
193
 
172
194
  handle_response(response, status)
173
195
  end
@@ -26,11 +26,17 @@ module BeyondApi
26
26
  autoload :WebhookSubscriptions, "beyond_api/resources/webhook_subscriptions"
27
27
 
28
28
  class Session
29
+ class InvalidUriProtocolError < StandardError; end
30
+
29
31
  attr_reader :api_url
30
32
  attr_accessor :access_token, :refresh_token
31
33
 
32
34
  def initialize(api_url:, access_token: nil, refresh_token: nil)
33
- @api_url = api_url
35
+ raise InvalidUriProtocolError, "Invalid URI protocol" unless api_url.start_with? "https://"
36
+
37
+ uri = URI.parse(api_url)
38
+
39
+ @api_url = "#{uri.scheme}://#{uri.host}/api"
34
40
  @access_token = access_token
35
41
  @refresh_token = refresh_token
36
42
  end
@@ -4,13 +4,23 @@ module BeyondApi
4
4
  module Utils
5
5
  extend self
6
6
 
7
- def handle_response(response, status, respond_with_true: false)
8
- if status.between?(200, 299)
9
- return true if respond_with_true
10
- response = sanitize_response(response)
11
- BeyondApi.configuration.object_struct_responses ? to_object_struct(response) : response
7
+ def handle_all_request(url, resource, params = {})
8
+ paginated_size = BeyondApi.configuration.all_pagination_size
9
+
10
+ if params[:paginated] == false
11
+ result = all_paginated(url, params.merge(page: 0, size: paginated_size))
12
+
13
+ (1..result[:page][:total_pages] - 1).each do |page|
14
+ result[:embedded][resource].concat(all_paginated(url, params.merge(page: page, size: paginated_size))[:embedded][resource])
15
+ end
16
+
17
+ result[:page][:size] = result[:page][:total_elements]
18
+ result[:page][:total_pages] = 1
19
+ result[:page][:number] = 0
20
+
21
+ result
12
22
  else
13
- handle_error(response, status)
23
+ all_paginated(url, params)
14
24
  end
15
25
  end
16
26
 
@@ -20,64 +30,56 @@ module BeyondApi
20
30
  BeyondApi.configuration.raise_error_requests ? raise(error) : error
21
31
  end
22
32
 
23
- def to_object_struct(data)
24
- if data.is_a? Hash
25
- return OpenStruct.new(data.map { |key, val| [key, to_object_struct(val)] }.to_h)
26
- elsif data.is_a? Array
27
- return data.map { |o| to_object_struct(o) }
33
+ def handle_response(response, status, respond_with_true: false)
34
+ if status.between?(200, 299)
35
+ return true if respond_with_true
36
+
37
+ response = sanitize_response(response)
38
+ BeyondApi.configuration.object_struct_responses ? to_object_struct(response) : response
28
39
  else
29
- return data
40
+ handle_error(response, status)
30
41
  end
31
42
  end
32
43
 
44
+ def sanitize_key(key)
45
+ key.chars.first == "_" ? key[1..-1] : key
46
+ end
47
+
33
48
  def sanitize_response(hash)
34
49
  {}.tap do |h|
35
50
  hash.each do |key, value|
36
51
  next if key == "_links" && BeyondApi.configuration.remove_response_links
52
+
37
53
  key = sanitize_key(key) if BeyondApi.configuration.remove_response_key_underscores
38
54
  h[key.underscore.to_sym] = transform(value)
39
55
  end
40
56
  end
41
57
  end
42
58
 
43
- def sanitize_key(key)
44
- key.chars.first == "_" ? key[1..-1] : key
45
- end
46
-
47
- def handle_all_request(url, resource, params = {})
48
- paginated_size = BeyondApi.configuration.all_pagination_size
49
-
50
- if params[:paginated] == false
51
- result = all_paginated(url, params.merge(page: 0, size: paginated_size))
52
-
53
- (1..result[:page][:total_pages] - 1).each do |page|
54
- result[:embedded][resource].concat(all_paginated(url, params.merge(page: page, size: paginated_size))[:embedded][resource])
55
- end
56
-
57
- result[:page][:size] = result[:page][:total_elements]
58
- result[:page][:total_pages] = 1
59
- result[:page][:number] = 0
60
-
61
- result
59
+ def to_object_struct(data)
60
+ if data.is_a? Hash
61
+ return OpenStruct.new(data.map { |key, val| [key, to_object_struct(val)] }.to_h)
62
+ elsif data.is_a? Array
63
+ return data.map { |o| to_object_struct(o) }
62
64
  else
63
- all_paginated(url, params)
65
+ return data
64
66
  end
65
67
  end
66
68
 
67
69
  private
68
70
 
69
- def transform(thing)
70
- case thing
71
- when Hash; sanitize_response(thing)
72
- when Array; thing.map { |v| transform(v) }
73
- else; thing
74
- end
75
- end
71
+ def all_paginated(url, params = {})
72
+ response, status = BeyondApi::Request.get(@session, url, params)
76
73
 
77
- def all_paginated(url, params = {})
78
- response, status = BeyondApi::Request.get(@session, url, params)
74
+ handle_response(response, status)
75
+ end
79
76
 
80
- handle_response(response, status)
77
+ def transform(thing)
78
+ case thing
79
+ when Hash; sanitize_response(thing)
80
+ when Array; thing.map { |v| transform(v) }
81
+ else; thing
81
82
  end
83
+ end
82
84
  end
83
85
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BeyondApi
2
- VERSION = "0.18.0.pre".freeze
4
+ VERSION = "0.18.2.pre"
3
5
  end
data/lib/beyond_api.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "beyond_api/version"
2
4
 
3
5
  require "logger"
@@ -25,7 +27,7 @@ module BeyondApi
25
27
  end
26
28
 
27
29
  class Configuration
28
- attr_accessor :client_id, :client_secret, :open_timeout, :timeout, :remove_response_links,
30
+ attr_accessor :client_id, :client_secret, :open_timeout, :timeout, :remove_response_links,
29
31
  :remove_response_key_underscores, :object_struct_responses, :raise_error_requests,
30
32
  :log_headers, :log_bodies, :log_level, :all_pagination_size
31
33
 
@@ -3,7 +3,7 @@
3
3
  module BeyondApi
4
4
  module Generators
5
5
  class InstallGenerator < Rails::Generators::Base
6
- source_root File.expand_path("../../templates", __FILE__)
6
+ source_root File.expand_path("../templates", __dir__)
7
7
 
8
8
  def copy_initializer
9
9
  template "beyond_api_initializer.rb", "config/initializers/beyond_api.rb"