shoptet 0.0.31 → 0.0.35
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 +4 -4
- data/lib/shoptet.rb +63 -8
- data/lib/shoptet/api_enumerator.rb +6 -4
- data/lib/shoptet/request.rb +33 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 834618535e5ec061b17899047e826cb4258cbcd1f162efba35dbd73201947eb8
|
4
|
+
data.tar.gz: 5b5cf393befc33bee8bf68f6352bb4a4e63d26b33c9d1d0fa5b3d01a02614340
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4478297d6958d02c4c647ed1b568b2da7748e774764625477179a029d04feacbd9ce80d3f6cc36d094b3ec2d2e511ce2281aec7658b15e5a0f4e21060a061b4
|
7
|
+
data.tar.gz: 52393d9a48043fac3485b4f99ca8b1c807987df18a50bf6abc9686b6ed850f1b24613e29c8beb5a2b20184bc3268c37ac9110f3102c95d161c356f4a5440c2ce
|
data/lib/shoptet.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'delegate'
|
2
4
|
require_relative 'shoptet/request'
|
3
5
|
require_relative 'shoptet/api_enumerator'
|
@@ -23,7 +25,7 @@ class Shoptet
|
|
23
25
|
end
|
24
26
|
|
25
27
|
def self.version
|
26
|
-
'0.0.
|
28
|
+
'0.0.35'
|
27
29
|
end
|
28
30
|
|
29
31
|
def self.ar_on_token_error(model)
|
@@ -111,12 +113,12 @@ class Shoptet
|
|
111
113
|
end
|
112
114
|
|
113
115
|
def shop_info api_params = {}
|
114
|
-
result =
|
116
|
+
result = get('https://api.myshoptet.com/api/eshop', api_params)
|
115
117
|
result['data']
|
116
118
|
end
|
117
119
|
|
118
120
|
def design_info api_params = {}
|
119
|
-
result =
|
121
|
+
result = get('https://api.myshoptet.com/api/eshop/design', api_params)
|
120
122
|
|
121
123
|
result['data']
|
122
124
|
end
|
@@ -163,17 +165,32 @@ class Shoptet
|
|
163
165
|
end
|
164
166
|
|
165
167
|
def order code, api_params = {}
|
166
|
-
result =
|
168
|
+
result = get("https://api.myshoptet.com/api/orders/#{code}", api_params)
|
167
169
|
result.dig('data', 'order')
|
168
170
|
end
|
169
171
|
|
170
172
|
def product guid, api_params = {}
|
171
|
-
result =
|
173
|
+
result = get("https://api.myshoptet.com/api/products/#{guid}", api_params)
|
172
174
|
result['data']
|
173
175
|
end
|
174
176
|
|
175
177
|
def product_by_code code, api_params = {}
|
176
|
-
result =
|
178
|
+
result = get("https://api.myshoptet.com/api/products/code/#{code}", api_params)
|
179
|
+
result['data']
|
180
|
+
end
|
181
|
+
|
182
|
+
def template_includes
|
183
|
+
result = get('https://api.myshoptet.com/api/template-include')
|
184
|
+
result['data']
|
185
|
+
end
|
186
|
+
|
187
|
+
def set_templates(data)
|
188
|
+
result = post_binary('https://api.myshoptet.com/api/template-include', data)
|
189
|
+
result['data']
|
190
|
+
end
|
191
|
+
|
192
|
+
def delete_template(location)
|
193
|
+
result = delete("https://api.myshoptet.com/api/template-include/#{location}")
|
177
194
|
result['data']
|
178
195
|
end
|
179
196
|
|
@@ -186,7 +203,7 @@ class Shoptet
|
|
186
203
|
result.fetch('access_token')
|
187
204
|
end
|
188
205
|
|
189
|
-
def
|
206
|
+
def get url, api_params = {}, retry_on_token_error = true
|
190
207
|
url = URI(url)
|
191
208
|
url.query = URI.encode_www_form(api_params) if api_params.any?
|
192
209
|
|
@@ -199,7 +216,45 @@ class Shoptet
|
|
199
216
|
if token_errors.any?
|
200
217
|
if retry_on_token_error
|
201
218
|
@on_token_error.call(self)
|
202
|
-
|
219
|
+
get(url, api_params, false)
|
220
|
+
else
|
221
|
+
raise Error.new(result)
|
222
|
+
end
|
223
|
+
else
|
224
|
+
result
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
def post_binary(url, data, retry_on_token_error = true)
|
229
|
+
url = URI(url)
|
230
|
+
headers = { 'Shoptet-Access-Token' => @api_token,
|
231
|
+
'Content-Type' => 'application/vnd.shoptet.v1.0' }
|
232
|
+
result = Shoptet::Request.post_binary(url, data, headers)
|
233
|
+
token_errors = handle_errors(result)
|
234
|
+
|
235
|
+
if token_errors.any?
|
236
|
+
if retry_on_token_error
|
237
|
+
@on_token_error.call(self)
|
238
|
+
post_binary(url, data, false)
|
239
|
+
else
|
240
|
+
raise Error.new(result)
|
241
|
+
end
|
242
|
+
else
|
243
|
+
result
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
def delete(url, retry_on_token_error = true)
|
248
|
+
url = URI(url)
|
249
|
+
headers = { 'Shoptet-Access-Token' => @api_token,
|
250
|
+
'Content-Type' => 'application/vnd.shoptet.v1.0' }
|
251
|
+
result = Shoptet::Request.delete(url, headers)
|
252
|
+
token_errors = handle_errors(result)
|
253
|
+
|
254
|
+
if token_errors.any?
|
255
|
+
if retry_on_token_error
|
256
|
+
@on_token_error.call(self)
|
257
|
+
delete(url, false)
|
203
258
|
else
|
204
259
|
raise Error.new(result)
|
205
260
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Shoptet
|
2
4
|
class ApiEnumerator < SimpleDelegator
|
3
5
|
def initialize base_url, filters, data_key, api
|
@@ -10,10 +12,10 @@ class Shoptet
|
|
10
12
|
first_page.dig('data', @data_key).each { y.yield _1 }
|
11
13
|
|
12
14
|
if total_pages > 1
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
(2..(total_pages - 1)).each do |page|
|
16
|
+
@api.request(base_url, filters.merge(page: page))
|
17
|
+
.dig('data', @data_key)
|
18
|
+
.each { y.yield _1 }
|
17
19
|
end
|
18
20
|
|
19
21
|
last_page.dig('data', @data_key).each { y.yield _1 }
|
data/lib/shoptet/request.rb
CHANGED
@@ -6,10 +6,10 @@ class Shoptet
|
|
6
6
|
def self.get url, headers
|
7
7
|
http = Net::HTTP.new(url.host, url.port)
|
8
8
|
http.use_ssl = true
|
9
|
-
http.open_timeout =
|
10
|
-
http.read_timeout =
|
11
|
-
http.write_timeout =
|
12
|
-
http.ssl_timeout =
|
9
|
+
http.open_timeout = 30
|
10
|
+
http.read_timeout = 30
|
11
|
+
http.write_timeout = 30
|
12
|
+
http.ssl_timeout = 30
|
13
13
|
|
14
14
|
request = Net::HTTP::Get.new(url)
|
15
15
|
headers.each do |key, value|
|
@@ -38,19 +38,41 @@ class Shoptet
|
|
38
38
|
JSON.parse(response.body)
|
39
39
|
end
|
40
40
|
|
41
|
+
def self.post_binary(url, body, headers)
|
42
|
+
request = Net::HTTP::Post.new(url)
|
43
|
+
request.body = body
|
44
|
+
request.content_type = 'appliaction/octet-stream'
|
45
|
+
headers.each do |key, value|
|
46
|
+
request[key] = value
|
47
|
+
end
|
48
|
+
|
49
|
+
response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
|
50
|
+
handle_net_timeouts { http.request(request) }
|
51
|
+
end
|
52
|
+
|
53
|
+
JSON.parse(response.body)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.delete(url, headers)
|
57
|
+
request = Net::HTTP::Delete.new(url)
|
58
|
+
headers.each do |key, value|
|
59
|
+
request[key] = value
|
60
|
+
end
|
61
|
+
|
62
|
+
response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
|
63
|
+
handle_net_timeouts { http.request(request) }
|
64
|
+
end
|
65
|
+
|
66
|
+
JSON.parse(response.body)
|
67
|
+
end
|
68
|
+
|
41
69
|
private
|
42
70
|
|
43
71
|
def self.handle_net_timeouts
|
44
|
-
attempts = 0
|
45
|
-
|
46
72
|
begin
|
47
73
|
yield
|
48
74
|
rescue Net::OpenTimeout
|
49
|
-
|
50
|
-
|
51
|
-
attempts += 1
|
52
|
-
|
53
|
-
retry
|
75
|
+
yield
|
54
76
|
end
|
55
77
|
end
|
56
78
|
end
|
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.
|
4
|
+
version: 0.0.35
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Premysl Donat
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: irb
|