shoptet 0.0.50 → 0.0.52

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 (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/shoptet/request.rb +17 -0
  3. data/lib/shoptet.rb +44 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e950945e05fb23d88082c56b466624842bcef6ea73cfe7c6361869c3eff56f12
4
- data.tar.gz: eddb00b718bf01161700e9cb0cdd55e0cecea5f9c4ac6e5dd4bb7d3cd258ff38
3
+ metadata.gz: 15fcbfedaa4b4ea58a0d7445279327a24b68fa15d72769f6f4db309a3825c2cd
4
+ data.tar.gz: 8f1e5a6ff4c35e1646353e8dca4bbe9d2cb5a658454aca05744f2a0f553c7605
5
5
  SHA512:
6
- metadata.gz: 77d6ae85a1959efc9f569112f69b3b5f4af5bff2e2a2f75f63f7c00293721908e0312b2a8c861d52a3c8160701df07fd27308ba99fd95fbca08f69190cefb713
7
- data.tar.gz: 2f624576b2c244ba832c6bf176c23b6c9ad82c096a275c45dae0d6f863f89cc4e4a78d63cddd484c78010451f9b59b770b84b15b350dd73f05bc7c0b918fecb0
6
+ metadata.gz: da18bc74211075d1647a3d40577c189ec51f7e1ff08a3916b1f7a1de93b15665251d9d4b6769f5ac607efd6dc6063214da2e3b590ac04f4b03e7e17775f4fd4e
7
+ data.tar.gz: e774443131ad00a503f6fb77705f9b3e47ac6422e5773e6a83b24d4d0a33ac9ef0b437a0650b98d542dab74e2dd493217f60971064d36f7db16fa2321de8cc88
@@ -56,6 +56,23 @@ class Shoptet
56
56
  JSON.parse(response.body)
57
57
  end
58
58
 
59
+ def self.post_json(url, body, headers)
60
+ request = Net::HTTP::Post.new(url)
61
+ request.body = body.to_json
62
+ headers.each do |key, value|
63
+ request[key] = value
64
+ end
65
+
66
+ response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
67
+ handle_connection_errors { http.request(request) }
68
+ end
69
+
70
+ JSON.parse(response.body)
71
+ rescue JSON::ParserError => e
72
+ message = "Original message: #{e.message}, response body: #{response.body}, code :#{response.code}, headers: #{response.to_hash.to_s}"
73
+ fail Shoptet::NonJSONResponse.new(message)
74
+ end
75
+
59
76
  def self.patch(url, body, headers)
60
77
  request = Net::HTTP::Patch.new(url)
61
78
  request.body = body.to_json
data/lib/shoptet.rb CHANGED
@@ -28,7 +28,7 @@ class Shoptet
28
28
  end
29
29
 
30
30
  def self.version
31
- '0.0.50'
31
+ '0.0.52'
32
32
  end
33
33
 
34
34
  def self.ar_on_token_error(model)
@@ -214,6 +214,30 @@ class Shoptet
214
214
  enumerize("https://api.myshoptet.com/api/articles/sections", api_params)
215
215
  end
216
216
 
217
+ def register_webhook(event, url)
218
+ data = { data: [{ event: event, url: url }] }
219
+ result = post_json("https://api.myshoptet.com/api/webhooks", data)
220
+ result['data']['webhooks']
221
+ end
222
+
223
+ def snapshot_products(api_params = {})
224
+ result = get('https://api.myshoptet.com/api/products/snapshot', api_params)
225
+ result['data']
226
+ end
227
+
228
+ def job(id)
229
+ result = get("https://api.myshoptet.com/api/system/jobs/#{id}")
230
+ result['data']
231
+ end
232
+
233
+ def webhooks(api_params={})
234
+ enumerize("https://api.myshoptet.com/api/webhooks", api_params)
235
+ end
236
+
237
+ def endpoints(api_params={})
238
+ enumerize("https://api.myshoptet.com/api/system/endpoints", api_params)
239
+ end
240
+
217
241
  def new_api_token
218
242
  headers = { 'Authorization' => "Bearer #{@oauth_token}" }
219
243
 
@@ -264,6 +288,25 @@ class Shoptet
264
288
  end
265
289
  end
266
290
 
291
+ def post_json(url, data, retry_on_token_error = true)
292
+ url = URI(url)
293
+ headers = { 'Shoptet-Access-Token' => @api_token,
294
+ 'Content-Type' => 'application/vnd.shoptet.v1.0' }
295
+ result = Shoptet::Request.post_json(url, data, headers)
296
+ token_errors = handle_errors(result)
297
+
298
+ if token_errors.any?
299
+ if retry_on_token_error
300
+ @on_token_error.call(self)
301
+ patch(url, data, false)
302
+ else
303
+ raise Error.new(result)
304
+ end
305
+ else
306
+ result
307
+ end
308
+ end
309
+
267
310
  def post_binary(url, data, retry_on_token_error = true)
268
311
  url = URI(url)
269
312
  headers = { 'Shoptet-Access-Token' => @api_token,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shoptet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.50
4
+ version: 0.0.52
5
5
  platform: ruby
6
6
  authors:
7
7
  - Premysl Donat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-15 00:00:00.000000000 Z
11
+ date: 2023-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: irb