Module: Arctic::Vendor

Defined in:
lib/arctic/vendor.rb,
lib/arctic/vendor/api.rb,
lib/arctic/vendor/vendor.rb,
lib/arctic/vendor/version.rb

Defined Under Namespace

Classes: API

Constant Summary collapse

MAX_THREADS =
ENV.fetch('MAX_THREADS', 4).to_s.to_i.freeze
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.apiObject



26
27
28
# File 'lib/arctic/vendor/vendor.rb', line 26

def api
  @api ||= Arctic::Vendor::API.new
end

.collect_products(&block) ⇒ Object

Fetches all products from all shops, where this vendor is the source vendor and pushes them to the Core API.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/arctic/vendor/vendor.rb', line 40

def collect_products(&block)
  Arctic.logger.info "Collecting products from source vendor..."
  products_count = 0

  seconds = time do
    each_shop do |shop, |
      products = api.send_products ['id'], shop['id'], yield(shop)
      products_count += products.size
    end
  end

  Arctic.logger.info "Collected #{products_count} products in #{seconds} seconds"
end

.distribute_productsObject

Fetches all products from the Core API and distributes them to the target vendors



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/arctic/vendor/vendor.rb', line 57

def distribute_products
  Arctic.logger.info "Distributing products to target vendor..."
  products_count = 0

  seconds = time do
    each_shop(type: :target) do |shop, |
      products = api.list_products ['id'], shop['id']
      products_count += products.size
      yield shop, products
    end
  end

  Arctic.logger.info "Distributed #{products_count} products in #{seconds} seconds"
end

.each_shop(type: :source) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/arctic/vendor/vendor.rb', line 17

def each_shop(type: :source)
  api.list_accounts.each do ||
    api.list_shops(['id']).each do |shop|
      yield shop,  if shop['type'] == type.to_s
    end
  end
end

.threaded(collection, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/arctic/vendor/vendor.rb', line 5

def threaded(collection, &block)
  threads = Arctic::Vendor::MAX_THREADS.times.collect do
    Thread.new do
      while item = collection.pop do
        yield item
      end
    end
  end
  threads.compact.map &:join
end

.timeObject



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

def time
  t1 = Time.now.to_f
  yield
  Time.now.to_f - t1
end