paginater 0.0.5
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +127 -0
- data/Gemfile.lock.broken +78 -0
- data/Gemfile.lock.good +127 -0
- data/LICENSE.txt +22 -0
- data/README.md +268 -0
- data/Rakefile +1 -0
- data/lib/paginater.rb +31 -0
- data/lib/paginater/constants.rb +32 -0
- data/lib/paginater/core.rb +54 -0
- data/lib/paginater/formatter.rb +131 -0
- data/lib/paginater/page/base_page.rb +31 -0
- data/lib/paginater/page/page_links.rb +50 -0
- data/lib/paginater/version.rb +3 -0
- data/paginater.gemspec +36 -0
- data/spec/paginater/paginatable_array_spec.rb +122 -0
- data/spec/paginater/paginater_spec.rb +327 -0
- data/spec/paginater/paginater_with_kaminari_spec.rb +165 -0
- data/spec/spec_helper.rb +58 -0
- data/spec/support/active_record_support.rb +53 -0
- data/test/config.ru +2 -0
- data/test/favicon.ico +0 -0
- data/test/rss_helper.rb +32 -0
- data/test/running_test.rb +32 -0
- metadata +233 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d8e835d5abb7ce6c5b68117582cf65b3bfa37f2b
|
4
|
+
data.tar.gz: 4c8d9e3b6ec7e9ab5699ac21a46b52ab04e62720
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 910c1b0d34981fdd2695e7a737cd80303a83e5538b5e5693a147a7f0fdd3c0b70c95af0dafd4ac66404c3bed7505a5cd913cb8cb96e2b6ef8df871b8f72f3afe
|
7
|
+
data.tar.gz: 1cc68b151e62de11e77077a999f9d58b078153240689c943403c513604c2ef3fbaff12d8375e8df3c21e1003b5224d206c8d25809bae626980e0e07b954bb1ab
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
paginater (0.0.5)
|
5
|
+
activerecord
|
6
|
+
bundler
|
7
|
+
cookiejar
|
8
|
+
grape
|
9
|
+
kaminari
|
10
|
+
rack-test
|
11
|
+
rake
|
12
|
+
rspec
|
13
|
+
rspec-rails
|
14
|
+
sqlite3
|
15
|
+
|
16
|
+
GEM
|
17
|
+
remote: https://rubygems.org/
|
18
|
+
specs:
|
19
|
+
actionpack (3.2.12)
|
20
|
+
activemodel (= 3.2.12)
|
21
|
+
activesupport (= 3.2.12)
|
22
|
+
builder (~> 3.0.0)
|
23
|
+
erubis (~> 2.7.0)
|
24
|
+
journey (~> 1.0.4)
|
25
|
+
rack (~> 1.4.5)
|
26
|
+
rack-cache (~> 1.2)
|
27
|
+
rack-test (~> 0.6.1)
|
28
|
+
sprockets (~> 2.2.1)
|
29
|
+
activemodel (3.2.12)
|
30
|
+
activesupport (= 3.2.12)
|
31
|
+
builder (~> 3.0.0)
|
32
|
+
activerecord (3.2.12)
|
33
|
+
activemodel (= 3.2.12)
|
34
|
+
activesupport (= 3.2.12)
|
35
|
+
arel (~> 3.0.2)
|
36
|
+
tzinfo (~> 0.3.29)
|
37
|
+
activesupport (3.2.12)
|
38
|
+
i18n (~> 0.6)
|
39
|
+
multi_json (~> 1.0)
|
40
|
+
arel (3.0.2)
|
41
|
+
backports (2.6.7)
|
42
|
+
builder (3.0.4)
|
43
|
+
cookiejar (0.3.0)
|
44
|
+
daemons (1.1.9)
|
45
|
+
descendants_tracker (0.0.1)
|
46
|
+
diff-lcs (1.2.1)
|
47
|
+
erubis (2.7.0)
|
48
|
+
eventmachine (1.0.3)
|
49
|
+
grape (0.3.2)
|
50
|
+
activesupport
|
51
|
+
builder
|
52
|
+
hashie (>= 1.2.0)
|
53
|
+
multi_json (>= 1.3.2)
|
54
|
+
multi_xml (>= 0.5.2)
|
55
|
+
rack
|
56
|
+
rack-accept
|
57
|
+
rack-mount
|
58
|
+
virtus
|
59
|
+
hashie (2.0.2)
|
60
|
+
hike (1.2.1)
|
61
|
+
i18n (0.6.4)
|
62
|
+
journey (1.0.4)
|
63
|
+
json (1.7.7)
|
64
|
+
kaminari (0.14.1)
|
65
|
+
actionpack (>= 3.0.0)
|
66
|
+
activesupport (>= 3.0.0)
|
67
|
+
multi_json (1.6.1)
|
68
|
+
multi_xml (0.5.3)
|
69
|
+
rack (1.4.5)
|
70
|
+
rack-accept (0.4.5)
|
71
|
+
rack (>= 0.4)
|
72
|
+
rack-cache (1.2)
|
73
|
+
rack (>= 0.4)
|
74
|
+
rack-mount (0.8.3)
|
75
|
+
rack (>= 1.0.0)
|
76
|
+
rack-ssl (1.3.3)
|
77
|
+
rack
|
78
|
+
rack-test (0.6.2)
|
79
|
+
rack (>= 1.0)
|
80
|
+
railties (3.2.12)
|
81
|
+
actionpack (= 3.2.12)
|
82
|
+
activesupport (= 3.2.12)
|
83
|
+
rack-ssl (~> 1.3.2)
|
84
|
+
rake (>= 0.8.7)
|
85
|
+
rdoc (~> 3.4)
|
86
|
+
thor (>= 0.14.6, < 2.0)
|
87
|
+
rake (10.0.3)
|
88
|
+
rdoc (3.12.2)
|
89
|
+
json (~> 1.4)
|
90
|
+
rspec (2.13.0)
|
91
|
+
rspec-core (~> 2.13.0)
|
92
|
+
rspec-expectations (~> 2.13.0)
|
93
|
+
rspec-mocks (~> 2.13.0)
|
94
|
+
rspec-core (2.13.1)
|
95
|
+
rspec-expectations (2.13.0)
|
96
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
97
|
+
rspec-mocks (2.13.0)
|
98
|
+
rspec-rails (2.13.0)
|
99
|
+
actionpack (>= 3.0)
|
100
|
+
activesupport (>= 3.0)
|
101
|
+
railties (>= 3.0)
|
102
|
+
rspec-core (~> 2.13.0)
|
103
|
+
rspec-expectations (~> 2.13.0)
|
104
|
+
rspec-mocks (~> 2.13.0)
|
105
|
+
sprockets (2.2.2)
|
106
|
+
hike (~> 1.2)
|
107
|
+
multi_json (~> 1.0)
|
108
|
+
rack (~> 1.0)
|
109
|
+
tilt (~> 1.1, != 1.3.0)
|
110
|
+
sqlite3 (1.3.7)
|
111
|
+
thin (1.5.0)
|
112
|
+
daemons (>= 1.0.9)
|
113
|
+
eventmachine (>= 0.12.6)
|
114
|
+
rack (>= 1.0.0)
|
115
|
+
thor (0.17.0)
|
116
|
+
tilt (1.3.5)
|
117
|
+
tzinfo (0.3.37)
|
118
|
+
virtus (0.5.4)
|
119
|
+
backports (~> 2.6.1)
|
120
|
+
descendants_tracker (~> 0.0.1)
|
121
|
+
|
122
|
+
PLATFORMS
|
123
|
+
ruby
|
124
|
+
|
125
|
+
DEPENDENCIES
|
126
|
+
paginater!
|
127
|
+
thin
|
data/Gemfile.lock.broken
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
paginater (0.0.5)
|
5
|
+
activerecord
|
6
|
+
bundler
|
7
|
+
cookiejar
|
8
|
+
grape
|
9
|
+
kaminari
|
10
|
+
rack-test
|
11
|
+
rake
|
12
|
+
rspec
|
13
|
+
rspec-rails
|
14
|
+
sqlite3
|
15
|
+
|
16
|
+
GEM
|
17
|
+
remote: https://rubygems.org/
|
18
|
+
specs:
|
19
|
+
activerecord (1.6.0)
|
20
|
+
activesupport (3.2.12)
|
21
|
+
i18n (~> 0.6)
|
22
|
+
multi_json (~> 1.0)
|
23
|
+
backports (2.6.7)
|
24
|
+
builder (3.2.0)
|
25
|
+
cookiejar (0.3.0)
|
26
|
+
daemons (1.1.9)
|
27
|
+
descendants_tracker (0.0.1)
|
28
|
+
diff-lcs (1.1.3)
|
29
|
+
eventmachine (1.0.3)
|
30
|
+
grape (0.3.2)
|
31
|
+
activesupport
|
32
|
+
builder
|
33
|
+
hashie (>= 1.2.0)
|
34
|
+
multi_json (>= 1.3.2)
|
35
|
+
multi_xml (>= 0.5.2)
|
36
|
+
rack
|
37
|
+
rack-accept
|
38
|
+
rack-mount
|
39
|
+
virtus
|
40
|
+
hashie (2.0.2)
|
41
|
+
i18n (0.6.4)
|
42
|
+
kaminari (0.11.0)
|
43
|
+
kaminari
|
44
|
+
multi_json (1.6.1)
|
45
|
+
multi_xml (0.5.3)
|
46
|
+
rack (1.5.2)
|
47
|
+
rack-accept (0.4.5)
|
48
|
+
rack (>= 0.4)
|
49
|
+
rack-mount (0.8.3)
|
50
|
+
rack (>= 1.0.0)
|
51
|
+
rack-test (0.6.2)
|
52
|
+
rack (>= 1.0)
|
53
|
+
rake (10.0.3)
|
54
|
+
rspec (2.1.0)
|
55
|
+
rspec-core (~> 2.1.0)
|
56
|
+
rspec-expectations (~> 2.1.0)
|
57
|
+
rspec-mocks (~> 2.1.0)
|
58
|
+
rspec-core (2.1.0)
|
59
|
+
rspec-expectations (2.1.0)
|
60
|
+
diff-lcs (~> 1.1.2)
|
61
|
+
rspec-mocks (2.1.0)
|
62
|
+
rspec-rails (2.1.0)
|
63
|
+
rspec (~> 2.1.0)
|
64
|
+
sqlite3 (1.3.7)
|
65
|
+
thin (1.5.0)
|
66
|
+
daemons (>= 1.0.9)
|
67
|
+
eventmachine (>= 0.12.6)
|
68
|
+
rack (>= 1.0.0)
|
69
|
+
virtus (0.5.4)
|
70
|
+
backports (~> 2.6.1)
|
71
|
+
descendants_tracker (~> 0.0.1)
|
72
|
+
|
73
|
+
PLATFORMS
|
74
|
+
ruby
|
75
|
+
|
76
|
+
DEPENDENCIES
|
77
|
+
paginater!
|
78
|
+
thin
|
data/Gemfile.lock.good
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
paginater (0.0.5)
|
5
|
+
activerecord
|
6
|
+
bundler
|
7
|
+
cookiejar
|
8
|
+
grape
|
9
|
+
kaminari
|
10
|
+
rack-test
|
11
|
+
rake
|
12
|
+
rspec
|
13
|
+
rspec-rails
|
14
|
+
sqlite3
|
15
|
+
|
16
|
+
GEM
|
17
|
+
remote: https://rubygems.org/
|
18
|
+
specs:
|
19
|
+
actionpack (3.2.12)
|
20
|
+
activemodel (= 3.2.12)
|
21
|
+
activesupport (= 3.2.12)
|
22
|
+
builder (~> 3.0.0)
|
23
|
+
erubis (~> 2.7.0)
|
24
|
+
journey (~> 1.0.4)
|
25
|
+
rack (~> 1.4.5)
|
26
|
+
rack-cache (~> 1.2)
|
27
|
+
rack-test (~> 0.6.1)
|
28
|
+
sprockets (~> 2.2.1)
|
29
|
+
activemodel (3.2.12)
|
30
|
+
activesupport (= 3.2.12)
|
31
|
+
builder (~> 3.0.0)
|
32
|
+
activerecord (3.2.12)
|
33
|
+
activemodel (= 3.2.12)
|
34
|
+
activesupport (= 3.2.12)
|
35
|
+
arel (~> 3.0.2)
|
36
|
+
tzinfo (~> 0.3.29)
|
37
|
+
activesupport (3.2.12)
|
38
|
+
i18n (~> 0.6)
|
39
|
+
multi_json (~> 1.0)
|
40
|
+
arel (3.0.2)
|
41
|
+
backports (2.6.7)
|
42
|
+
builder (3.0.4)
|
43
|
+
cookiejar (0.3.0)
|
44
|
+
daemons (1.1.9)
|
45
|
+
descendants_tracker (0.0.1)
|
46
|
+
diff-lcs (1.2.1)
|
47
|
+
erubis (2.7.0)
|
48
|
+
eventmachine (1.0.3)
|
49
|
+
grape (0.3.2)
|
50
|
+
activesupport
|
51
|
+
builder
|
52
|
+
hashie (>= 1.2.0)
|
53
|
+
multi_json (>= 1.3.2)
|
54
|
+
multi_xml (>= 0.5.2)
|
55
|
+
rack
|
56
|
+
rack-accept
|
57
|
+
rack-mount
|
58
|
+
virtus
|
59
|
+
hashie (2.0.2)
|
60
|
+
hike (1.2.1)
|
61
|
+
i18n (0.6.4)
|
62
|
+
journey (1.0.4)
|
63
|
+
json (1.7.7)
|
64
|
+
kaminari (0.14.1)
|
65
|
+
actionpack (>= 3.0.0)
|
66
|
+
activesupport (>= 3.0.0)
|
67
|
+
multi_json (1.6.1)
|
68
|
+
multi_xml (0.5.3)
|
69
|
+
rack (1.4.5)
|
70
|
+
rack-accept (0.4.5)
|
71
|
+
rack (>= 0.4)
|
72
|
+
rack-cache (1.2)
|
73
|
+
rack (>= 0.4)
|
74
|
+
rack-mount (0.8.3)
|
75
|
+
rack (>= 1.0.0)
|
76
|
+
rack-ssl (1.3.3)
|
77
|
+
rack
|
78
|
+
rack-test (0.6.2)
|
79
|
+
rack (>= 1.0)
|
80
|
+
railties (3.2.12)
|
81
|
+
actionpack (= 3.2.12)
|
82
|
+
activesupport (= 3.2.12)
|
83
|
+
rack-ssl (~> 1.3.2)
|
84
|
+
rake (>= 0.8.7)
|
85
|
+
rdoc (~> 3.4)
|
86
|
+
thor (>= 0.14.6, < 2.0)
|
87
|
+
rake (10.0.3)
|
88
|
+
rdoc (3.12.2)
|
89
|
+
json (~> 1.4)
|
90
|
+
rspec (2.13.0)
|
91
|
+
rspec-core (~> 2.13.0)
|
92
|
+
rspec-expectations (~> 2.13.0)
|
93
|
+
rspec-mocks (~> 2.13.0)
|
94
|
+
rspec-core (2.13.1)
|
95
|
+
rspec-expectations (2.13.0)
|
96
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
97
|
+
rspec-mocks (2.13.0)
|
98
|
+
rspec-rails (2.13.0)
|
99
|
+
actionpack (>= 3.0)
|
100
|
+
activesupport (>= 3.0)
|
101
|
+
railties (>= 3.0)
|
102
|
+
rspec-core (~> 2.13.0)
|
103
|
+
rspec-expectations (~> 2.13.0)
|
104
|
+
rspec-mocks (~> 2.13.0)
|
105
|
+
sprockets (2.2.2)
|
106
|
+
hike (~> 1.2)
|
107
|
+
multi_json (~> 1.0)
|
108
|
+
rack (~> 1.0)
|
109
|
+
tilt (~> 1.1, != 1.3.0)
|
110
|
+
sqlite3 (1.3.7)
|
111
|
+
thin (1.5.0)
|
112
|
+
daemons (>= 1.0.9)
|
113
|
+
eventmachine (>= 0.12.6)
|
114
|
+
rack (>= 1.0.0)
|
115
|
+
thor (0.17.0)
|
116
|
+
tilt (1.3.5)
|
117
|
+
tzinfo (0.3.37)
|
118
|
+
virtus (0.5.4)
|
119
|
+
backports (~> 2.6.1)
|
120
|
+
descendants_tracker (~> 0.0.1)
|
121
|
+
|
122
|
+
PLATFORMS
|
123
|
+
ruby
|
124
|
+
|
125
|
+
DEPENDENCIES
|
126
|
+
paginater!
|
127
|
+
thin
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 zeroed
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,268 @@
|
|
1
|
+
# Paginater
|
2
|
+
|
3
|
+
A wannabe pagination gem for [Grape](https://github.com/intridea/grape)
|
4
|
+
|
5
|
+
_YAPG: Yet another pagination gem..._
|
6
|
+
|
7
|
+
Paginater is a simple (still work in progress) gem that want to be a little utility when your API is dealing with a collection of objects.
|
8
|
+
|
9
|
+
You can use Paginater in your Grape app to have a simple feature of pagination that understand `page` and `size` parameters in a GET route.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'paginater'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install paginater
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Paginater (actually) supports 3 pagination systems:
|
28
|
+
|
29
|
+
- `:simple`
|
30
|
+
- `:kaminari`
|
31
|
+
- `:wrapper`
|
32
|
+
|
33
|
+
The `:simple` option uses only the core library functions of Ruby; the `:kaminari` option delegates the pagination to the powerful gem [Kaminari](https://github.com/amatsuda/kaminari) and the `:wrapper` gives you the possibility to wrap your content in a convenient object (this feature is work in progress).
|
34
|
+
Paginater also provides with each response, headers useful for pagination.
|
35
|
+
|
36
|
+
Enable Paginater makes the API url aware of the `page` parameter (required) and the `size` parameter (optional).
|
37
|
+
|
38
|
+
Tell your API to use Paginater:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class API < Grape:API
|
42
|
+
format :json
|
43
|
+
formatter :json, Grape::Formatter::Paginater
|
44
|
+
|
45
|
+
# ...
|
46
|
+
|
47
|
+
get '/stuff', :paginater => :simple do
|
48
|
+
# ... your stuff
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
And get the content paginated with:
|
55
|
+
|
56
|
+
`/stuff?page=1` and `/stuff?page=1&size=4`
|
57
|
+
|
58
|
+
Use Kaminari gem:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class API < Grape:API
|
62
|
+
format :json
|
63
|
+
formatter :json, Grape::Formatter::Paginater
|
64
|
+
|
65
|
+
# ...
|
66
|
+
|
67
|
+
get '/users', :paginater => :kaminari do
|
68
|
+
User.where("age > 18")
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
Using `:kaminari` gives more flexibilty and don't raise any error when an exceeding page is required.
|
75
|
+
|
76
|
+
With a `:size` greater than the `total_count` will be returned and diplayed all the items in the resource.
|
77
|
+
|
78
|
+
Paginater also provide in the headers (see the examples) links useful for pagination.
|
79
|
+
Another feature in progress is the possibility to wrap your content in a convenient object; maybe in the future will be possible to associate with this object a specific template...
|
80
|
+
Set this option (if you dare) with: `:paginater => :wrapper`
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class API < Grape:API
|
84
|
+
format :json
|
85
|
+
formatter :json, Grape::Formatter::Paginater
|
86
|
+
|
87
|
+
# ...
|
88
|
+
|
89
|
+
get '/stuff', :paginater => :wrapper do
|
90
|
+
# ... your stuff
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
Browse the `spec` directory and `spec/paginater/paginater_spec.rb` or `spec/paginater/paginater_with_kaminari_spec.rb` files for more examples.
|
97
|
+
|
98
|
+
The last feature in progress is something useful stored in cookies. Actually you can find a value: `#page/#pages/#count` calculated with size=1.
|
99
|
+
|
100
|
+
## A Running Test
|
101
|
+
|
102
|
+
In the `test` directory there is a very simple running test (configured with the `:kaminari` option).
|
103
|
+
|
104
|
+
```
|
105
|
+
$ cd test
|
106
|
+
/test$ bundle exec rackup
|
107
|
+
[2013-03-09 16:05:25] INFO WEBrick 1.3.1
|
108
|
+
[2013-03-09 16:05:25] INFO ruby 2.0.0 (2013-02-24) [i686-linux]
|
109
|
+
[2013-03-09 16:05:25] INFO WEBrick::HTTPServer#start: pid=3776 port=9292
|
110
|
+
...
|
111
|
+
```
|
112
|
+
|
113
|
+
This expose two resources: `/foo` and `/news` (the last require an internet connection).
|
114
|
+
Then try:
|
115
|
+
|
116
|
+
```
|
117
|
+
http://localhost:9292/foo?page=1
|
118
|
+
["Alpha"]
|
119
|
+
|
120
|
+
http://localhost:9292/foo?page=4&size=6
|
121
|
+
["Sierra","Tango","Uniform","Victor","Whiskey","X-Ray"]
|
122
|
+
|
123
|
+
http://localhost:9292/foo?page=2&size=4
|
124
|
+
["Echo","Foxtrot","Golf","Hotel"]
|
125
|
+
|
126
|
+
http://localhost:9292/foo
|
127
|
+
{"error":"missing param"}
|
128
|
+
|
129
|
+
http://localhost:9292/foo?page=42
|
130
|
+
[] #=> with :kaminari
|
131
|
+
#=> {"error":"invalid page 42"} # with :simple
|
132
|
+
|
133
|
+
http://localhost:9292/foo?page=2&size=42
|
134
|
+
[] #=> with :kaminari
|
135
|
+
#=> {"error":"page 2 out"} # with :simple
|
136
|
+
|
137
|
+
http://localhost:9292/foo?page=1&size=111
|
138
|
+
["Alpha","Bravo","Charlie", <...> ,"X-Ray","Yankee","Zulu"]
|
139
|
+
|
140
|
+
http://localhost:9292/foo?page=1&size=0
|
141
|
+
#=> no error because default size is 1
|
142
|
+
#=> {"error":"divided by 0"} # with :kaminari
|
143
|
+
#=> {"error":"invalid size 0"} # with :simple
|
144
|
+
|
145
|
+
http://localhost:9292/foo?page=0
|
146
|
+
{"error":"first page must be 1"}
|
147
|
+
|
148
|
+
```
|
149
|
+
|
150
|
+
## headers
|
151
|
+
|
152
|
+
In the response header there are links useful for pagination... maybe more to come...
|
153
|
+
|
154
|
+
- first page: `{:url=>\"/foo?page=1\", :rel=>\"first\"};{:url=>\"/foo?page=2\", :rel=>\"next\"}\n`
|
155
|
+
- last page: `{:url=>\"/foo?page=1\", :rel=>\"first\"};;{:url=>\"/foo?page=9\", :rel=>\"prev\"}`
|
156
|
+
- just a page: `{:url=>\"/foo?page=1&size=3\", :rel=>\"first\"};{:url=>\"/foo?page=4&size=3\", :rel=>\"next\"};{:url=>\"/foo?page=2&size=3\", :rel=>\"prev\"}`
|
157
|
+
|
158
|
+
The first page:
|
159
|
+
|
160
|
+
```
|
161
|
+
$ curl -I http://localhost:9292/foo?page=1&size=4
|
162
|
+
$ HTTP/1.1 200 OK
|
163
|
+
Content-Type: application/json
|
164
|
+
Link: {:url=>"/foo?page=1", :rel=>"first"};{:url=>"/foo?page=2", :rel=>"next"}
|
165
|
+
Content-Length: 9
|
166
|
+
Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-02-24)
|
167
|
+
Date: Sat, 09 Mar 2013 15:21:10 GMT
|
168
|
+
Connection: Keep-Alive
|
169
|
+
|
170
|
+
```
|
171
|
+
|
172
|
+
```
|
173
|
+
$ curl -I http://localhost:9292/news?page=1&size=1
|
174
|
+
$ HTTP/1.1 200 OK
|
175
|
+
Content-Type: application/json
|
176
|
+
Link: {:url=>"/news?page=1", :rel=>"first"};{:url=>"/news?page=2", :rel=>"next"}
|
177
|
+
Content-Length: 130
|
178
|
+
Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-02-24)
|
179
|
+
Date: Sat, 09 Mar 2013 16:56:15 GMT
|
180
|
+
Connection: Keep-Alive
|
181
|
+
|
182
|
+
```
|
183
|
+
|
184
|
+
Just a page:
|
185
|
+
|
186
|
+
```
|
187
|
+
$ curl -I http://localhost:9292/foo?page=2&size=4
|
188
|
+
$ HTTP/1.1 200 OK
|
189
|
+
Content-Type: application/json
|
190
|
+
Set-Cookie: 2/26/26
|
191
|
+
Link: {:url=>"/foo?page=1", :rel=>"first"};{:url=>"/foo?page=3", :rel=>"next"};{:url=>"/foo?page=1", :rel=>"prev"}
|
192
|
+
Content-Length: 9
|
193
|
+
Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-02-24)
|
194
|
+
Date: Sat, 09 Mar 2013 15:22:12 GMT
|
195
|
+
Connection: Keep-Alive
|
196
|
+
|
197
|
+
```
|
198
|
+
|
199
|
+
The last page:
|
200
|
+
|
201
|
+
```
|
202
|
+
$ curl -I http://localhost:9292/foo?page=26
|
203
|
+
$ HTTP/1.1 200 OK
|
204
|
+
Content-Type: application/json
|
205
|
+
Set-Cookie: 26/26/26
|
206
|
+
Link: {:url=>"/foo?page=1", :rel=>"first"};;{:url=>"/foo?page=25", :rel=>"prev"}
|
207
|
+
Content-Length: 8
|
208
|
+
Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-02-24)
|
209
|
+
Date: Sat, 09 Mar 2013 15:23:03 GMT
|
210
|
+
Connection: Keep-Alive
|
211
|
+
|
212
|
+
```
|
213
|
+
|
214
|
+
```
|
215
|
+
$ curl -I http://localhost:9292/news?page=10
|
216
|
+
$ HTTP/1.1 200 OK
|
217
|
+
Content-Type: application/json
|
218
|
+
Set-Cookie: 10/10/10
|
219
|
+
Link: {:url=>"/news?page=1", :rel=>"first"};{:url=>"/news?page=9", :rel=>"prev"}
|
220
|
+
Content-Length: 127
|
221
|
+
Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-02-24)
|
222
|
+
Date: Sat, 09 Mar 2013 16:56:47 GMT
|
223
|
+
Connection: Keep-Alive
|
224
|
+
|
225
|
+
```
|
226
|
+
|
227
|
+
With `:simple`
|
228
|
+
|
229
|
+
```
|
230
|
+
$ curl -I http://localhost:9292/news?page=11
|
231
|
+
HTTP/1.1 500 Internal Server Error
|
232
|
+
Content-Type: application/json
|
233
|
+
Content-Length: 27
|
234
|
+
Connection: keep-alive
|
235
|
+
Server: thin 1.5.0 codename Knife
|
236
|
+
```
|
237
|
+
|
238
|
+
With `:kaminari`
|
239
|
+
|
240
|
+
```
|
241
|
+
#=> []
|
242
|
+
HTTP/1.1 200 OK
|
243
|
+
```
|
244
|
+
|
245
|
+
## Note
|
246
|
+
|
247
|
+
Unrelated but inspired by this beautiful code:
|
248
|
+
|
249
|
+
- [grape](https://github.com/intridea/grape)
|
250
|
+
- [grape-entity](https://github.com/agileanimal/grape-entity)
|
251
|
+
- [grape-rabl](https://github.com/LTe/grape-rabl)
|
252
|
+
|
253
|
+
## Discuss
|
254
|
+
|
255
|
+
There is a thread on the [grape-mailinglist](https://groups.google.com/forum/?fromgroups=#!forum/ruby-grape):
|
256
|
+
|
257
|
+
- https://groups.google.com/forum/?fromgroups=#!topic/ruby-grape/vH2DP1qsN9I
|
258
|
+
|
259
|
+
## Contributing
|
260
|
+
|
261
|
+
1. Fork it
|
262
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
263
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
264
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
265
|
+
5. Create new Pull Request
|
266
|
+
|
267
|
+
_
|
268
|
+
|