shopify-api-limits 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ spec/shopify_api.yml
@@ -1,10 +1,24 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
- require 'shopify-api-limits/active_resource/connection'
4
- require 'shopify-api-limits/shopify_api/shopify_api'
5
-
6
3
  module ShopifyAPI
7
4
  module Limits
5
+ # Connection hack
6
+ require 'shopify-api-limits/active_resource/connection'
7
+
8
+ require 'shopify-api-limits/shopify_api/shopify_api'
8
9
 
10
+ class Error < StandardError
11
+ def self.status_code(code = nil)
12
+ return @code unless code
13
+ @code = code
14
+ end
15
+
16
+ def status_code
17
+ self.class.status_code
18
+ end
19
+ end
20
+
21
+ class GlobalError < Error; status_code(1) ; end
22
+ class ShopError < Error; status_code(2) ; end
9
23
  end
10
24
  end
@@ -1,9 +1,11 @@
1
+ require 'active_resource'
2
+
1
3
  module ActiveResource
2
4
  class Connection
3
5
  # HACK 1: Add an attr_reader for response
4
6
  attr_reader :response
5
7
 
6
- def request(method, path, *arguments)
8
+ def request(method, path, *arguments)
7
9
  result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
8
10
  payload[:method] = method
9
11
  payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
@@ -1,46 +1,56 @@
1
1
  module ShopifyAPI
2
2
  # Takes form num_requests_executed/max_requests
3
3
  # Eg: 101/3000
4
- CALL_LIMIT_HEADER_GLOBAL = 'http_x_shopify_api_call_limit'
5
- CALL_LIMIT_HEADER_LOCAL = 'http_x_shopify_shop_api_call_limit'
4
+ CALL_LIMIT_HEADER_PARAM = {
5
+ :global => 'http_x_shopify_api_call_limit',
6
+ :shop => 'http_x_shopify_shop_api_call_limit'
7
+ }
6
8
 
7
9
  class << self
8
10
  ##
9
11
  # How many more API calls can I make?
12
+ # GLOBAL: 10/3000 2999/3000
13
+ # SHOP: 250/300 60/300
10
14
  #
11
- def available_calls
12
- call_limit - call_count
15
+ def available_calls
16
+ shop = call_limit(:shop) - call_count(:shop)
17
+ global = call_limit(:global) - call_count(:global)
18
+ shop < global ? shop : global
13
19
  end
14
20
 
15
21
  ##
16
22
  # Have I reached my API call limit?
17
23
  #
18
- def maxed_out?
19
- call_limit == call_count
24
+ def maxed?
25
+ available_calls == 0
20
26
  end
21
27
 
22
28
  ##
23
29
  # How many total API calls can I make?
30
+ # NOTE: subtracting 1 from call_limit because I think ShopifyAPI cuts off at 299/2999 or shop/global limits.
24
31
  # @return Integer
25
32
  #
26
- def call_limit
27
- @api_call_limit ||= api_call_limit_param.pop.to_i
33
+ def call_limit(scope=:shop)
34
+ @api_call_limit ||= {}
35
+ @api_call_limit[scope] ||= api_call_limit_param(scope).pop.to_i - 1
28
36
  end
29
37
 
30
38
  ##
31
39
  # How many API calls have I made?
32
40
  # @return Integer
33
- def call_count
34
- api_call_limit_param.shift.to_i
41
+ def call_count(scope=:shop)
42
+ api_call_limit_param(scope).shift.to_i
35
43
  end
36
-
37
- def api_call_limit_param
38
- response[CALL_LIMIT_HEADER_GLOBAL].split('/')
39
- end
40
-
44
+
41
45
  def response
42
46
  Shop.current unless ActiveResource::Base.connection.response
43
47
  ActiveResource::Base.connection.response
44
48
  end
49
+
50
+ private
51
+
52
+ def api_call_limit_param(scope)
53
+ response[CALL_LIMIT_HEADER_PARAM[scope]].split('/')
54
+ end
45
55
  end
46
56
  end
@@ -1,5 +1,5 @@
1
1
  module ShopifyAPI
2
2
  module Limits
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -15,6 +15,8 @@ Gem::Specification.new do |s|
15
15
  #s.rubyforge_project = "shopify-api-limits"
16
16
 
17
17
  s.add_dependency "shopify_api", ">= 1.2.2"
18
+ s.add_development_dependency "rspec", ">=2.6.0"
19
+ s.add_development_dependency "shopify_api", ">= 1.2.2"
18
20
 
19
21
  s.files = `git ls-files`.split("\n")
20
22
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler.require(:default, :development)
3
+
4
+ # Load shopify config
5
+ config = YAML.load_file(File.join(File.dirname(__FILE__), "shopify_api.yml"))
6
+
7
+ ActiveResource::Base.site = config["site"]
@@ -0,0 +1,32 @@
1
+
2
+ require './spec/boot'
3
+
4
+ describe "Limits" do
5
+ it "Can fetch local limits" do
6
+ count = ShopifyAPI.call_count :shop
7
+ limit = ShopifyAPI.call_limit :shop
8
+
9
+ (count < 300 && count > 0).should be_true
10
+ (count < limit).should be_true
11
+ ShopifyAPI.maxed?.should be_false
12
+ (ShopifyAPI.available_calls > 0).should be_true
13
+ end
14
+
15
+ it "Can fetch global limits" do
16
+ count = ShopifyAPI.call_count :global
17
+ limit = ShopifyAPI.call_limit :global
18
+
19
+ (count < 3000 && count > 0).should be_true
20
+ (count < limit).should be_true
21
+ ShopifyAPI.maxed?.should be_false
22
+ (ShopifyAPI.available_calls > 0).should be_true
23
+ end
24
+
25
+ it "Can execute up to local max" do
26
+ until ShopifyAPI.maxed?
27
+ ShopifyAPI::Shop.current
28
+ puts "avail: #{ShopifyAPI.available_calls}, maxed: #{ShopifyAPI.maxed?}"
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1 @@
1
+ site: [insert your ActiveResource::Base.site url here]
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: shopify-api-limits
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Chris Scott
@@ -24,6 +24,28 @@ dependencies:
24
24
  version: 1.2.2
25
25
  type: :runtime
26
26
  version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 2.6.0
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: shopify_api
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.2.2
47
+ type: :development
48
+ version_requirements: *id003
27
49
  description: This gem adds the ability to read shopify API call limits to the ShopifyAPI gem
28
50
  email:
29
51
  - christocracy@gmail.com
@@ -43,6 +65,9 @@ files:
43
65
  - lib/shopify-api-limits/shopify_api/shopify_api.rb
44
66
  - lib/shopify-api-limits/version.rb
45
67
  - shopify-api-limits.gemspec
68
+ - spec/boot.rb
69
+ - spec/limits.rb
70
+ - spec/shopify_api.yml.example
46
71
  has_rdoc: true
47
72
  homepage: ""
48
73
  licenses: []
@@ -71,5 +96,7 @@ rubygems_version: 1.5.0
71
96
  signing_key:
72
97
  specification_version: 3
73
98
  summary: This gem adds the ability to read shopify API call limits to the ShopifyAPI gem
74
- test_files: []
75
-
99
+ test_files:
100
+ - spec/boot.rb
101
+ - spec/limits.rb
102
+ - spec/shopify_api.yml.example