Class: Arctic::Vendor::API

Inherits:
Object
  • Object
show all
Defined in:
lib/arctic/vendor/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ API

Returns a new instance of API.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/arctic/vendor/api.rb', line 10

def initialize(**options)
  @vendor_id = options.fetch(:vendor_id, ENV.fetch('VENDOR_ID'))

  @token = options.fetch(:token, ENV.fetch('ARCTIC_CORE_API_TOKEN'))

  api_url = options.fetch(:url,
    ENV.fetch('ARCTIC_CORE_API_URL',
      'http://localhost:5000/v1/vendors'))

  headers = {
    'Content-Type': 'application/json',
    Accept: 'application/json',
  }

  @connection = Faraday.new url: api_url.chomp('/'), headers: headers do |conn|
    conn.basic_auth(vendor_id, token)
    conn.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/arctic/vendor/api.rb', line 8

def connection
  @connection
end

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/arctic/vendor/api.rb', line 8

def token
  @token
end

#vendor_idObject (readonly)

Returns the value of attribute vendor_id.



8
9
10
# File 'lib/arctic/vendor/api.rb', line 8

def vendor_id
  @vendor_id
end

Instance Method Details

#list_products(shop_id, **params) ⇒ Object

Retrieve products from the Core API



61
62
63
64
65
66
# File 'lib/arctic/vendor/api.rb', line 61

def list_products(shop_id, **params)
  params[:per_page] = params.delete(:batch_size) || 100
  make_paginated_request(:get, "shops/#{shop_id}/products", params: params) do |products|
    yield products
  end
end

#list_shopsObject

List shops for a single account



31
32
33
# File 'lib/arctic/vendor/api.rb', line 31

def list_shops
  make_request :get, "shops"
end

#send_currencies(shop_id, currencies) ⇒ Object

Send products to the Core API



49
50
51
52
53
54
55
56
57
58
# File 'lib/arctic/vendor/api.rb', line 49

def send_currencies(shop_id, currencies)
  Arctic::Vendor.threaded(currencies.dup) do |curr|
    begin
      make_request :put, "shops/#{shop_id}/currencies", body: curr
    rescue => e
      Arctic.logger.error "Failed to send currency (#{e.class}): #{e.message} -- #{curr}"
    end
  end
  currencies
end

#send_products(shop_id, products) ⇒ Object

Send products to the Core API



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/arctic/vendor/api.rb', line 36

def send_products(shop_id, products)
  Arctic::Vendor.threaded(products.dup) do |prod|
    begin
      make_request :post, "shops/#{shop_id}/products", body: prod
    rescue => e
      Arctic.logger.error "Failed to send product (#{e.class}): #{e.message} -- #{prod}"
    end
  end
  make_request :put, "shops/#{shop_id}", params: { collected_at: Time.now.to_s(:db) }
  products
end

#update_product(shop_id, sku, **params) ⇒ Object

Marks the shop as synchronized by the vendor



69
70
71
72
# File 'lib/arctic/vendor/api.rb', line 69

def update_product(shop_id, sku, **params)
  Arctic.logger.debug "Updating Product(#{sku}).."
  make_request :patch, "shops/#{shop_id}/products/#{sku}", params: params
end

#update_products(shop_id, products, **params) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/arctic/vendor/api.rb', line 74

def update_products(shop_id, products, **params)
  Arctic::Vendor.threaded(products) do |prod|
    begin
      update_product shop_id, prod.fetch('sku'), **params
    rescue KeyError => e
      Arctic.logger.error "Invalid product: #{e.message} -- #{prod}"
    end
  end
end