Module: Shopifydev::ShopifyAPI::ConsumeAPI

Defined in:
lib/shopifydev/shopify_api/consume_api.rb

Defined Under Namespace

Classes: Report

Class Method Summary collapse

Class Method Details

.consume(num = nil) {|report.last| ... } ⇒ Object

Yields:

  • (report.last)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/shopifydev/shopify_api/consume_api.rb', line 14

def self.consume(num=nil)
  report = []
  num ||= 1
  num = num.to_i
  report << Report.new(info: "consuming api calls until only #{num} are left...")
  yield report.last if block_given?
  res = Shydra::Resources::Product.new.run
  pids = res.data.map{|r| r['id']}
  used, max = res.headers['HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT'].split('/').map(&:to_i)
  report << Report.new(debug: "used: #{used.inspect} max: #{max.inspect}")
  yield report.last if block_given?
  h = Shydra::Hydra.new(max_concurrency: 50)
  to_consume = max - used - num - 2
  report << Report.new(debug: "queueing to_consume: #{to_consume.inspect} requests")
  yield report.last if block_given?
  if to_consume > 0
    to_consume.times{ h.queue(Shydra::Resources::Product.new(id: pids.sample)) }
    h.run
    ::ShopifyAPI::Shop.current
  end
  report << Report.new(status: "credit_left: #{::ShopifyAPI.credit_left}")
  yield report.last if block_given?

  return report
end