shoptet 0.0.50 → 0.0.51

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 +40 -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: 67a5715203ee35a2805467665199d2145191ae320ff223ee4e3ce4b655094912
4
+ data.tar.gz: 210622cc133ea367c2204ae32d045275678515a7dbe653ce42650bf416143c52
5
5
  SHA512:
6
- metadata.gz: 77d6ae85a1959efc9f569112f69b3b5f4af5bff2e2a2f75f63f7c00293721908e0312b2a8c861d52a3c8160701df07fd27308ba99fd95fbca08f69190cefb713
7
- data.tar.gz: 2f624576b2c244ba832c6bf176c23b6c9ad82c096a275c45dae0d6f863f89cc4e4a78d63cddd484c78010451f9b59b770b84b15b350dd73f05bc7c0b918fecb0
6
+ metadata.gz: 9fd716f2d14d1a7ba5444a9d5db67d3ad8fac4d29e8906e0c41cbfc1b98c6779ae4424041a6a46234298d516cceda674846a636b4a74160244189e12412b2487
7
+ data.tar.gz: 3a6625b639db24d653b477e6f16d735a84e8b3a2f0ddc73704af0cb1d09cdf564881d68a0ae7c5e54392311a16143142d829314529a896f28c8b12f8ff767e8c
@@ -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.51'
32
32
  end
33
33
 
34
34
  def self.ar_on_token_error(model)
@@ -214,6 +214,26 @@ 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
+
217
237
  def new_api_token
218
238
  headers = { 'Authorization' => "Bearer #{@oauth_token}" }
219
239
 
@@ -264,6 +284,25 @@ class Shoptet
264
284
  end
265
285
  end
266
286
 
287
+ def post_json(url, data, retry_on_token_error = true)
288
+ url = URI(url)
289
+ headers = { 'Shoptet-Access-Token' => @api_token,
290
+ 'Content-Type' => 'application/vnd.shoptet.v1.0' }
291
+ result = Shoptet::Request.post_json(url, data, headers)
292
+ token_errors = handle_errors(result)
293
+
294
+ if token_errors.any?
295
+ if retry_on_token_error
296
+ @on_token_error.call(self)
297
+ patch(url, data, false)
298
+ else
299
+ raise Error.new(result)
300
+ end
301
+ else
302
+ result
303
+ end
304
+ end
305
+
267
306
  def post_binary(url, data, retry_on_token_error = true)
268
307
  url = URI(url)
269
308
  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.51
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