Class: Shoptet

Inherits:
Object
  • Object
show all
Defined in:
lib/shoptet.rb,
lib/shoptet/request.rb,
lib/shoptet/api_enumerator.rb

Defined Under Namespace

Classes: AddonNotInstalled, AddonSuspended, ApiEnumerator, EmptyResponse, Error, InvalidTokenNoRights, LanguageBeeingInstalled, MalformedAuthHeader, MaxPageReached, MissingModule, NonJSONResponse, ProductNotFound, ProjectNotFound, Request, StockNotFound, SystemMaintenance, TooManyRequests, UnknownError, UrlLocked

Constant Summary collapse

EXPIRED_TOKEN_CODE =
'expired-token'
INVALID_TOKEN_CODE =
'invalid-token'
ADDON_NOT_INSTALLED =
'Addon installation is not approved.'
ON_TOKEN_ERROR =
-> (api) do
  api.api_token = api.new_api_token
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth_url:, oauth_token:, shop_url:, client_id:, api_token: nil, on_token_error: nil, logger: nil) ⇒ Shoptet

Returns a new instance of Shoptet.



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/shoptet.rb', line 87

def initialize(oauth_url:, oauth_token:, shop_url:, client_id:, api_token: nil, on_token_error: nil, logger: nil)
  @oauth_url = oauth_url
  @oauth_token = oauth_token
  @shop_url = shop_url
  @client_id = client_id
  @on_token_error = on_token_error || ON_TOKEN_ERROR
  @api_token = api_token
  @logger = logger
  if @logger
    @logger.debug("Initialized Shoptet api")
  end
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



85
86
87
# File 'lib/shoptet.rb', line 85

def api_token
  @api_token
end

Class Method Details

.ar_on_token_error(model) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shoptet.rb', line 38

def self.ar_on_token_error(model)
  -> (api) do
    model.with_lock do
      model.reload

      if model.api_token != api.api_token
        api.api_token = model.api_token
      else
        new_token = api.new_api_token
        api.api_token = new_token
        model.api_token = new_token
        model.save!
      end
    end
  end
end

.basic_eshop(url, access_token) ⇒ Object



81
82
83
# File 'lib/shoptet.rb', line 81

def self.basic_eshop url, access_token
  Shoptet::Request.get(url, { 'Authorization' => "Bearer #{access_token}" })
end

.install(url, redirect_url, client_id, client_secret, code) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/shoptet.rb', line 55

def self.install url, redirect_url, client_id, client_secret, code
  data = {
    'redirect_uri' => redirect_url,
    'client_id' => client_id,
    'client_secret' => client_secret,
    'code' => code,
    'grant_type' => 'authorization_code',
    'scope' => 'api'
  }

  Shoptet::Request.post(url, data)
end

.login_token(url, code, client_id, client_secret, redirect_url) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/shoptet.rb', line 68

def self. url, code, client_id, client_secret, redirect_url
  data = {
    code: code,
    grant_type: 'authorization_code',
    client_id: client_id,
    client_secret: client_secret,
    redirect_uri: redirect_url,
    scope: 'basic_eshop'
  }

  Shoptet::Request.post(url, data)
end

.versionObject



34
35
36
# File 'lib/shoptet.rb', line 34

def self.version
  '0.0.64'
end

Instance Method Details

#article(id, api_params = {}) ⇒ Object



211
212
213
214
# File 'lib/shoptet.rb', line 211

def article(id, api_params = {})
  result = get("https://api.myshoptet.com/api/articles/#{id}", api_params)
  result['data']['article']
end

#article_sections(api_params = {}) ⇒ Object



221
222
223
# File 'lib/shoptet.rb', line 221

def article_sections(api_params = {})
  enumerize("https://api.myshoptet.com/api/articles/sections", api_params)
end

#articles(api_params = {}) ⇒ Object



207
208
209
# File 'lib/shoptet.rb', line 207

def articles(api_params = {})
  enumerize("https://api.myshoptet.com/api/articles", api_params)
end

#authorize_url(redirect_url, state) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/shoptet.rb', line 114

def authorize_url redirect_url, state
  query = {
    client_id: @client_id,
    state: state,
    scope: 'basic_eshop',
    response_type: 'code',
    redirect_uri: redirect_url
  }.to_query

  URI("#{@shop_url}action/OAuthServer/authorize?#{query}")
end

#brands(api_params = {}) ⇒ Object



249
250
251
# File 'lib/shoptet.rb', line 249

def brands(api_params = {})
  enumerize("https://api.myshoptet.com/api/brands", api_params)
end

#delete(url, retry_on_token_error = true) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/shoptet.rb', line 345

def delete(url, retry_on_token_error = true)
  @api_token ||= @on_token_error.call(self)
  url = URI(url)
  headers = { 'Shoptet-Access-Token' => @api_token,
              'Content-Type' => 'application/vnd.shoptet.v1.0' }
  result = Shoptet::Request.delete(url, headers)
  token_errors = handle_errors(result)

  if token_errors.any?
    if retry_on_token_error
      @on_token_error.call(self)
      delete(url, false)
    else
      raise Error.new(result)
    end
  else
    result
  end
end

#delete_template(location) ⇒ Object



202
203
204
205
# File 'lib/shoptet.rb', line 202

def delete_template(location)
  result = delete("https://api.myshoptet.com/api/template-include/#{location}")
  result['data']
end

#design_info(api_params = {}) ⇒ Object



131
132
133
134
135
# File 'lib/shoptet.rb', line 131

def design_info api_params = {}
  result = get('https://api.myshoptet.com/api/eshop/design', api_params)

  result['data']
end

#endpoint_approved?(endpoint) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/shoptet.rb', line 104

def endpoint_approved? endpoint
  @approved_endpoints ||= endpoints

  @approved_endpoints.any? { _1['endpoint'] == endpoint }
end

#endpoints(api_params = {}) ⇒ Object



100
101
102
# File 'lib/shoptet.rb', line 100

def endpoints api_params = {}
  enumerize('https://api.myshoptet.com/api/system/endpoints', api_params)
end

#endpoints_approved?(*endpoints_to_check) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/shoptet.rb', line 110

def endpoints_approved? *endpoints_to_check
  endpoints_to_check.all? { endpoint_approved? _1 }
end

#flags(api_params = {}) ⇒ Object



245
246
247
# File 'lib/shoptet.rb', line 245

def flags(api_params = {})
  enumerize("https://api.myshoptet.com/api/products/flags", api_params)
end

#get(url, api_params = {}, retry_on_token_error = true) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/shoptet.rb', line 262

def get(url, api_params = {}, retry_on_token_error = true)
  @api_token ||= @on_token_error.call(self)
  url = URI(url)
  url.query = URI.encode_www_form(api_params) if api_params.any?

  headers = { 'Shoptet-Access-Token' => @api_token,
              'Content-Type' => 'application/vnd.shoptet.v1.0' }

  result = Shoptet::Request.get(url, headers, @logger)
  token_errors = handle_errors(result)

  if token_errors.any?
    if retry_on_token_error
      @on_token_error.call(self)
      get(url, api_params, false)
    else
      raise Error.new(result)
    end
  else
    result
  end
end

#job(id) ⇒ Object



236
237
238
239
# File 'lib/shoptet.rb', line 236

def job(id)
  result = get("https://api.myshoptet.com/api/system/jobs/#{id}")
  result['data']
end

#new_api_tokenObject



253
254
255
256
257
258
259
260
# File 'lib/shoptet.rb', line 253

def new_api_token
  headers = { 'Authorization' => "Bearer #{@oauth_token}" }

  result = Shoptet::Request.get(URI(@oauth_url), headers)
  handle_errors(result)

  result.fetch('access_token')
end

#order(code, api_params = {}) ⇒ Object



177
178
179
180
# File 'lib/shoptet.rb', line 177

def order code, api_params = {}
  result = get("https://api.myshoptet.com/api/orders/#{code}", api_params)
  result.dig('data', 'order')
end

#orders(api_params = {}) ⇒ Object



169
170
171
# File 'lib/shoptet.rb', line 169

def orders api_params = {}
  enumerize('https://api.myshoptet.com/api/orders', api_params)
end

#orders_changes(api_params = {}) ⇒ Object



173
174
175
# File 'lib/shoptet.rb', line 173

def orders_changes api_params = {}
  enumerize('https://api.myshoptet.com/api/orders/changes', api_params)
end

#patch(url, data, retry_on_token_error = true) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/shoptet.rb', line 285

def patch(url, data, retry_on_token_error = true)
  @api_token ||= @on_token_error.call(self)
  url = URI(url)
  headers = { 'Shoptet-Access-Token' => @api_token,
              'Content-Type' => 'application/vnd.shoptet.v1.0' }
  result = Shoptet::Request.patch(url, data, headers)
  token_errors = handle_errors(result)

  if token_errors.any?
    if retry_on_token_error
      @on_token_error.call(self)
      patch(url, data, false)
    else
      raise Error.new(result)
    end
  else
    result
  end
end

#post_binary(url, data, retry_on_token_error = true) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/shoptet.rb', line 325

def post_binary(url, data, retry_on_token_error = true)
  @api_token ||= @on_token_error.call(self)
  url = URI(url)
  headers = { 'Shoptet-Access-Token' => @api_token,
              'Content-Type' => 'application/vnd.shoptet.v1.0' }
  result = Shoptet::Request.post_binary(url, data, headers)
  token_errors = handle_errors(result)

  if token_errors.any?
    if retry_on_token_error
      @on_token_error.call(self)
      post_binary(url, data, false)
    else
      raise Error.new(result)
    end
  else
    result
  end
end

#post_json(url, data, retry_on_token_error = true) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/shoptet.rb', line 305

def post_json(url, data, retry_on_token_error = true)
  @api_token ||= @on_token_error.call(self)
  url = URI(url)
  headers = { 'Shoptet-Access-Token' => @api_token,
              'Content-Type' => 'application/vnd.shoptet.v1.0' }
  result = Shoptet::Request.post_json(url, data, headers)
  token_errors = handle_errors(result)

  if token_errors.any?
    if retry_on_token_error
      @on_token_error.call(self)
      patch(url, data, false)
    else
      raise Error.new(result)
    end
  else
    result
  end
end

#price_lists(api_params = {}) ⇒ Object



161
162
163
# File 'lib/shoptet.rb', line 161

def price_lists api_params = {}
  enumerize('https://api.myshoptet.com/api/pricelists', api_params)
end

#prices(price_list_id, api_params = {}) ⇒ Object



165
166
167
# File 'lib/shoptet.rb', line 165

def prices price_list_id, api_params = {}
  enumerize("https://api.myshoptet.com/api/pricelists/#{price_list_id}", api_params, 'pricelist')
end

#product(guid, api_params = {}) ⇒ Object



182
183
184
185
# File 'lib/shoptet.rb', line 182

def product guid, api_params = {}
  result = get("https://api.myshoptet.com/api/products/#{guid}", api_params)
  result['data']
end

#product_by_code(code, api_params = {}) ⇒ Object



187
188
189
190
# File 'lib/shoptet.rb', line 187

def product_by_code code, api_params = {}
  result = get("https://api.myshoptet.com/api/products/code/#{code}", api_params)
  result['data']
end

#product_categories(api_params = {}) ⇒ Object



153
154
155
# File 'lib/shoptet.rb', line 153

def product_categories api_params = {}
  enumerize('https://api.myshoptet.com/api/categories', api_params)
end

#products(api_params = {}) ⇒ Object



141
142
143
# File 'lib/shoptet.rb', line 141

def products api_params = {}
  enumerize("https://api.myshoptet.com/api/products", api_params)
end

#products_changes(api_params = {}) ⇒ Object



157
158
159
# File 'lib/shoptet.rb', line 157

def products_changes api_params = {}
  enumerize('https://api.myshoptet.com/api/products/changes', api_params)
end

#register_webhook(event, url) ⇒ Object



225
226
227
228
229
# File 'lib/shoptet.rb', line 225

def register_webhook(event, url)
  data = { data: [{ event: event, url: url }] }
  result = post_json("https://api.myshoptet.com/api/webhooks", data)
  result['data']['webhooks']
end

#set_templates(data) ⇒ Object



197
198
199
200
# File 'lib/shoptet.rb', line 197

def set_templates(data)
  result = post_binary('https://api.myshoptet.com/api/template-include', data)
  result['data']
end

#shop_info(api_params = {}) ⇒ Object



126
127
128
129
# File 'lib/shoptet.rb', line 126

def shop_info api_params = {}
  result = get('https://api.myshoptet.com/api/eshop', api_params)
  result['data']
end

#snapshot_products(api_params = {}) ⇒ Object



231
232
233
234
# File 'lib/shoptet.rb', line 231

def snapshot_products(api_params = {})
  result = get('https://api.myshoptet.com/api/products/snapshot', api_params)
  result['data']
end

#stocks(api_params = {}) ⇒ Object



137
138
139
# File 'lib/shoptet.rb', line 137

def stocks api_params = {}
  enumerize('https://api.myshoptet.com/api/stocks', api_params)
end

#stocks_movements(warehouse_id, api_params = {}) ⇒ Object



149
150
151
# File 'lib/shoptet.rb', line 149

def stocks_movements warehouse_id, api_params = {}
  enumerize("https://api.myshoptet.com/api/stocks/#{warehouse_id}/movements", api_params)
end

#supplies(warehouse_id, api_params = {}) ⇒ Object



145
146
147
# File 'lib/shoptet.rb', line 145

def supplies warehouse_id, api_params = {}
  enumerize("https://api.myshoptet.com/api/stocks/#{warehouse_id}/supplies", api_params)
end

#suspended?Boolean

Returns:

  • (Boolean)


365
366
367
368
369
# File 'lib/shoptet.rb', line 365

def suspended?
  false if shop_info
rescue Shoptet::AddonSuspended
  true
end

#template_includesObject



192
193
194
195
# File 'lib/shoptet.rb', line 192

def template_includes
  result = get('https://api.myshoptet.com/api/template-include')
  result['data']
end

#update_article(id, api_params = {}) ⇒ Object



216
217
218
219
# File 'lib/shoptet.rb', line 216

def update_article(id, api_params = {})
  result = patch("https://api.myshoptet.com/api/articles/#{id}", api_params)
  result['data']['article']
end

#webhooks(api_params = {}) ⇒ Object



241
242
243
# File 'lib/shoptet.rb', line 241

def webhooks(api_params={})
  enumerize("https://api.myshoptet.com/api/webhooks", api_params)
end