shopify-api-limits 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.rdoc +53 -0
- data/Rakefile +2 -0
- data/lib/shopify-api-limits.rb +8 -0
- data/lib/shopify-api-limits/active_resource/connection.rb +20 -0
- data/lib/shopify-api-limits/shopify_api/shopify_api.rb +46 -0
- data/lib/shopify-api-limits/version.rb +5 -0
- data/shopify-api-limits.gemspec +23 -0
- metadata +75 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
= shopify-api-limits
|
2
|
+
|
3
|
+
My friend Dave (aka "hunkybill") posted a problem to me one day about ShopifyAPI call limits, offering a case of beer if I could find a solution:
|
4
|
+
http://forums.shopify.com/categories/9/posts/49003
|
5
|
+
|
6
|
+
So in the HTTP headers, the ShopifyAPI will return to you in each API request how many calls you've made, as well as the maximum number of calls available.
|
7
|
+
|
8
|
+
== Problem
|
9
|
+
ActiveResource does not make it easy to read the HTTP response headers, since the method #request in ActiveResource::Connection does not save a reference to the HTTP response:
|
10
|
+
|
11
|
+
# Makes a request to the remote service.
|
12
|
+
def request(method, path, *arguments)
|
13
|
+
result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
|
14
|
+
payload[:method] = method
|
15
|
+
payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
|
16
|
+
payload[:result] = http.send(method, path, *arguments)
|
17
|
+
end
|
18
|
+
handle_response(result) # <------------------- right here: handle_response return an instance of HTTPResponse but doesn't save a ref to it
|
19
|
+
rescue Timeout::Error => e
|
20
|
+
raise TimeoutError.new(e.message)
|
21
|
+
rescue OpenSSL::SSL::SSLError => e
|
22
|
+
raise SSLError.new(e.message)
|
23
|
+
end
|
24
|
+
|
25
|
+
== Solution
|
26
|
+
Hack ActiveResource::Connection to introduce a new attr_reader :response and saved the returned HTTPResponse instance to it.
|
27
|
+
|
28
|
+
class ActiveResource
|
29
|
+
class Connection
|
30
|
+
# HACK 1: Add an attr_reader for response
|
31
|
+
attr_reader :response
|
32
|
+
|
33
|
+
def request(method, path, *arguments)
|
34
|
+
result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
|
35
|
+
payload[:method] = method
|
36
|
+
payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
|
37
|
+
payload[:result] = http.send(method, path, *arguments)
|
38
|
+
end
|
39
|
+
# HACK 2: Save response to instance var @response
|
40
|
+
@response = handle_response(result)
|
41
|
+
rescue Timeout::Error => e
|
42
|
+
raise TimeoutError.new(e.message)
|
43
|
+
rescue OpenSSL::SSL::SSLError => e
|
44
|
+
raise SSLError.new(e.message)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
Now it's possible to access the HTTPResponse instance directly from ActiveResource, via:
|
50
|
+
foo = ActiveResource::Base.connection.response['http-header-param-foo']
|
51
|
+
|
52
|
+
|
53
|
+
== Installation
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
class ActiveResource
|
2
|
+
class Connection
|
3
|
+
# HACK 1: Add an attr_reader for response
|
4
|
+
attr_reader :response
|
5
|
+
|
6
|
+
def request(method, path, *arguments)
|
7
|
+
result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
|
8
|
+
payload[:method] = method
|
9
|
+
payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
|
10
|
+
payload[:result] = http.send(method, path, *arguments)
|
11
|
+
end
|
12
|
+
# HACK 2: Save response to instance var @response
|
13
|
+
@response = handle_response(result)
|
14
|
+
rescue Timeout::Error => e
|
15
|
+
raise TimeoutError.new(e.message)
|
16
|
+
rescue OpenSSL::SSL::SSLError => e
|
17
|
+
raise SSLError.new(e.message)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ShopifyAPI
|
2
|
+
# Takes form num_requests_executed/max_requests
|
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'
|
6
|
+
|
7
|
+
class << self
|
8
|
+
##
|
9
|
+
# How many more API calls can I make?
|
10
|
+
#
|
11
|
+
def available_calls
|
12
|
+
call_limit - call_count
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# Have I reached my API call limit?
|
17
|
+
#
|
18
|
+
def maxed_out?
|
19
|
+
call_limit == call_count
|
20
|
+
end
|
21
|
+
|
22
|
+
##
|
23
|
+
# How many total API calls can I make?
|
24
|
+
# @return Integer
|
25
|
+
#
|
26
|
+
def call_limit
|
27
|
+
@api_call_limit ||= api_call_limit_param.pop.to_i
|
28
|
+
end
|
29
|
+
|
30
|
+
##
|
31
|
+
# How many API calls have I made?
|
32
|
+
# @return Integer
|
33
|
+
def call_count
|
34
|
+
api_call_limit_param.shift.to_i
|
35
|
+
end
|
36
|
+
|
37
|
+
def api_call_limit_param
|
38
|
+
response[CALL_LIMIT_HEADER_GLOBAL].split('/')
|
39
|
+
end
|
40
|
+
|
41
|
+
def response
|
42
|
+
Shop.current unless ActiveResource::Base.connection.response
|
43
|
+
ActiveResource::Base.connection.response
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "shopify-api-limits/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "shopify-api-limits"
|
7
|
+
s.version = ShopifyAPI::Limits::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Chris Scott"]
|
10
|
+
s.email = ["christocracy@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{This gem adds the ability to read shopify API call limits to the ShopifyAPI gem}
|
13
|
+
s.description = %q{This gem adds the ability to read shopify API call limits to the ShopifyAPI gem}
|
14
|
+
|
15
|
+
#s.rubyforge_project = "shopify-api-limits"
|
16
|
+
|
17
|
+
s.add_dependency "shopify_api", ">= 1.2.3"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shopify-api-limits
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Scott
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-15 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: shopify_api
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 1.2.3
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
description: This gem adds the ability to read shopify API call limits to the ShopifyAPI gem
|
28
|
+
email:
|
29
|
+
- christocracy@gmail.com
|
30
|
+
executables: []
|
31
|
+
|
32
|
+
extensions: []
|
33
|
+
|
34
|
+
extra_rdoc_files: []
|
35
|
+
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- README.rdoc
|
40
|
+
- Rakefile
|
41
|
+
- lib/shopify-api-limits.rb
|
42
|
+
- lib/shopify-api-limits/active_resource/connection.rb
|
43
|
+
- lib/shopify-api-limits/shopify_api/shopify_api.rb
|
44
|
+
- lib/shopify-api-limits/version.rb
|
45
|
+
- shopify-api-limits.gemspec
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: ""
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.5.0
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: This gem adds the ability to read shopify API call limits to the ShopifyAPI gem
|
74
|
+
test_files: []
|
75
|
+
|