napa_pagination 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e016ee576bf219579c7fe3657e17dbc6d21daa8
4
+ data.tar.gz: 9cf55ebabf41fc5548ce881af3fb0ddd07ada7df
5
+ SHA512:
6
+ metadata.gz: 70d76059dd628eecc843cfb5b4171710a6e811107d19b0115a4e8fb30f432b9340cb0baeaab1d7a8635549a4c42c5bd5cbca5f86434a7f14deb20fb7e00235fe
7
+ data.tar.gz: b31a2204b58fae09917da894a9973b97aa70f27fa8d8dc5403e9330638a0fc29d10fb9eadc67d21392c3535fdb26a3cef84d7d9e90342eaa3101acc2cf21eb54
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ .ruby-gemset
20
+ .rspec
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ ---
2
+ language: ruby
3
+ cache:
4
+ - bundler
5
+ - apt
6
+ rvm:
7
+ - ruby-2.0.0
8
+
9
+ script: "bundle exec rspec spec --color"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in napa_pagination.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Bellycard Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ "Napa", and the Napa logo are registered trademarks of Bellycard Inc. All rights reserved.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # NapaPagination
2
+
3
+ A simple pagination representer for Napa
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'napa_pagination'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install napa_pagination
18
+
19
+ ## Usage
20
+ Once you have the gem installed, simply replace `represent` with the `paginate` when returning the response from your API. Below is an example based on the Napa Quickstart Guide.
21
+
22
+ ```ruby
23
+ class PeopleApi < Grape::API
24
+ desc 'Get a list of people'
25
+ params do
26
+ optional :ids, type: String, desc: 'comma separated person ids'
27
+ end
28
+ get do
29
+ people = Person.filter(declared(params, include_missing: false))
30
+ # represent people, with: PersonRepresenter
31
+ paginate people, with: PersonRepresenter
32
+ end
33
+
34
+ ...
35
+ end
36
+ ```
37
+
38
+ By default, this will default to 25 results per page. If you want to change that you can pass in an override in the params block:
39
+
40
+ ```ruby
41
+ class PeopleApi < Grape::API
42
+ desc 'Get a list of people'
43
+ params do
44
+ optional :ids, type: String, desc: 'comma separated person ids'
45
+ optional :per_page, type: Integer, desc: 'Results per page', default: 100
46
+ end
47
+ get do
48
+ people = Person.filter(declared(params, include_missing: false))
49
+ # represent people, with: PersonRepresenter
50
+ paginate people, with: PersonRepresenter
51
+ end
52
+
53
+ ...
54
+ end
55
+ ```
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it ( http://github.com/<my-github-username>/napa_pagination/fork )
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,33 @@
1
+ module NapaPagination
2
+ module GrapeHelpers
3
+ def paginate(data, with: nil, **args)
4
+ raise ArgumentError.new(":with option is required") if with.nil?
5
+
6
+ if data.respond_to?(:to_a)
7
+ return {}.tap do |r|
8
+ r[:data] = data.map{ |item| with.new(item).to_hash(args) }
9
+ r[:pagination] = NapaPagination::Pagination.new(represent_pagination(data)).to_h
10
+ end
11
+ else
12
+ return { data: with.new(data).to_hash(args)}
13
+ end
14
+ end
15
+
16
+ def represent_pagination(data)
17
+ # don't paginate if collection is already paginated
18
+ return data if data.respond_to?(:total_count)
19
+
20
+ page = params.try(:page) || 1
21
+ per_page = params.try(:per_page) || 25
22
+
23
+ if data.is_a?(Array)
24
+ Kaminari.paginate_array(data).page(page).per(per_page)
25
+ else
26
+ data.page(page).per(per_page)
27
+ end
28
+ end
29
+
30
+ # extend all endpoints to include this
31
+ Grape::Endpoint.send :include, self
32
+ end
33
+ end
@@ -0,0 +1,20 @@
1
+ module NapaPagination
2
+ class Pagination
3
+ def initialize(object)
4
+ @object = object
5
+ end
6
+
7
+ def to_json(options = {})
8
+ to_h.to_json(options)
9
+ end
10
+
11
+ def to_h
12
+ {}.tap do |p|
13
+ p[:page] = @object.current_page if @object.respond_to?(:current_page)
14
+ p[:per_page] = @object.limit_value if @object.respond_to?(:limit_value)
15
+ p[:total_pages] = @object.total_pages if @object.respond_to?(:total_pages)
16
+ p[:total_count] = @object.total_count if @object.respond_to?(:total_count)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module NapaPagination
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'grape'
2
+ require 'napa'
3
+ require 'kaminari/grape'
4
+
5
+ require 'napa_pagination/version'
6
+ require 'napa_pagination/pagination'
7
+ require 'napa_pagination/grape_helpers'
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'napa_pagination/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "napa_pagination"
8
+ spec.version = NapaPagination::VERSION
9
+ spec.authors = ["Darby Frey"]
10
+ spec.email = ["darby@bellycard.com"]
11
+ spec.summary = %q{A simple pagination representer for Napa}
12
+ spec.description = %q{A simple pagination representer for Napa}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'grape'
22
+ spec.add_dependency 'napa'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency 'rspec'
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'activerecord'
29
+ spec.add_development_dependency 'sqlite3'
30
+ spec.add_development_dependency 'kaminari'
31
+ spec.add_development_dependency 'actionview'
32
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+ require 'napa_pagination/grape_helpers'
3
+
4
+ class FooApi < Grape::API; end
5
+ class FooRepresenter < Napa::Representer; end
6
+
7
+ describe NapaPagination::GrapeHelpers do
8
+ before do
9
+ @endpoint = Grape::Endpoint.new(nil, {path: '/test', method: :get})
10
+ end
11
+
12
+ context '#represent_pagination' do
13
+ before do
14
+ Foo.create(word: 'bar')
15
+ Foo.create(word: 'baz')
16
+ end
17
+
18
+ after do
19
+ Foo.destroy_all
20
+ end
21
+
22
+ it 'returns the collection if it is already paginated' do
23
+ objects = Kaminari.paginate_array([Foo.new, Foo.new, Foo.new]).page(1)
24
+ output = @endpoint.represent_pagination(objects)
25
+
26
+ expect(output.class).to eq(objects.class)
27
+ end
28
+
29
+ it 'returns a paginated collection if given an array' do
30
+ output = @endpoint.represent_pagination(Foo.all.page(1))
31
+
32
+ expect(output.class).to be(Foo::ActiveRecord_Relation)
33
+ expect(output.total_pages).to be(1)
34
+ expect(output.total_count).to be(2)
35
+ end
36
+
37
+ it 'returns a paginated collection if given an ActiveRecord_Relation' do
38
+ output = @endpoint.represent_pagination(Foo.all)
39
+
40
+ expect(output.total_count).to be(2)
41
+ expect(output.total_pages).to be(1)
42
+ end
43
+
44
+ it 'overrides the page and per_page defaults if supplied as params' do
45
+ allow(@endpoint).to receive_message_chain(:params, :page).and_return(2)
46
+ allow(@endpoint).to receive_message_chain(:params, :per_page).and_return(1)
47
+
48
+ output = @endpoint.represent_pagination(Foo.all)
49
+
50
+ expect(output.current_page).to be(2)
51
+ expect(output.total_count).to be(2)
52
+ expect(output.total_pages).to be(2)
53
+ end
54
+ end
55
+
56
+ context '#paginate' do
57
+ it 'raises an exception if no representer is given' do
58
+ object = Foo.new
59
+ expect{ @endpoint.paginate(object) }.to raise_error
60
+ end
61
+
62
+ it 'returns the object nested in the data key when given a single object' do
63
+ object = Foo.new
64
+ output = @endpoint.paginate(object, with: FooRepresenter)
65
+
66
+ expect(output.has_key?(:data)).to be true
67
+ expect(output[:data]['object_type']).to eq('foo')
68
+ end
69
+
70
+ it 'returns a collection of objects nested in the data key' do
71
+ objects = [Foo.new, Foo.new, Foo.new]
72
+ output = @endpoint.paginate(objects, with: FooRepresenter)
73
+
74
+ expect(output.has_key?(:data)).to be true
75
+ expect(output[:data].class).to be(Array)
76
+ expect(output[:data].first['object_type']).to eq('foo')
77
+ end
78
+
79
+ it 'returns a collection with pagination attributes if the collection is paginated' do
80
+ objects = Kaminari.paginate_array([Foo.new, Foo.new, Foo.new]).page(1)
81
+ output = @endpoint.paginate(objects, with: FooRepresenter)
82
+
83
+ expect(output.has_key?(:data)).to be true
84
+ expect(output.has_key?(:pagination)).to be true
85
+ expect(output[:pagination][:page]).to eq(1)
86
+ expect(output[:pagination][:per_page]).to eq(25)
87
+ expect(output[:pagination][:total_pages]).to eq(1)
88
+ expect(output[:pagination][:total_count]).to eq(3)
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ require 'active_record'
3
+ require 'hashie'
4
+ require 'napa_pagination/pagination'
5
+
6
+ describe NapaPagination::Pagination do
7
+ context '#to_h' do
8
+ it 'returns all pagination attributes that the object responds to' do
9
+ object = Hashie::Mash.new(
10
+ current_page: 2,
11
+ limit_value: 25,
12
+ total_pages: 10,
13
+ total_count: 248
14
+ )
15
+
16
+ data = NapaPagination::Pagination.new(object)
17
+ expect(data.to_h[:page]).to be(2)
18
+ expect(data.to_h[:per_page]).to be(25)
19
+ expect(data.to_h[:total_pages]).to be(10)
20
+ expect(data.to_h[:total_count]).to be(248)
21
+ end
22
+
23
+ it 'skips an attribute if the object does not respond to it' do
24
+ object = Hashie::Mash.new(
25
+ current_page: 2,
26
+ limit_value: 25
27
+ )
28
+
29
+ data = NapaPagination::Pagination.new(object)
30
+ expect(data.to_h[:page]).to be(2)
31
+ expect(data.to_h[:per_page]).to be(25)
32
+ expect(data.to_h[:total_pages]).to be_nil
33
+ expect(data.to_h[:total_count]).to be_nil
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ require 'napa_pagination'
2
+ require 'pry'
3
+
4
+ RSpec.configure do |config|
5
+ config.before(:all) do
6
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
7
+
8
+ ActiveRecord::Schema.define(version: 1) do
9
+ create_table :foos do |t|
10
+ t.string :word
11
+ end
12
+ end
13
+
14
+ class Foo < ActiveRecord::Base
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,201 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: napa_pagination
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Darby Frey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: napa
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
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: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: kaminari
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: actionview
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: A simple pagination representer for Napa
154
+ email:
155
+ - darby@bellycard.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - .gitignore
161
+ - .travis.yml
162
+ - Gemfile
163
+ - LICENSE
164
+ - README.md
165
+ - Rakefile
166
+ - lib/napa_pagination.rb
167
+ - lib/napa_pagination/grape_helpers.rb
168
+ - lib/napa_pagination/pagination.rb
169
+ - lib/napa_pagination/version.rb
170
+ - napa_pagination.gemspec
171
+ - spec/grape_helpers_spec.rb
172
+ - spec/pagination_spec.rb
173
+ - spec/spec_helper.rb
174
+ homepage: ''
175
+ licenses:
176
+ - MIT
177
+ metadata: {}
178
+ post_install_message:
179
+ rdoc_options: []
180
+ require_paths:
181
+ - lib
182
+ required_ruby_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - '>='
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ required_rubygems_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - '>='
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ requirements: []
193
+ rubyforge_project:
194
+ rubygems_version: 2.2.2
195
+ signing_key:
196
+ specification_version: 4
197
+ summary: A simple pagination representer for Napa
198
+ test_files:
199
+ - spec/grape_helpers_spec.rb
200
+ - spec/pagination_spec.rb
201
+ - spec/spec_helper.rb