beyond_api 0.9.0.pre → 0.12.1.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,252 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "beyond_api/utils"
4
+
5
+ module BeyondApi
6
+ module ShopLocations
7
+
8
+ #
9
+ # A +GET+ request is used to retrieve a list of all locations for the current shop.
10
+ #
11
+ # $ curl 'https://yourshop.api.urn/shop/locations' -i -X GET \
12
+ # -H 'Accept: application/hal+json' \
13
+ # -H 'Authorization: Bearer <Access token>'
14
+ #
15
+ # @beyond_api.scopes +loca:r+
16
+ #
17
+ # @option params [Boolean] :paginated
18
+ # @option params [Integer] :size the page size
19
+ # @option params [Integer] :page the page number
20
+ #
21
+ # @return [OpenStruct]
22
+ #
23
+ # @example
24
+ # @locations = session.shop.locations(size: 100, page: 0)
25
+ #
26
+ def locations(params = {})
27
+ handle_all_request("/shop/locations", :locations, params)
28
+ end
29
+
30
+ #
31
+ # A +POST+ request is used to create a location.
32
+ #
33
+ # $ curl 'https://yourshop.api.urn/shop/locations' -i -X POST \
34
+ # -H 'Content-Type: application/json;charset=UTF-8' \
35
+ # -H 'Accept: application/hal+json' \
36
+ # -H 'Authorization: Bearer <Access token>' \
37
+ # -d '{
38
+ # "languageCode" : "de",
39
+ # "storeCode" : "",
40
+ # "locationName" : "HealthyThings",
41
+ # "primaryPhone" : "01522 3097093",
42
+ # "address" : {
43
+ # "locality" : "Hamburg",
44
+ # "postalCode" : "20253",
45
+ # "regionCode" : "DE",
46
+ # "addressLines" : [ "Pilatuspool 2", "ePages GmbH" ]
47
+ # },
48
+ # "primaryCategory" : {
49
+ # "categoryId" : "gcid:store",
50
+ # "displayName" : "Geschaeft"
51
+ # },
52
+ # "websiteUrl" : "",
53
+ # "regularHours" : {
54
+ # "periods" : [ {
55
+ # "openDay" : "MONDAY",
56
+ # "openTime" : "18:00",
57
+ # "closeDay" : "TUESDAY",
58
+ # "closeTime" : "03:00"
59
+ # }, {
60
+ # "openDay" : "WEDNESDAY",
61
+ # "openTime" : "17:00",
62
+ # "closeDay" : "WEDNESDAY",
63
+ # "closeTime" : "23:00"
64
+ # } ]
65
+ # },
66
+ # "latLng" : {
67
+ # "latitude" : 53.5847424,
68
+ # "longitude" : 9.968901
69
+ # }
70
+ # }'
71
+ #
72
+ # @beyond_api.scopes +loca:c+
73
+ #
74
+ # @param body [Hash] the request body
75
+ #
76
+ # @return [OpenStruct]
77
+ #
78
+ # @example
79
+ # body = {
80
+ # "languageCode" => "de",
81
+ # "storeCode" => "",
82
+ # "locationName" => "HealthyThings",
83
+ # "primaryPhone" => "01522 3097093",
84
+ # "address" => {
85
+ # "locality" => "Hamburg",
86
+ # "postalCode" => "20253",
87
+ # "regionCode" => "DE",
88
+ # "addressLines" => [ "Pilatuspool 2", "ePages GmbH" ]
89
+ # },
90
+ # "primaryCategory" => {
91
+ # "categoryId" => "gcid:store",
92
+ # "displayName" => "Geschaeft"
93
+ # },
94
+ # "websiteUrl" => "",
95
+ # "regularHours" => {
96
+ # "periods" => [ {
97
+ # "openDay" => "MONDAY",
98
+ # "openTime" => "18:00",
99
+ # "closeDay" => "TUESDAY",
100
+ # "closeTime" => "03:00"
101
+ # }, {
102
+ # "openDay" => "WEDNESDAY",
103
+ # "openTime" => "17:00",
104
+ # "closeDay" => "WEDNESDAY",
105
+ # "closeTime" => "23:00"
106
+ # } ]
107
+ # },
108
+ # "latLng" : {
109
+ # "latitude" => 53.5847424,
110
+ # "longitude" => 9.968901
111
+ # }
112
+ # }
113
+ # @location = session.shop.create_location(body)
114
+ #
115
+ def create_location(body)
116
+ response, status = BeyondApi::Request.post(@session, "/shop/locations", body)
117
+
118
+ handle_response(response, status)
119
+ end
120
+
121
+ #
122
+ # A +DELETE+ request is used to delete a location.
123
+ #
124
+ # $ curl 'https://yourshop.api.urn/shop/locations/5bcf7c0a-d130-4cf8-af0c-5e57d4605be0' -i -X DELETE \
125
+ # -H 'Accept: application/hal+json' \
126
+ # -H 'Authorization: Bearer <Access token>'
127
+ #
128
+ # @beyond_api.scopes +loca:d+
129
+ #
130
+ # @param location_id [String] the location UUID
131
+ #
132
+ # @return true
133
+ #
134
+ # @example
135
+ # session.shop.delete_location("f461fb56-1984-4ade-bd4e-007c273cc923")
136
+ #
137
+ def delete_location(location_id)
138
+ response, status = BeyondApi::Request.delete(@session, "/shop/locations/#{location_id}")
139
+
140
+ handle_response(response, status, respond_with_true: true)
141
+ end
142
+
143
+ #
144
+ # A +GET+ request is used to retrieve a particular location by its id.
145
+ #
146
+ # $ curl 'https://yourshop.api.urn/shop/locations/09ca2715-cdf7-4af3-8b22-9ecf39d1e202' -i -X GET \
147
+ # -H 'Accept: application/hal+json' \
148
+ # -H 'Authorization: Bearer <Access token>'
149
+ #
150
+ # @beyond_api.scopes +loca:r+
151
+ #
152
+ # @param location_id [String] the location UUID
153
+ #
154
+ # @return [OpenStruct]
155
+ #
156
+ # @example
157
+ # @location = session.shop.location("27a94b71-9b17-4f06-9596-fbbf4d18021f")
158
+ #
159
+ def location(location_id)
160
+ response, status = BeyondApi::Request.get(@session, "/shop/locations/#{location_id}")
161
+
162
+ handle_response(response, status)
163
+ end
164
+
165
+ #
166
+ # A +PUT+ request is used to update a location.
167
+ #
168
+ # $ curl 'https://yourshop.api.urn/shop/locations/a7a2acfa-0243-4e52-8b56-81cb781ce61d' -i -X PUT \
169
+ # -H 'Content-Type: application/json;charset=UTF-8' \
170
+ # -H 'Accept: application/hal+json' \
171
+ # -H 'Authorization: Bearer <Access token>' \
172
+ # -d '{
173
+ # "languageCode" : "en",
174
+ # "storeCode" : "",
175
+ # "locationName" : "UnhealthyThings",
176
+ # "primaryPhone" : "01234 691997",
177
+ # "address" : {
178
+ # "locality" : "London",
179
+ # "postalCode" : "1",
180
+ # "regionCode" : "GB",
181
+ # "addressLines" : [ "St. James Square", "ePages Ltd" ]
182
+ # },
183
+ # "primaryCategory" : {
184
+ # "categoryId" : "gcid:store",
185
+ # "displayName" : "Shop"
186
+ # },
187
+ # "websiteUrl" : "",
188
+ # "regularHours" : {
189
+ # "periods" : [ {
190
+ # "openDay" : "SATURDAY",
191
+ # "openTime" : "06:00",
192
+ # "closeDay" : "SATURDAY",
193
+ # "closeTime" : "22:00"
194
+ # } ]
195
+ # },
196
+ # "latLng" : {
197
+ # "latitude" : 51.5072,
198
+ # "longitude" : -0.1353
199
+ # }
200
+ # }'
201
+ #
202
+ # @beyond_api.scopes +loca:u+
203
+ #
204
+ # @param location_id [String] the location UUID
205
+ # @param body [Hash] the request body
206
+ #
207
+ # @return [OpenStruct]
208
+ #
209
+ # @example
210
+ # body = {
211
+ # "languageCode" => "de",
212
+ # "storeCode" => "",
213
+ # "locationName" => "HealthyThings",
214
+ # "primaryPhone" => "01522 3097093",
215
+ # "address" => {
216
+ # "locality" => "Hamburg",
217
+ # "postalCode" => "20253",
218
+ # "regionCode" => "DE",
219
+ # "addressLines" => [ "Pilatuspool 2", "ePages GmbH" ]
220
+ # },
221
+ # "primaryCategory" => {
222
+ # "categoryId" => "gcid:store",
223
+ # "displayName" => "Geschaeft"
224
+ # },
225
+ # "websiteUrl" => "",
226
+ # "regularHours" => {
227
+ # "periods" => [ {
228
+ # "openDay" => "MONDAY",
229
+ # "openTime" => "18:00",
230
+ # "closeDay" => "TUESDAY",
231
+ # "closeTime" => "03:00"
232
+ # }, {
233
+ # "openDay" => "WEDNESDAY",
234
+ # "openTime" => "17:00",
235
+ # "closeDay" => "WEDNESDAY",
236
+ # "closeTime" => "23:00"
237
+ # } ]
238
+ # },
239
+ # "latLng" : {
240
+ # "latitude" => 53.5847424,
241
+ # "longitude" => 9.968901
242
+ # }
243
+ # }
244
+ # @location = session.shop.update_location("27a94b71-9b17-4f06-9596-fbbf4d18021f", body)
245
+ #
246
+ def update_location(location_id, body)
247
+ response, status = BeyondApi::Request.put(@session, "/shop/locations/#{location_id}", body)
248
+
249
+ handle_response(response, status)
250
+ end
251
+ end
252
+ end
@@ -1,9 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "beyond_api/utils"
4
+
3
5
  module BeyondApi
4
6
  class Token
5
7
  class InvalidSessionError < StandardError; end
6
8
 
9
+ include BeyondApi::Utils
10
+
7
11
  attr_reader :session
8
12
 
9
13
  def initialize(session)
@@ -44,7 +48,7 @@ module BeyondApi
44
48
  @session.refresh_token = response["refresh_token"]
45
49
  @session
46
50
  else
47
- BeyondApi::Error.new(response)
51
+ handle_error(response, status)
48
52
  end
49
53
  end
50
54
  end
@@ -40,6 +40,7 @@ module BeyondApi
40
40
  #
41
41
  # @beyond_api.scopes +user:r+
42
42
  #
43
+ # @option params [Boolean] :paginated
43
44
  # @option params [Integer] :size the page size
44
45
  # @option params [Integer] :page the page number
45
46
  #
@@ -49,9 +50,7 @@ module BeyondApi
49
50
  # @users = session.users.all(size: 100, page: 0)
50
51
  #
51
52
  def all(params = {})
52
- response, status = BeyondApi::Request.get(@session, "/users")
53
-
54
- handle_response(response, status)
53
+ handle_all_request("/users", :users, params)
55
54
  end
56
55
 
57
56
  #
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "beyond_api/utils"
4
- require "beyond_api/resources/variations/images"
5
- require "beyond_api/resources/variations/availability"
6
4
 
7
5
  module BeyondApi
6
+ autoload :Images, "beyond_api/resources/variations/images"
7
+ autoload :Availability, "beyond_api/resources/variations/availability"
8
+
8
9
  class Variations < Base
9
10
  include BeyondApi::VariationImages
10
11
  include BeyondApi::VariationAvailability
@@ -34,6 +34,7 @@ module BeyondApi
34
34
  # -H 'Accept: application/hal+json' \
35
35
  # -H 'Authorization: Bearer <Access token>'
36
36
  #
37
+ # @option params [Boolean] :paginated
37
38
  # @option params [Integer] :size the page size
38
39
  # @option params [Integer] :page the page number
39
40
  #
@@ -43,9 +44,7 @@ module BeyondApi
43
44
  # @webhook_subscriptions = session.webhook_subscriptions.all(size: 100, page: 0)
44
45
  #
45
46
  def all(params = {})
46
- response, status = BeyondApi::Request.get(@session, "/webhook-subscriptions", params)
47
-
48
- handle_response(response, status)
47
+ handle_all_request("/webhook-subscriptions", :webhook_subscriptions, params)
49
48
  end
50
49
 
51
50
  #
@@ -1,28 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "beyond_api/resources/base"
4
- require "beyond_api/resources/carts"
5
- require "beyond_api/resources/categories_view"
6
- require "beyond_api/resources/categories"
7
- require "beyond_api/resources/checkout_settings"
8
- require "beyond_api/resources/customers"
9
- require "beyond_api/resources/newsletter_target"
10
- require "beyond_api/resources/order_settings"
11
- require "beyond_api/resources/orders"
12
- require "beyond_api/resources/payment_methods"
13
- require "beyond_api/resources/product_attribute_definitions"
14
- require "beyond_api/resources/products_view"
15
- require "beyond_api/resources/products"
16
- require "beyond_api/resources/script_tags"
17
- require "beyond_api/resources/shipping_zones"
18
- require "beyond_api/resources/shop"
19
- require "beyond_api/resources/signers"
20
- require "beyond_api/resources/token"
21
- require "beyond_api/resources/users"
22
- require "beyond_api/resources/variations"
23
- require "beyond_api/resources/webhook_subscriptions"
24
-
25
3
  module BeyondApi
4
+ autoload :Base, "beyond_api/resources/base"
5
+ autoload :Carts, "beyond_api/resources/carts"
6
+ autoload :CategoriesView, "beyond_api/resources/categories_view"
7
+ autoload :Categories, "beyond_api/resources/categories"
8
+ autoload :CheckoutSettings, "beyond_api/resources/checkout_settings"
9
+ autoload :Customers, "beyond_api/resources/customers"
10
+ autoload :NewsletterTarget, "beyond_api/resources/newsletter_target"
11
+ autoload :OrderSettings, "beyond_api/resources/order_settings"
12
+ autoload :Orders, "beyond_api/resources/orders"
13
+ autoload :PaymentMethods, "beyond_api/resources/payment_methods"
14
+ autoload :ProductAttributeDefinitions, "beyond_api/resources/product_attribute_definitions"
15
+ autoload :ProductsView, "beyond_api/resources/products_view"
16
+ autoload :Products, "beyond_api/resources/products"
17
+ autoload :ScriptTags, "beyond_api/resources/script_tags"
18
+ autoload :ShippingZones, "beyond_api/resources/shipping_zones"
19
+ autoload :Shop, "beyond_api/resources/shop"
20
+ autoload :Signers, "beyond_api/resources/signers"
21
+ autoload :Token, "beyond_api/resources/token"
22
+ autoload :Users, "beyond_api/resources/users"
23
+ autoload :Variations, "beyond_api/resources/variations"
24
+ autoload :WebhookSubscriptions, "beyond_api/resources/webhook_subscriptions"
25
+
26
26
  class Session
27
27
  attr_reader :api_url
28
28
  attr_accessor :access_token, :refresh_token
@@ -10,11 +10,16 @@ module BeyondApi
10
10
  response = sanitize_response(response)
11
11
  BeyondApi.configuration.object_struct_responses ? to_object_struct(response) : response
12
12
  else
13
- BeyondApi.logger.error "[Beyond API] #{response}"
14
- BeyondApi.configuration.raise_error_requests ? raise(response.to_s) : BeyondApi::Error.new(response)
13
+ handle_error(response, status)
15
14
  end
16
15
  end
17
16
 
17
+ def handle_error(response, status)
18
+ BeyondApi.logger.error "[Beyond API] #{status}: #{response}"
19
+ error = BeyondApi::Error.new(response, status)
20
+ BeyondApi.configuration.raise_error_requests ? raise(error) : error
21
+ end
22
+
18
23
  def to_object_struct(data)
19
24
  if data.is_a? Hash
20
25
  return OpenStruct.new(data.map { |key, val| [key, to_object_struct(val)] }.to_h)
@@ -39,6 +44,22 @@ module BeyondApi
39
44
  key.chars.first == "_" ? key[1..-1] : key
40
45
  end
41
46
 
47
+ def handle_all_request(url, resource, params = {})
48
+ if params[:paginated] == false
49
+ result = all_paginated(url, { page: 0, size: 1000 })
50
+
51
+ (1..result[:page][:total_pages] - 1).each do |page|
52
+ result[:embedded][resource].concat(all_paginated(url, { page: page, size: 1000 })[:embedded][resource])
53
+ end
54
+
55
+ result.is_a?(Hash) ? result.delete(:page) : result.delete_field(:page)
56
+
57
+ result
58
+ else
59
+ all_paginated(url, params)
60
+ end
61
+ end
62
+
42
63
  private
43
64
 
44
65
  def transform(thing)
@@ -48,5 +69,11 @@ module BeyondApi
48
69
  else; thing
49
70
  end
50
71
  end
72
+
73
+ def all_paginated(url, params = {})
74
+ response, status = BeyondApi::Request.get(@session, url, params)
75
+
76
+ handle_response(response, status)
77
+ end
51
78
  end
52
79
  end
@@ -1,3 +1,3 @@
1
1
  module BeyondApi
2
- VERSION = "0.9.0.pre".freeze
2
+ VERSION = "0.12.1.pre".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beyond_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0.pre
4
+ version: 0.12.1.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Unai Abrisketa
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2020-05-11 00:00:00.000000000 Z
13
+ date: 2020-08-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -160,6 +160,11 @@ files:
160
160
  - lib/beyond_api/resources/script_tags.rb
161
161
  - lib/beyond_api/resources/shipping_zones.rb
162
162
  - lib/beyond_api/resources/shop.rb
163
+ - lib/beyond_api/resources/shops/address.rb
164
+ - lib/beyond_api/resources/shops/attributes.rb
165
+ - lib/beyond_api/resources/shops/images.rb
166
+ - lib/beyond_api/resources/shops/legals.rb
167
+ - lib/beyond_api/resources/shops/locations.rb
163
168
  - lib/beyond_api/resources/signers.rb
164
169
  - lib/beyond_api/resources/token.rb
165
170
  - lib/beyond_api/resources/users.rb