shoptet 0.0.1 → 0.0.6
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/.gitignore +1 -0
- data/README.md +2 -3
- data/lib/shoptet.rb +51 -56
- data/lib/shoptet/request.rb +11 -0
- 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: 6d9c548dd730e3b59fb8ae843dab0fdae19b4779c27042c0aa5f01bed576cc88
|
4
|
+
data.tar.gz: 610aee2527b42a799c771b172a1fa54b942b5d0823080f2f2761271ce8efd6dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 334fc975ad12a81db120372b3478369db2ed4c67df97e7bbc6cd62f2fe875e1f39f2366ee8f82330edd80e517eb592cf3962491d5328aeaf9cb67a72b3d41352
|
7
|
+
data.tar.gz: c627078af8708d7ad6beec828d1e19ff22e6e088042e1fd09d67c4ae86307e4e76093499e7041ac096d0326400157681121ffb5c8e5618f9de9299e824701ff1
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Shoptet
|
2
2
|
|
3
|
-
This is Ruby API wrapper for [Shoptet API](https://
|
4
|
-
|
5
|
-
This is currently under development and hasn't been released to Rubygems yet.
|
3
|
+
This is Ruby API wrapper for [Shoptet API](https://shoptet.docs.apiary.io) which provides access to e-shop data for addon developers.
|
6
4
|
|
7
5
|
# How to install
|
8
6
|
|
@@ -82,6 +80,7 @@ Also they accept hash with params which will be passed to Shoptet api. Through t
|
|
82
80
|
## Other
|
83
81
|
|
84
82
|
* `Shoptet#new_api_token` - this returns new api token created with oauth token
|
83
|
+
* `Shoptet::install` - TODO
|
85
84
|
|
86
85
|
## License
|
87
86
|
|
data/lib/shoptet.rb
CHANGED
@@ -1,23 +1,17 @@
|
|
1
1
|
require_relative 'shoptet/request'
|
2
2
|
|
3
3
|
class Shoptet
|
4
|
-
class Error < StandardError
|
5
|
-
#TODO: check that this works
|
6
|
-
attr_reader :additional_data
|
7
|
-
|
8
|
-
def initialize message, additional_data = {}
|
9
|
-
super(message)
|
10
|
-
@additional_data = additional_data
|
11
|
-
end
|
12
|
-
end
|
4
|
+
class Error < StandardError; end
|
13
5
|
class AddonSuspended < StandardError; end
|
6
|
+
class AddonNotInstalled < StandardError; end
|
7
|
+
class InvalidTokenNoRights < StandardError; end
|
14
8
|
|
15
9
|
DEFAULT_ON_TOKEN_ERROR = -> (api) do
|
16
10
|
api.api_token = api.new_api_token
|
17
11
|
end
|
18
12
|
|
19
13
|
def self.version
|
20
|
-
'0.0.
|
14
|
+
'0.0.6'
|
21
15
|
end
|
22
16
|
|
23
17
|
def self.ar_on_token_error(model)
|
@@ -37,6 +31,19 @@ class Shoptet
|
|
37
31
|
end
|
38
32
|
end
|
39
33
|
|
34
|
+
def self.install uri, redirect_uri, client_id, client_secret, code
|
35
|
+
data = {
|
36
|
+
'redirect_uri' => redirect_uri,
|
37
|
+
'client_id' => client_id,
|
38
|
+
'client_secret' => client_secret,
|
39
|
+
'code' => code,
|
40
|
+
'grant_type' => 'authorization_code',
|
41
|
+
'scope' => 'api'
|
42
|
+
}
|
43
|
+
|
44
|
+
Shoptet::Request.post uri, data
|
45
|
+
end
|
46
|
+
|
40
47
|
attr_accessor :api_token
|
41
48
|
|
42
49
|
def initialize(oauth_url, oauth_token, api_token = nil, on_token_error = nil)
|
@@ -46,11 +53,19 @@ class Shoptet
|
|
46
53
|
@on_token_error = on_token_error || DEFAULT_ON_TOKEN_ERROR
|
47
54
|
end
|
48
55
|
|
56
|
+
#TODO: return ['data'] already
|
49
57
|
def shop_info api_params = {}
|
50
|
-
request 'https://api.myshoptet.com/api/eshop'
|
58
|
+
result = request 'https://api.myshoptet.com/api/eshop'
|
59
|
+
result['data']
|
51
60
|
end
|
52
61
|
|
53
|
-
def
|
62
|
+
def design_info api_params = {}
|
63
|
+
result = request 'https://api.myshoptet.com/api/eshop/design'
|
64
|
+
|
65
|
+
result['data']
|
66
|
+
end
|
67
|
+
|
68
|
+
def stocks api_params = {}
|
54
69
|
enumerize 'https://api.myshoptet.com/api/stocks', api_params
|
55
70
|
end
|
56
71
|
|
@@ -63,6 +78,11 @@ class Shoptet
|
|
63
78
|
enumerize uri, api_params
|
64
79
|
end
|
65
80
|
|
81
|
+
def stocks_movements warehouse_id, api_params = {}
|
82
|
+
uri = "https://api.myshoptet.com/api/stocks/#{warehouse_id}/movements"
|
83
|
+
enumerize uri, api_params
|
84
|
+
end
|
85
|
+
|
66
86
|
def product_categories api_params = {}
|
67
87
|
enumerize 'https://api.myshoptet.com/api/categories', api_params
|
68
88
|
end
|
@@ -93,35 +113,20 @@ class Shoptet
|
|
93
113
|
def order code, api_params = {}
|
94
114
|
uri = "https://api.myshoptet.com/api/orders/#{code}"
|
95
115
|
result = request assemble_uri(uri, api_params)
|
96
|
-
result.dig
|
116
|
+
result.dig 'data', 'order'
|
97
117
|
end
|
98
118
|
|
99
119
|
def product guid, api_params = {}
|
100
120
|
uri = "https://api.myshoptet.com/api/products/#{guid}"
|
101
121
|
result = request assemble_uri(uri, api_params)
|
102
|
-
result
|
122
|
+
result['data']
|
103
123
|
end
|
104
124
|
|
105
125
|
def new_api_token
|
106
126
|
headers = { 'Authorization' => "Bearer #{@oauth_token}" }
|
107
127
|
|
108
128
|
result = Shoptet::Request.get @oauth_url, headers
|
109
|
-
handle_errors result
|
110
|
-
|
111
|
-
# error = result['error']
|
112
|
-
# errors = result['errors'] || []
|
113
|
-
#
|
114
|
-
# #TODO: unite error handling with #request
|
115
|
-
# if error || errors.any?
|
116
|
-
# additional_data = {
|
117
|
-
# uri: @oauth_url,
|
118
|
-
# headers: scrub_sensitive_headers(headers)
|
119
|
-
# }
|
120
|
-
#
|
121
|
-
# raise Error.new result, additional_data
|
122
|
-
# else
|
123
|
-
# result.fetch 'access_token'
|
124
|
-
# end
|
129
|
+
handle_errors result, @oauth_url, headers
|
125
130
|
|
126
131
|
result.fetch 'access_token'
|
127
132
|
end
|
@@ -145,32 +150,36 @@ class Shoptet
|
|
145
150
|
total_pages = first_page.dig('data', 'paginator', 'pageCount') || 0
|
146
151
|
other_pages = 2..total_pages
|
147
152
|
|
148
|
-
first_page.dig('data', data_key).each { y.yield _1
|
153
|
+
first_page.dig('data', data_key).each { y.yield _1 }
|
149
154
|
|
150
155
|
other_pages.each do |page|
|
151
156
|
uri = assemble_uri base_uri, filters.merge(page: page)
|
152
157
|
result = request uri
|
153
|
-
result.dig('data', data_key).each { y.yield _1
|
158
|
+
result.dig('data', data_key).each { y.yield _1 }
|
154
159
|
end
|
155
160
|
end
|
156
161
|
end
|
157
162
|
|
158
|
-
def request uri
|
163
|
+
def request uri, retry_on_token_error = true
|
159
164
|
headers = { 'Shoptet-Access-Token' => @api_token,
|
160
165
|
'Content-Type' => 'application/vnd.shoptet.v1.0' }
|
161
166
|
|
162
167
|
result = Shoptet::Request.get uri, headers
|
163
|
-
token_errors = handle_errors result
|
168
|
+
token_errors = handle_errors result, uri, headers
|
164
169
|
|
165
170
|
if token_errors.any?
|
166
|
-
|
167
|
-
|
171
|
+
if retry_on_token_error
|
172
|
+
@on_token_error.call self
|
173
|
+
request uri, false
|
174
|
+
else
|
175
|
+
raise Error.new result
|
176
|
+
end
|
168
177
|
else
|
169
178
|
result
|
170
179
|
end
|
171
180
|
end
|
172
181
|
|
173
|
-
def handle_errors result
|
182
|
+
def handle_errors result, uri, headers
|
174
183
|
error = result['error']
|
175
184
|
errors = result['errors'] || []
|
176
185
|
token_errors, non_token_errors = errors.partition { |err| ['invalid-token', 'expired-token'].include? err['errorCode'] }
|
@@ -178,29 +187,15 @@ class Shoptet
|
|
178
187
|
if error || non_token_errors.any?
|
179
188
|
if error == 'addon_suspended'
|
180
189
|
raise AddonSuspended
|
190
|
+
elsif error == 'addon_not_installed'
|
191
|
+
raise AddonNotInstalled
|
192
|
+
elsif errors.any? { |err| err["errorCode"] == 'invalid-token-no-rights' }
|
193
|
+
raise InvalidTokenNoRights
|
181
194
|
else
|
182
|
-
|
183
|
-
uri: uri,
|
184
|
-
headers: scrub_sensitive_headers(headers)
|
185
|
-
}
|
186
|
-
|
187
|
-
raise Error.new result, additional_data
|
195
|
+
raise Error.new result
|
188
196
|
end
|
189
197
|
end
|
190
198
|
|
191
199
|
token_errors
|
192
200
|
end
|
193
|
-
|
194
|
-
def scrub_sensitive_headers headers
|
195
|
-
scrubbed = {}
|
196
|
-
|
197
|
-
to_scrub = ['Shoptet-Access-Token', 'Authorization']
|
198
|
-
to_scrub.each do |header|
|
199
|
-
if headers[header]
|
200
|
-
scrubbed[header] = "#{headers[header][0..20]}..."
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
headers.merge scrubbed
|
205
|
-
end
|
206
201
|
end
|
data/lib/shoptet/request.rb
CHANGED
@@ -28,5 +28,16 @@ class Shoptet
|
|
28
28
|
rescue Net::OpenTimeout
|
29
29
|
retry if attempt < 4
|
30
30
|
end
|
31
|
+
|
32
|
+
def self.post uri, body
|
33
|
+
req = Net::HTTP::Post.new uri
|
34
|
+
req.set_form_data body
|
35
|
+
|
36
|
+
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
37
|
+
http.request req
|
38
|
+
end
|
39
|
+
|
40
|
+
JSON.parse res.body
|
41
|
+
end
|
31
42
|
end
|
32
43
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Premysl Donat
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-11-
|
11
|
+
date: 2020-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: irb
|