Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'active_support/core_ext/module/aliasing' | ||
|
||
module ActiveResource | ||
class Connection | ||
|
||
attr_reader :response | ||
|
||
def handle_response_with_response_capture(response) | ||
@response = handle_response_without_response_capture(response) | ||
end | ||
|
||
alias_method_chain :handle_response, :response_capture | ||
# alias_method :handle_response_without_instance, :handle_response | ||
# alias_method :handle_response, :handle_response_with_instance | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module ShopifyAPI | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
christocracy
|
||
module Limits | ||
def self.included(klass) | ||
klass.send(:extend, ClassMethods) | ||
end | ||
|
||
module ClassMethods | ||
|
||
# Takes form num_requests_executed/max_requests | ||
# Eg: 101/3000 | ||
CREDIT_LIMIT_HEADER_PARAM = { | ||
:global => 'http_x_shopify_api_call_limit', | ||
:shop => 'http_x_shopify_shop_api_call_limit' | ||
} | ||
|
||
## | ||
# How many more API calls can I make? | ||
# @return {Integer} | ||
# | ||
def credit_left | ||
shop = credit_limit(:shop) - credit_used(:shop) | ||
global = credit_limit(:global) - credit_used(:global) | ||
shop < global ? shop : global | ||
end | ||
alias_method :available_calls, :credit_left | ||
This comment has been minimized.
Sorry, something went wrong.
christocracy
|
||
|
||
## | ||
# Have I reached my API call limit? | ||
# @return {Boolean} | ||
# | ||
def credit_maxed? | ||
credit_left <= 0 | ||
end | ||
alias_method :maxed?, :credit_maxed? | ||
|
||
## | ||
# 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. | ||
# @param {Symbol} scope [:shop|:global] | ||
# @return {Integer} | ||
# | ||
def credit_limit(scope=:shop) | ||
@api_credit_limit ||= {} | ||
@api_credit_limit[scope] ||= api_credit_limit_param(scope).pop.to_i - 1 | ||
end | ||
alias_method :call_limit, :credit_limit | ||
|
||
## | ||
# How many API calls have I made? | ||
# @param {Symbol} scope [:shop|:global] | ||
# @return {Integer} | ||
# | ||
def credit_used(scope=:shop) | ||
api_credit_limit_param(scope).shift.to_i | ||
end | ||
alias_method :call_count, :credit_used | ||
|
||
## | ||
# @return {HTTPResonse} | ||
# | ||
def response | ||
Shop.current unless ShopifyAPI::Base.connection.response | ||
ShopifyAPI::Base.connection.response | ||
end | ||
|
||
private | ||
|
||
## | ||
# @return {Array} | ||
# | ||
def api_credit_limit_param(scope) | ||
response[CREDIT_LIMIT_HEADER_PARAM[scope]].split('/') | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'test_helper' | ||
require 'mocha' | ||
|
||
class LimitsTest < Test::Unit::TestCase | ||
def setup | ||
ShopifyAPI::Base.site = "test.myshopify.com" | ||
@header_hash = {'http_x_shopify_api_call_limit' => '150/3000', | ||
'http_x_shopify_shop_api_call_limit' => '100/300'} | ||
ShopifyAPI::Base.connection.expects(:response).at_least(0).returns(@header_hash) | ||
end | ||
|
||
context "Limits" do | ||
should "fetch limit total" do | ||
assert_equal(299, ShopifyAPI.credit_limit(:shop)) | ||
assert_equal(2999, ShopifyAPI.credit_limit(:global)) | ||
end | ||
|
||
should "fetch used calls" do | ||
assert_equal(100, ShopifyAPI.credit_used(:shop)) | ||
assert_equal(150, ShopifyAPI.credit_used(:global)) | ||
end | ||
|
||
should "calculate remaining calls" do | ||
assert_equal(199, ShopifyAPI.credit_left) | ||
end | ||
|
||
should "flag maxed out credits" do | ||
assert !ShopifyAPI.maxed? | ||
@header_hash = {'http_x_shopify_api_call_limit' => '2999/3000', | ||
'http_x_shopify_shop_api_call_limit' => '299/300'} | ||
ShopifyAPI::Base.connection.expects(:response).at_least(1).returns(@header_hash) | ||
assert ShopifyAPI.maxed? | ||
This comment has been minimized.
Sorry, something went wrong.
christocracy
|
||
end | ||
end | ||
|
||
|
||
end |
You just drop in my code without any acknowledgement at all?? Maybe a link in the file header to where it came from?
https://github.com/christocracy/shopify-api-limits/blob/master/lib/shopify-api-limits/shopify_api/limits.rb