shopify-api-limits 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -50,4 +50,29 @@ Now it's possible to access the HTTPResponse instance directly from ActiveResour
50
50
  foo = ActiveResource::Base.connection.response['http-header-param-foo']
51
51
 
52
52
 
53
- == Installation
53
+ == Installation
54
+ gem "shopify_api"
55
+ gem "shopify-api-limits"
56
+
57
+ == Usage
58
+ count_shop = ShopifyAPI.call_count :shop
59
+ limit_shop = ShopifyAPI.call_limit :shop
60
+
61
+ count_global = ShopifyAPI.call_count :global
62
+ limit_global = ShopifyAPI.call_limit :global
63
+
64
+ Generally, you shouldn't need to use the methods above directly -- rather they're used under-the-hood by the following helpful methods which don't require a scope (:shop/:global): If the <b>:global</b> scope has fewer calls available than the <b>:local</b> scope, the methods will operate upon the <b>:global</b> scope; otherwise, values will be returned based upon the <b>:shop</b> scope.
65
+
66
+ unless ShopifyAPI.maxed?
67
+ #make some ShopifyAPI calls
68
+ end
69
+
70
+ until ShopifyAPI.maxed?
71
+ # make some ShopifyAPI calls
72
+ end
73
+
74
+ while ShopifyAPI.available_calls
75
+ # make some ShopifyAPI calls
76
+ end
77
+
78
+
@@ -22,3 +22,5 @@ module ShopifyAPI
22
22
  class ShopError < Error; status_code(2) ; end
23
23
  end
24
24
  end
25
+
26
+ ShopifyAPI.send(:include, ShopifyAPI::Limits)
@@ -2,21 +2,15 @@ require 'active_resource'
2
2
 
3
3
  module ActiveResource
4
4
  class Connection
5
- # HACK 1: Add an attr_reader for response
5
+ # HACK: Add an attr_reader for response
6
6
  attr_reader :response
7
-
8
- def request(method, path, *arguments)
9
- result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
10
- payload[:method] = method
11
- payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
12
- payload[:result] = http.send(method, path, *arguments)
13
- end
14
- # HACK 2: Save response to instance var @response
15
- @response = handle_response(result)
16
- rescue Timeout::Error => e
17
- raise TimeoutError.new(e.message)
18
- rescue OpenSSL::SSL::SSLError => e
19
- raise SSLError.new(e.message)
20
- end
7
+
8
+ # capture the original #handle_response as unbound method instead of using alias
9
+ handle_response = self.instance_method(:handle_response)
10
+
11
+ # re-implement #handle_response to capture the returned HTTPResponse to an instance var.
12
+ define_method(:handle_response) do |response|
13
+ @response = handle_response.bind(self).call(response)
14
+ end
21
15
  end
22
16
  end
@@ -1,16 +1,22 @@
1
1
  module ShopifyAPI
2
- # Takes form num_requests_executed/max_requests
3
- # Eg: 101/3000
4
- CALL_LIMIT_HEADER_PARAM = {
5
- :global => 'http_x_shopify_api_call_limit',
6
- :shop => 'http_x_shopify_shop_api_call_limit'
7
- }
2
+ module Limits
3
+ def self.included(klass)
4
+ klass.send(:extend, ClassMethods)
5
+ end
6
+ end
8
7
 
9
- class << self
10
- ##
8
+ module ClassMethods
9
+
10
+ # Takes form num_requests_executed/max_requests
11
+ # Eg: 101/3000
12
+ CALL_LIMIT_HEADER_PARAM = {
13
+ :global => 'http_x_shopify_api_call_limit',
14
+ :shop => 'http_x_shopify_shop_api_call_limit'
15
+ }
16
+
17
+ ##
11
18
  # How many more API calls can I make?
12
- # GLOBAL: 10/3000 2999/3000
13
- # SHOP: 250/300 60/300
19
+ # @return {Integer}
14
20
  #
15
21
  def available_calls
16
22
  shop = call_limit(:shop) - call_count(:shop)
@@ -20,6 +26,7 @@ module ShopifyAPI
20
26
 
21
27
  ##
22
28
  # Have I reached my API call limit?
29
+ # @return {Boolean}
23
30
  #
24
31
  def maxed?
25
32
  available_calls == 0
@@ -28,7 +35,8 @@ module ShopifyAPI
28
35
  ##
29
36
  # How many total API calls can I make?
30
37
  # NOTE: subtracting 1 from call_limit because I think ShopifyAPI cuts off at 299/2999 or shop/global limits.
31
- # @return Integer
38
+ # @param {Symbol} scope [:shop|:global]
39
+ # @return {Integer}
32
40
  #
33
41
  def call_limit(scope=:shop)
34
42
  @api_call_limit ||= {}
@@ -37,20 +45,28 @@ module ShopifyAPI
37
45
 
38
46
  ##
39
47
  # How many API calls have I made?
40
- # @return Integer
48
+ # @param {Symbol} scope [:shop|:global]
49
+ # @return {Integer}
50
+ #
41
51
  def call_count(scope=:shop)
42
52
  api_call_limit_param(scope).shift.to_i
43
53
  end
44
-
54
+
55
+ ##
56
+ # @return {HTTPResonse}
57
+ #
45
58
  def response
46
59
  Shop.current unless ActiveResource::Base.connection.response
47
60
  ActiveResource::Base.connection.response
48
61
  end
49
-
62
+
50
63
  private
51
-
64
+
65
+ ##
66
+ # @return {Array}
67
+ #
52
68
  def api_call_limit_param(scope)
53
69
  response[CALL_LIMIT_HEADER_PARAM[scope]].split('/')
54
70
  end
55
- end
71
+ end
56
72
  end
@@ -1,5 +1,5 @@
1
1
  module ShopifyAPI
2
2
  module Limits
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -1 +1 @@
1
- site: [insert your ActiveResource::Base.site url here]
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.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Chris Scott
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-15 00:00:00 -04:00
13
+ date: 2011-05-16 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency