class Arctic::Vendor::API

Attributes

connection[R]
token[R]
vendor_id[R]

Public Class Methods

new(**options) click to toggle source
# 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

Public Instance Methods

list_products(shop_id, **params) { |products| ... } click to toggle source

Retrieve products from the Core API

# 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_shops() click to toggle source

List shops for a single account

# File lib/arctic/vendor/api.rb, line 31
def list_shops
  make_request :get, "shops"
end
send_currencies(shop_id, currencies) click to toggle source

Send products to the Core API

# 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) click to toggle source

Send products to the Core API

# 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) click to toggle source

Marks the shop as synchronized by the vendor

# 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) click to toggle source
# 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

Private Instance Methods

make_paginated_request(method, path, body: {}, params: {}) { |make_request method, path, body: body, params: params| ... } click to toggle source
# File lib/arctic/vendor/api.rb, line 117
def make_paginated_request(method, path, body: {}, params: {})
  response = raw_request :head, path, body: body, params: params
  Arctic.logger.debug "Pagination response headers: #{response.headers}"

  page = response.headers['page'] || 1
  per_page = response.headers['per-page'] || 1
  total_record = response.headers['total'] || 1
  pages = (total_record.to_f / per_page.to_f).ceil
  collection = (1..pages).to_a

  Arctic::Vendor.threaded collection do |n|
    params = params.merge page: n
    yield make_request method, path, body: body, params: params
  end
end
make_request(method, path, body: {}, params: {}) click to toggle source
# File lib/arctic/vendor/api.rb, line 101
def make_request(method, path, body: {}, params: {})
  response = raw_request method, path, body: body, params: params

  json = begin
    JSON.parse(response.body)
  rescue JSON::ParserError
    {}
  end

  raise json['error'] if json.is_a?(Hash) && json['error']

  Arctic.logger.info "#{method.to_s.upcase} #{path}?#{params.to_query}: #{response.status}"

  json
end
raw_request(method, path, body: {}, params: {}) click to toggle source
# File lib/arctic/vendor/api.rb, line 86
def raw_request(method, path, body: {}, params: {})
  # Remove preceeding slash to avoid going from base url /v1 to /
  path = path.reverse.chomp('/').reverse

  response = connection.public_send(method, path) do |r|
    if params.any?
      params.each do |k, v|
        r.params[k.to_s] = v
      end
    end

    r.body = body.to_json if body.any?
  end
end