grape-kaminari 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b08eab293223b2ad7369fe8c005894a97d6a674a
4
- data.tar.gz: bb86d66fc3b9e8b013d9c34f0de183bcdb84a01b
3
+ metadata.gz: 4fb24c245069275a376233ac2b1ab9ea14bacf83
4
+ data.tar.gz: 1f74c3dd9df09d1bc3f015e23dd2f2289239eed2
5
5
  SHA512:
6
- metadata.gz: e293e4e23a6d4ba7c3e024fa0112c2c012add6efe86ec578a581887a40a9b42b6c8776d9b19f30790ae0a447898860faf6c3595b55e552d53c63e322db93e0c4
7
- data.tar.gz: 0c1330a4d198b4221c7c5a88924dabb678c467055afb118bdd3b3087b98316178d8eb37a4900be6890609503dc05aa57f4a67f3c18c38bdc10cc9a8e525b176f
6
+ metadata.gz: 3f2fbb8ce000c0938fe14adc08e7c646bae1be1f2341fba2eff44e21e59133766db1152ceb873ea482d62ea17992a0eb8c7bdd381935b1c952999e778520fb6d
7
+ data.tar.gz: 8d87799ba47f32584ed935f7f1f8db84ed71fbb8d4b92c937c8370570ab0aa577309268957eeb23fe3baa9b894211001f2ee74a77b7c8a54c5d9c44b1f133280
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Grape::Kaminari [![Gem Version](https://badge.fury.io/rb/grape-kaminari.png)](http://badge.fury.io/rb/grape-kaminari)
1
+ # Grape::Kaminari [![Gem Version](https://badge.fury.io/rb/grape-kaminari.png)](http://badge.fury.io/rb/grape-kaminari) [![Circle CI](https://circleci.com/gh/monterail/grape-kaminari.png?style=shield)](https://circleci.com/gh/monterail/grape-kaminari)
2
2
 
3
3
  [kaminari](https://github.com/amatsuda/kaminari) paginator integration for [grape](https://github.com/intridea/grape) API framework.
4
4
 
@@ -33,11 +33,16 @@ class MyApi < Grape::API
33
33
  desc 'Return a list of posts.'
34
34
 
35
35
  # Annotate action with `paginate`.
36
- # This will add two optional params: page and per_page
36
+ # This will add three optional params: page, per_page, and offset
37
+ #
37
38
  # You can optionally overwrite the default :per_page setting (10)
38
39
  # and the :max_per_page(false/disabled) setting which will use a validator to
39
40
  # check that per_page is below the given number.
40
- paginate :per_page => 20, :max_per_page => 30
41
+ #
42
+ # You can disable the offset parameter from appearing in the API
43
+ # documentation by setting it to false.
44
+ #
45
+ paginate per_page: 20, max_per_page: 30, offset: 5
41
46
 
42
47
  get do
43
48
  posts = Post.where(...)
@@ -46,6 +51,14 @@ class MyApi < Grape::API
46
51
  # with arguments automatically passed from params
47
52
  paginate(posts)
48
53
  end
54
+
55
+ get do
56
+ things = ['a', 'standard', 'array', 'of', 'things', '...']
57
+
58
+ # Use `Kaminari.paginate_array` method to convert the array
59
+ # into an object that can be passed to `paginate` helper.
60
+ paginate(Kaminari.paginate_array(things))
61
+ end
49
62
  end
50
63
  end
51
64
  ```
data/Rakefile CHANGED
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ task default: :spec
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.3"
26
26
  spec.add_development_dependency "rake"
27
+ spec.add_development_dependency 'rspec', '~> 2.9'
27
28
  end
@@ -23,7 +23,8 @@ module Grape
23
23
  def self.paginate(options = {})
24
24
  options.reverse_merge!(
25
25
  per_page: ::Kaminari.config.default_per_page || 10,
26
- max_per_page: ::Kaminari.config.max_per_page
26
+ max_per_page: ::Kaminari.config.max_per_page,
27
+ offset: 0
27
28
  )
28
29
  params do
29
30
  optional :page, type: Integer, default: 1,
@@ -31,7 +32,10 @@ module Grape
31
32
  optional :per_page, type: Integer, default: options[:per_page],
32
33
  desc: 'Number of results to return per page.',
33
34
  max_value: options[:max_per_page]
34
- optional :offset, type: Integer, default: 0
35
+ if options[:offset].is_a? Numeric
36
+ optional :offset, type: Integer, default: options[:offset],
37
+ desc: 'Pad a number of results.'
38
+ end
35
39
  end
36
40
  end
37
41
  end
@@ -6,7 +6,11 @@ module Grape
6
6
 
7
7
  attr = params[attr_name]
8
8
  if attr && @option && attr > @option
9
- raise Grape::Exceptions::Validation, param: @scope.full_name(attr_name), message: "must be less than #{@option}"
9
+ if Gem::Version.new(Grape::VERSION) >= Gem::Version.new('0.9.0')
10
+ raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "must be less than #{@option}"
11
+ else
12
+ raise Grape::Exceptions::Validation, param: @scope.full_name(attr_name), message: "must be less than #{@option}"
13
+ end
10
14
  end
11
15
  end
12
16
  end
@@ -1,5 +1,5 @@
1
1
  module Grape
2
2
  module Kaminari
3
- VERSION = "0.1.5"
3
+ VERSION = "0.1.6"
4
4
  end
5
5
  end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ class UnPaginatedAPI < Grape::API
4
+ # Intentionally not including Grape::Kaminari
5
+ end
6
+
7
+ class PaginatedAPI < Grape::API
8
+ include Grape::Kaminari
9
+ end
10
+
11
+ describe Grape::Kaminari do
12
+
13
+ describe 'unpaginated api' do
14
+ subject { Class.new(UnPaginatedAPI) }
15
+
16
+ it 'raises an error' do
17
+ expect { subject.paginate }.to raise_error(NoMethodError, /undefined method `paginate' for/i)
18
+ end
19
+ end
20
+
21
+ describe 'default paginated api' do
22
+ subject { Class.new(PaginatedAPI) }
23
+
24
+ it 'adds to declared parameters' do
25
+ subject.paginate
26
+ expect(subject.settings[:declared_params]).to eq([:page, :per_page, :offset])
27
+ end
28
+
29
+ describe 'descriptions, validation, and defaults' do
30
+ before do
31
+ subject.paginate
32
+ subject.get '/' do; end
33
+ end
34
+ let(:params) {subject.routes.first.route_params}
35
+
36
+ it 'does not require :page' do
37
+ expect(params['page'][:required]).to eq(false)
38
+ end
39
+
40
+ it 'does not require :per_page' do
41
+ expect(params['per_page'][:required]).to eq(false)
42
+ end
43
+
44
+ it 'does not require :offset' do
45
+ expect(params['offset'][:required]).to eq(false)
46
+ end
47
+
48
+ it 'describes :page' do
49
+ expect(params['page'][:desc]).to eq('Page offset to fetch.')
50
+ end
51
+
52
+ it 'describes :per_page' do
53
+ expect(params['per_page'][:desc]).to eq('Number of results to return per page.')
54
+ end
55
+
56
+ it 'describes :offset' do
57
+ expect(params['offset'][:desc]).to eq('Pad a number of results.')
58
+ end
59
+
60
+ it 'validates :page as Integer' do
61
+ expect(params['page'][:type]).to eq('Integer')
62
+ end
63
+
64
+ it 'validates :per_page as Integer' do
65
+ expect(params['per_page'][:type]).to eq('Integer')
66
+ end
67
+
68
+ it 'validates :offset as Integer' do
69
+ expect(params['offset'][:type]).to eq('Integer')
70
+ end
71
+
72
+ it 'defaults :page to 1' do
73
+ expect(params['page'][:default]).to eq(1)
74
+ end
75
+
76
+ it 'defaults :per_page to Kaminari.config.default_per_page' do
77
+ expect(params['per_page'][:default]).to eq(::Kaminari.config.default_per_page)
78
+ end
79
+
80
+ it 'defaults :offset to 0' do
81
+ expect(params['offset'][:default]).to eq(0)
82
+ end
83
+
84
+
85
+ end
86
+
87
+ end
88
+
89
+ describe 'custom paginated api' do
90
+ subject { Class.new(PaginatedAPI) }
91
+ def app; subject; end
92
+
93
+ before do
94
+ subject.paginate per_page:99, max_per_page: 999, offset: 9
95
+ subject.get '/' do; end
96
+ end
97
+ let(:params) {subject.routes.first.route_params}
98
+
99
+ it 'defaults :per_page to customized value' do
100
+ expect(params['per_page'][:default]).to eq(99)
101
+ end
102
+
103
+ it 'succeeds when :per_page is within :max_value' do
104
+ get('/', page: 1, per_page: 999)
105
+ expect(last_response.status).to eq 200
106
+ end
107
+
108
+ it 'ensures :per_page is within :max_value' do
109
+ get('/', page: 1, per_page: 1_000)
110
+ expect(last_response.status).to eq 400
111
+ expect(last_response.body).to match /per_page must be less than 999/
112
+ end
113
+
114
+ it 'defaults :offset to customized value' do
115
+ expect(params['offset'][:default]).to eq(9)
116
+ end
117
+
118
+ end
119
+
120
+ describe 'paginated api without :offset' do
121
+ subject { Class.new(PaginatedAPI) }
122
+
123
+ it 'excludes :offset from declared params' do
124
+ subject.paginate offset: false
125
+ expect(subject.settings[:declared_params]).not_to include(:offset)
126
+ end
127
+
128
+ end
129
+
130
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ class PaginatedAPI < Grape::API
4
+ include Grape::Kaminari
5
+
6
+ paginate
7
+ get '' do
8
+ paginate(Kaminari.paginate_array((1..10).to_a))
9
+ end
10
+
11
+ paginate offset: false
12
+ get 'no-offset' do
13
+ paginate(Kaminari.paginate_array((1..10).to_a))
14
+ end
15
+ end
16
+
17
+ describe Grape::Kaminari do
18
+ subject { PaginatedAPI.new }
19
+ def app; subject; end
20
+ let(:json) { JSON.parse(last_response.body) }
21
+ let(:header) { last_response.header }
22
+
23
+ describe 'paginated helper' do
24
+
25
+ it 'returns the first page' do
26
+ get '/', page: 1, per_page: 3
27
+ expect(json).to eq [1, 2, 3]
28
+ end
29
+
30
+ it 'returns the second page' do
31
+ get '/', page: 2, per_page: 3
32
+ expect(json).to eq [4, 5, 6]
33
+ end
34
+
35
+ # This is here to ensure that Kaminari can handle `padding(false)`
36
+ # and still do the right thing.
37
+ it 'works when offset is false' do
38
+ get '/no-offset', page: 1, per_page: 3
39
+ expect(json).to eq [1, 2, 3]
40
+ end
41
+
42
+ it 'sets headers' do
43
+ get '/', page: 3, per_page: 2, offset: 1
44
+ expect(header['X-Total']).to eq '10'
45
+ expect(header['X-Total-Pages']).to eq '5'
46
+ expect(header['X-Per-Page']).to eq '2'
47
+ expect(header['X-Page']).to eq '3'
48
+ expect(header['X-Next-Page']).to eq '4'
49
+ expect(header['X-Prev-Page']).to eq '2'
50
+ expect(header['X-Offset']).to eq '1'
51
+ end
52
+
53
+ end
54
+
55
+
56
+ end
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'kaminari'
5
+ require 'grape'
6
+ require 'grape-kaminari'
7
+ require 'rack/test'
8
+
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir['#{File.dirname(__FILE__)}/support/**/*.rb'].each {|f| require f}
12
+
13
+ I18n.enforce_available_locales = false
14
+
15
+ RSpec.configure do |config|
16
+ config.include Rack::Test::Methods
17
+ config.treat_symbols_as_metadata_keys_with_true_values = true
18
+ config.order = 'random'
19
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-kaminari
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tymon Tobolski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-23 00:00:00.000000000 Z
11
+ date: 2014-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '2.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '2.9'
69
83
  description: kaminari paginator integration for grape API framework
70
84
  email:
71
85
  - tymon.tobolski@monterail.com
@@ -83,6 +97,9 @@ files:
83
97
  - lib/grape/kaminari.rb
84
98
  - lib/grape/kaminari/max_value_validator.rb
85
99
  - lib/grape/kaminari/version.rb
100
+ - spec/kaminari_spec.rb
101
+ - spec/paginate_helper_spec.rb
102
+ - spec/spec_helper.rb
86
103
  homepage: ''
87
104
  licenses:
88
105
  - MIT
@@ -107,4 +124,7 @@ rubygems_version: 2.1.11
107
124
  signing_key:
108
125
  specification_version: 4
109
126
  summary: kaminari integration for grape
110
- test_files: []
127
+ test_files:
128
+ - spec/kaminari_spec.rb
129
+ - spec/paginate_helper_spec.rb
130
+ - spec/spec_helper.rb