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, LanguageNotFound, 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.



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

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.



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

def api_token
  @api_token
end

Class Method Details

.ar_on_token_error(model) ⇒ Object



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

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



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

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



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

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



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

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



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

def self.version
  '0.0.65'
end

Instance Method Details

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



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

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



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

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

#articles(api_params = {}) ⇒ Object



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

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

#authorize_url(redirect_url, state) ⇒ Object



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

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



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

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

#delete(url, retry_on_token_error = true) ⇒ Object



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

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



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

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

#design_info(api_params = {}) ⇒ Object



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

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)


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

def endpoint_approved? endpoint
  @approved_endpoints ||= endpoints

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

#endpoints(api_params = {}) ⇒ Object



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

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

#endpoints_approved?(*endpoints_to_check) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#flags(api_params = {}) ⇒ Object



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

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



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

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



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

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

#new_api_tokenObject



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

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



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

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



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

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

#orders_changes(api_params = {}) ⇒ Object



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

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



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

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



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

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



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

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



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

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

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



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

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



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

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



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

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



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

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

#products(api_params = {}) ⇒ Object



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

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

#products_changes(api_params = {}) ⇒ Object



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

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

#register_webhook(event, url) ⇒ Object



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

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



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

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

#shop_info(api_params = {}) ⇒ Object



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

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

#snapshot_products(api_params = {}) ⇒ Object



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

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

#stocks(api_params = {}) ⇒ Object



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

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

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



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

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



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

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

#suspended?Boolean

Returns:

  • (Boolean)


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

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

#template_includesObject



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

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

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



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

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



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

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