shopify-api-limits 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,8 @@ module ShopifyAPI
4
4
  module Limits
5
5
  # Connection hack
6
6
  require 'shopify-api-limits/active_resource/connection'
7
+ require 'shopify-api-limits/active_resource/base'
8
+
7
9
  require 'shopify-api-limits/shopify_api/limits'
8
10
 
9
11
  def self.included(klass)
@@ -0,0 +1,39 @@
1
+ ##
2
+ # Redefines #find_every to automatically compose resultsets from multiple ShopifyAPI queries due to API limit of 250 records / request.
3
+ # Seemlessly stitches all requests to #all, #find(:all), etc, as if there were no LIMIT.
4
+ # @see http://wiki.shopify.com/Retrieving_more_than_250_Products%2C_Orders_etc.
5
+ #
6
+ module ActiveResource
7
+ class Base
8
+ SHOPIFY_API_MAX_LIMIT = 250
9
+
10
+ class << self
11
+ # get reference to unbound class-method #find_every
12
+ find_every = self.instance_method(:find_every)
13
+
14
+ define_method(:find_every) do |options|
15
+ options[:params] ||= {}
16
+
17
+ # Determine number of ShopifyAPI requests to stitch together all records of this query.
18
+ limit = options[:params][:limit]
19
+
20
+ # Bail out to default functionality unless limit == false
21
+ return find_every.bind(self).call(options) unless limit == false
22
+
23
+ total = count(options).to_f
24
+ options[:params][:limit] = SHOPIFY_API_MAX_LIMIT
25
+ pages = (total/SHOPIFY_API_MAX_LIMIT).ceil
26
+
27
+ # raise Limits::Error if not enough credits to retrieve entire recordset
28
+ raise ShopifyAPI::Limits::Error.new unless ShopifyAPI.credit_left >= pages
29
+
30
+ # Iterate from 1 -> max-pages and call the original #find_every, capturing the responses into one list
31
+ rs = []
32
+ 1.upto(pages) {|page|
33
+ rs.concat find_every.bind(self).call(options.update(:page => page))
34
+ }
35
+ rs
36
+ end
37
+ end
38
+ end
39
+ end
@@ -11,6 +11,6 @@ module ActiveResource
11
11
  # re-implement #handle_response to capture the returned HTTPResponse to an instance var.
12
12
  define_method(:handle_response) do |response|
13
13
  @response = handle_response.bind(self).call(response)
14
- end
15
- end
14
+ end
15
+ end
16
16
  end
@@ -1,5 +1,5 @@
1
1
  module ShopifyAPI
2
2
  module Limits
3
- VERSION = "0.0.7"
3
+ VERSION = "0.0.8"
4
4
  end
5
5
  end
File without changes
@@ -0,0 +1,23 @@
1
+ require './spec/boot'
2
+
3
+ describe "Query" do
4
+ it "Can retrieve an all query with empty params" do
5
+ rs = ShopifyAPI::Product.all
6
+ (rs.length == ShopifyAPI::Product.count).should be_true
7
+ end
8
+
9
+
10
+ it "Can respect limit param when provided" do
11
+ rs = ShopifyAPI::Product.all(:params => {:limit => 1})
12
+ puts "limit rs: #{rs.length}"
13
+ (rs.length == 1).should be_true
14
+ end
15
+
16
+ it "Can aggregate resultset by using :limit => false" do
17
+ rs = ShopifyAPI::Product.all(:params => {:limit => false})
18
+ puts "len: #{rs.length}"
19
+ (rs.length == ShopifyAPI::Product.count).should be_true
20
+ end
21
+
22
+
23
+ end
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.7
5
+ version: 0.0.8
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-16 00:00:00 -04:00
13
+ date: 2011-05-18 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -61,12 +61,14 @@ files:
61
61
  - README.rdoc
62
62
  - Rakefile
63
63
  - lib/shopify-api-limits.rb
64
+ - lib/shopify-api-limits/active_resource/base.rb
64
65
  - lib/shopify-api-limits/active_resource/connection.rb
65
66
  - lib/shopify-api-limits/shopify_api/limits.rb
66
67
  - lib/shopify-api-limits/version.rb
67
68
  - shopify-api-limits.gemspec
68
69
  - spec/boot.rb
69
- - spec/limits.rb
70
+ - spec/credits_spec.rb
71
+ - spec/query_spec.rb
70
72
  - spec/shopify_api.yml.example
71
73
  has_rdoc: true
72
74
  homepage: ""
@@ -98,5 +100,6 @@ specification_version: 3
98
100
  summary: This gem adds the ability to read shopify API call limits to the ShopifyAPI gem
99
101
  test_files:
100
102
  - spec/boot.rb
101
- - spec/limits.rb
103
+ - spec/credits_spec.rb
104
+ - spec/query_spec.rb
102
105
  - spec/shopify_api.yml.example