shopify-kaminari 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format d
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in shopify-kaminari.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,46 @@
1
+ = shopify-kaminari
2
+
3
+ Extends ShopifyAPI with support for paginating the resources via the Kaminari gem.
4
+
5
+ == Examples
6
+
7
+ View code:
8
+
9
+ ul
10
+ - @products.each do |product|
11
+ li= product.title
12
+
13
+ = paginate @products
14
+
15
+ To paginate all the products:
16
+
17
+ @products = ShopifyAPI::Product.paginate per: 25,
18
+ page: params[:page]
19
+
20
+ To pass some parameters:
21
+
22
+ @products = ShopifyAPI::Product.paginate per: 25,
23
+ page: 1,
24
+ params: {published_status: 'published'}
25
+
26
+ == Default values
27
+
28
+ Both the :per and :page params are optional, and default to:
29
+
30
+ :per => 25
31
+ :page => 1
32
+
33
+ == Contributing to shopify-mock
34
+
35
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
36
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
37
+ * Fork the project
38
+ * Start a feature/bugfix branch
39
+ * Commit and push until you are happy with your contribution
40
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
41
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
42
+
43
+ == Copyright
44
+
45
+ Copyright (c) 2011 Travis Haynes. See LICENSE.txt for further details.
46
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :default => [:spec]
4
+
5
+ desc "runs all specs"
6
+ task :spec do
7
+ files = Dir[File.expand_path("../spec/**/*_spec.rb", __FILE__)].join(" ")
8
+ puts files
9
+ exec "bundle exec rspec #{files}"
10
+ end
@@ -0,0 +1,77 @@
1
+ require 'shopify_api'
2
+
3
+ module ShopifyAPI
4
+
5
+ # add Kaminari pagination support to ShopifyAPI
6
+ module Kaminari
7
+ @@per = 25
8
+
9
+ class << self
10
+ # get limit of rows per page
11
+ # @return [Integer] the per
12
+ # @example Get the current per per page
13
+ # current_per = ShopifyAPI::Kaminari.per
14
+ # @api public
15
+ def per
16
+ @@per
17
+ end
18
+
19
+ # set limit of rows to return per page
20
+ # @param [Integer] value The new limit per row value
21
+ # @return [Integer] the limit per row
22
+ # @example Set limit per page to 50
23
+ # ShopifyAPI::Kaminari.per = 50
24
+ # @api public
25
+ def per=(value)
26
+ @@per = value
27
+ end
28
+ end
29
+
30
+ # paginates a ShopifyAPI::Base resource
31
+ # @param [Hash, Integer] options Can be either a Hash like this: {:page => Integer, :per => Integer}, or the page number
32
+ # @return [Array] Array of ShopifyAPI resources
33
+ # @example Paginate all ShopifyAPI::Product
34
+ # @products = ShopifyAPI::Product.page :page => params[:page]
35
+ # @example Paginate per 15
36
+ # @products = ShopifyAPI::Product.page :page => params[:page], :per => 15
37
+ # @api public
38
+ def paginate(options = {})
39
+ options = {:page => options} if options.class == Fixnum
40
+ options = {} if options.nil?
41
+
42
+ # create params for pagination
43
+ params = {
44
+ :page => options[:page] || 1,
45
+ :per => options[:per] || 25
46
+ }
47
+
48
+ # remove pagination params from options
49
+ options.delete :page
50
+ options.delete :per
51
+
52
+ # merge any params from options
53
+ params = params.merge options[:params] if options.include? :params
54
+
55
+ # remove params from options
56
+ options.delete :params
57
+
58
+ # create arguments to pass to ActiveResource find
59
+ args = {:params => params}.merge options
60
+
61
+ # run the query
62
+ result = self.find :all, args
63
+
64
+ # add instance methods to result Array to support paginating with Kaminari
65
+ result.instance_eval <<-EVAL
66
+ def current_page; #{args[:params][:page]}; end
67
+ def num_pages; #{self.count / args[:params][:per] + 1}; end
68
+ def limit_value; #{args[:params][:per]}; end
69
+ EVAL
70
+
71
+ result
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ ShopifyAPI::Base.send :extend, ShopifyAPI::Kaminari
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "shopify-kaminari"
6
+ s.version = "0.0.1"
7
+ s.authors = ["Travis Haynes"]
8
+ s.email = ["travis.j.haynes@gmail.com"]
9
+ s.homepage = ""
10
+ s.summary = %q{Provides shopify_api with pagination support via Kaminari}
11
+ s.description = %q{Extends ShopifyAPI, adding support to paginate using the Kaminari gem.}
12
+
13
+ s.rubyforge_project = "shopify-kaminari"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency("shopify_api", [">= 1.2.5"])
21
+ s.add_dependency("kaminari", [">= 0.12.4"])
22
+ s.add_dependency("rspec", [">= 2.6.0"])
23
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe ShopifyAPI::Kaminari do
4
+
5
+ describe "#per" do
6
+ it "should default to 25" do
7
+ described_class.per.should eq 25
8
+ end
9
+ it "should persist a custom value" do
10
+ described_class.per.should_not eq 10
11
+ described_class.per = 10
12
+ described_class.per.should eq 10
13
+ end
14
+ end
15
+
16
+ describe "#paginate" do
17
+ it "should respond to current_page" do
18
+ results = ShopifyAPI::Test.paginate
19
+ results.should respond_to :current_page
20
+ end
21
+ it "should respond to num_pages" do
22
+ results = ShopifyAPI::Test.paginate
23
+ results.should respond_to :num_pages
24
+ end
25
+ it "should respond to limit_value" do
26
+ results = ShopifyAPI::Test.paginate
27
+ results.should respond_to :limit_value
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ require File.expand_path("../../lib/shopify-kaminari", __FILE__)
2
+
3
+ # load support files
4
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| load f}
@@ -0,0 +1,13 @@
1
+ module ShopifyAPI
2
+ class Test < Base
3
+ end
4
+ end
5
+
6
+ test_response = { "tests" => [] }.to_json
7
+
8
+ ActiveResource::HttpMock.respond_to do |mock|
9
+ mock.get "/tests.json?page=1&per=25", {}, test_response
10
+ mock.get "/tests/count.json", {}, { "count" => 0 }.to_json
11
+ end
12
+
13
+ ShopifyAPI::Base.site = "http://localhost"
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shopify-kaminari
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Travis Haynes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-23 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shopify_api
16
+ requirement: &12318260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.2.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *12318260
25
+ - !ruby/object:Gem::Dependency
26
+ name: kaminari
27
+ requirement: &12317760 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.12.4
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *12317760
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &12317280 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 2.6.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *12317280
47
+ description: Extends ShopifyAPI, adding support to paginate using the Kaminari gem.
48
+ email:
49
+ - travis.j.haynes@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - Gemfile
57
+ - README.rdoc
58
+ - Rakefile
59
+ - lib/shopify-kaminari.rb
60
+ - shopify-kaminari.gemspec
61
+ - spec/shopify-kaminari_spec.rb
62
+ - spec/spec_helper.rb
63
+ - spec/support/shopify_test_resource.rb
64
+ homepage: ''
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project: shopify-kaminari
84
+ rubygems_version: 1.8.8
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Provides shopify_api with pagination support via Kaminari
88
+ test_files: []