Module: ShopifyAPI::Limits::ClassMethods

Defined in:
lib/shopify_api/limits.rb

Constant Summary collapse

CREDIT_LIMIT_HEADER_PARAM =

Takes form num_requests_executed/max_requests Eg: 101/3000

{
  :global => 'http_x_shopify_api_call_limit',
  :shop => 'http_x_shopify_shop_api_call_limit'
}

Instance Method Summary collapse

Instance Method Details

#credit_leftInteger Also known as: available_calls

How many more API calls can I make?

Returns:

  • (Integer)


20
21
22
23
24
# File 'lib/shopify_api/limits.rb', line 20

def credit_left
  shop = credit_limit(:shop) - credit_used(:shop)
  global = credit_limit(:global) - credit_used(:global)      
  shop < global ? shop : global
end

#credit_limit(scope = :shop) ⇒ Integer Also known as: call_limit

How many total API calls can I make? NOTE: subtracting 1 from credit_limit because I think ShopifyAPI cuts off at 299/2999 or shop/global limits.

Parameters:

  • scope (Symbol) (defaults to: :shop)
    :shop|:global

Returns:

  • (Integer)


42
43
44
45
# File 'lib/shopify_api/limits.rb', line 42

def credit_limit(scope=:shop)
  @api_credit_limit ||= {}
  @api_credit_limit[scope] ||= api_credit_limit_param(scope).pop.to_i - 1     
end

#credit_maxed?Boolean Also known as: maxed?

Have I reached my API call limit?

Returns:

  • (Boolean)


31
32
33
# File 'lib/shopify_api/limits.rb', line 31

def credit_maxed?
  credit_left <= 0
end

#credit_used(scope = :shop) ⇒ Integer Also known as: call_count

How many API calls have I made?

Parameters:

  • scope (Symbol) (defaults to: :shop)
    :shop|:global

Returns:

  • (Integer)


53
54
55
# File 'lib/shopify_api/limits.rb', line 53

def credit_used(scope=:shop)
  api_credit_limit_param(scope).shift.to_i
end

#responseHTTPResonse

Returns:

  • (HTTPResonse)


61
62
63
64
# File 'lib/shopify_api/limits.rb', line 61

def response
  Shop.current unless ShopifyAPI::Base.connection.response
  ShopifyAPI::Base.connection.response
end