api-pagination 6.0.0 → 7.0.0
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 +4 -4
- data/lib/api-pagination/configuration.rb +35 -30
- data/lib/api-pagination/hooks.rb +8 -8
- data/lib/api-pagination/railtie.rb +2 -2
- data/lib/api-pagination/version.rb +2 -2
- data/lib/api-pagination.rb +24 -21
- data/lib/grape/pagination.rb +17 -17
- data/lib/rails/pagination.rb +11 -13
- metadata +12 -55
- data/spec/active_record_spec.rb +0 -50
- data/spec/api-pagination_spec.rb +0 -76
- data/spec/grape_spec.rb +0 -165
- data/spec/rails_spec.rb +0 -312
- data/spec/sequel_spec.rb +0 -30
- data/spec/spec_helper.rb +0 -39
- data/spec/support/active_record/foo.rb +0 -3
- data/spec/support/active_record/schema.rb +0 -5
- data/spec/support/numbers_api.rb +0 -41
- data/spec/support/numbers_controller.rb +0 -103
- data/spec/support/shared_examples/existing_headers.rb +0 -14
- data/spec/support/shared_examples/first_page.rb +0 -30
- data/spec/support/shared_examples/last_page.rb +0 -31
- data/spec/support/shared_examples/middle_page.rb +0 -22
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
require 'action_controller/railtie'
|
|
2
|
-
require 'api-pagination/hooks'
|
|
3
|
-
require 'ostruct'
|
|
4
|
-
|
|
5
|
-
module Rails
|
|
6
|
-
def self.application
|
|
7
|
-
@application ||= begin
|
|
8
|
-
routes = ActionDispatch::Routing::RouteSet.new
|
|
9
|
-
OpenStruct.new(:routes => routes, :env_config => {})
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
module ControllerExampleGroup
|
|
15
|
-
def self.included(base)
|
|
16
|
-
base.extend ClassMethods
|
|
17
|
-
base.send(:include, ActionController::TestCase::Behavior)
|
|
18
|
-
|
|
19
|
-
base.prepend_before do
|
|
20
|
-
@routes = Rails.application.routes
|
|
21
|
-
@controller = described_class.new
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
module ClassMethods
|
|
26
|
-
def setup(*methods)
|
|
27
|
-
methods.each do |method|
|
|
28
|
-
if method.to_s =~ /^setup_(fixtures|controller_request_and_response)$/
|
|
29
|
-
prepend_before { send method }
|
|
30
|
-
else
|
|
31
|
-
before { send method }
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def teardown(*methods)
|
|
37
|
-
methods.each { |method| after { send method } }
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
Rails.application.routes.draw do
|
|
43
|
-
resources :numbers, :only => [:index] do
|
|
44
|
-
collection do
|
|
45
|
-
get :index_with_custom_render
|
|
46
|
-
get :index_with_no_per_page
|
|
47
|
-
get :index_with_paginate_array_options
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
class NumbersSerializer
|
|
53
|
-
def initialize(numbers)
|
|
54
|
-
@numbers = numbers
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def to_json(options = {})
|
|
58
|
-
{ numbers: @numbers.map { |n| { number: n } } }.to_json
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
class NumbersController < ActionController::API
|
|
63
|
-
include Rails.application.routes.url_helpers
|
|
64
|
-
|
|
65
|
-
def index
|
|
66
|
-
total = params.fetch(:count).to_i
|
|
67
|
-
|
|
68
|
-
if params[:with_headers]
|
|
69
|
-
query = request.query_parameters.dup
|
|
70
|
-
query.delete(:with_headers)
|
|
71
|
-
headers['Link'] = %(<#{numbers_url}?#{query.to_param}>; rel="without")
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
paginate :json => (1..total).to_a, :per_page => 10
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
def index_with_custom_render
|
|
78
|
-
total = params.fetch(:count).to_i
|
|
79
|
-
numbers = (1..total).to_a
|
|
80
|
-
numbers = paginate numbers, :per_page => 10
|
|
81
|
-
|
|
82
|
-
render json: NumbersSerializer.new(numbers)
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
def index_with_no_per_page
|
|
86
|
-
total = params.fetch(:count).to_i
|
|
87
|
-
numbers = (1..total).to_a
|
|
88
|
-
numbers = paginate numbers
|
|
89
|
-
|
|
90
|
-
render json: NumbersSerializer.new(numbers)
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def index_with_paginate_array_options
|
|
94
|
-
count = params.fetch(:count).to_i
|
|
95
|
-
total_count = params.fetch(:paginate_array_total_count).to_i
|
|
96
|
-
numbers = (1..count).to_a
|
|
97
|
-
numbers = paginate numbers, paginate_array_options: {total_count: total_count}
|
|
98
|
-
|
|
99
|
-
render json: NumbersSerializer.new(numbers)
|
|
100
|
-
end
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
ApiPagination::Railtie.initializers.each(&:run)
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
shared_examples 'an endpoint with existing Link headers' do
|
|
2
|
-
it 'should keep existing Links' do
|
|
3
|
-
expect(links).to include('<http://example.org/numbers?count=30>; rel="without"')
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
it 'should contain pagination Links' do
|
|
7
|
-
expect(links).to include('<http://example.org/numbers?count=30&page=2&with_headers=true>; rel="next"')
|
|
8
|
-
expect(links).to include('<http://example.org/numbers?count=30&page=3&with_headers=true>; rel="last"')
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
it 'should give a Total header' do
|
|
12
|
-
expect(total).to eq(30)
|
|
13
|
-
end
|
|
14
|
-
end
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
shared_examples 'an endpoint with a first page' do
|
|
2
|
-
it 'should not give a link with rel "first"' do
|
|
3
|
-
expect(link).not_to include('rel="first"')
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
it 'should not give a link with rel "prev"' do
|
|
7
|
-
expect(link).not_to include('rel="prev"')
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it 'should give a link with rel "last"' do
|
|
11
|
-
expect(links).to include('<http://example.org/numbers?count=100&page=10>; rel="last"')
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it 'should give a link with rel "next"' do
|
|
15
|
-
expect(links).to include('<http://example.org/numbers?count=100&page=2>; rel="next"')
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it 'should give a Total header' do
|
|
19
|
-
expect(total).to eq(100)
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it 'should list the first page of numbers in the response body' do
|
|
23
|
-
body = '[1,2,3,4,5,6,7,8,9,10]'
|
|
24
|
-
if defined?(response)
|
|
25
|
-
expect(response.body).to eq(body)
|
|
26
|
-
else
|
|
27
|
-
expect(last_response.body).to eq(body)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
shared_examples 'an endpoint with a last page' do
|
|
2
|
-
it 'should not give a link with rel "last"' do
|
|
3
|
-
expect(link).not_to include('rel="last"')
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
it 'should not give a link with rel "next"' do
|
|
7
|
-
expect(link).not_to include('rel="next"')
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it 'should give a link with rel "first"' do
|
|
11
|
-
expect(links).to include('<http://example.org/numbers?count=100&page=1>; rel="first"')
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it 'should give a link with rel "prev"' do
|
|
15
|
-
expect(links).to include('<http://example.org/numbers?count=100&page=9>; rel="prev"')
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it 'should give a Total header' do
|
|
19
|
-
expect(total).to eq(100)
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it 'should list the last page of numbers in the response body' do
|
|
23
|
-
body = '[91,92,93,94,95,96,97,98,99,100]'
|
|
24
|
-
|
|
25
|
-
if defined?(response)
|
|
26
|
-
expect(response.body).to eq(body)
|
|
27
|
-
else
|
|
28
|
-
expect(last_response.body).to eq(body)
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
shared_examples 'an endpoint with a middle page' do
|
|
2
|
-
it 'should give all pagination links' do
|
|
3
|
-
expect(links).to include('<http://example.org/numbers?count=100&page=1>; rel="first"')
|
|
4
|
-
expect(links).to include('<http://example.org/numbers?count=100&page=10>; rel="last"')
|
|
5
|
-
expect(links).to include('<http://example.org/numbers?count=100&page=3>; rel="next"')
|
|
6
|
-
expect(links).to include('<http://example.org/numbers?count=100&page=1>; rel="prev"')
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
it 'should give a Total header' do
|
|
10
|
-
expect(total).to eq(100)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
it 'should list a middle page of numbers in the response body' do
|
|
14
|
-
body = '[11,12,13,14,15,16,17,18,19,20]'
|
|
15
|
-
|
|
16
|
-
if defined?(response)
|
|
17
|
-
expect(response.body).to eq(body)
|
|
18
|
-
else
|
|
19
|
-
expect(last_response.body).to eq(body)
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|