3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/grape/pagination.rb', line 3
def self.included(base)
Grape::Endpoint.class_eval do
def paginate(collection)
per_page = params[:per_page] || route_setting(:per_page)
options = {
:page => params[:page],
:per_page => [per_page, route_setting(:max_per_page)].compact.min
}
collection = ApiPagination.paginate(collection, options)
links = (['Link'] || "").split(',').map(&:strip)
url = request.url.sub(/\?.*$/, '')
pages = ApiPagination.pages_from(collection)
pages.each do |k, v|
old_params = Rack::Utils.parse_nested_query(request.query_string)
new_params = old_params.merge('page' => v)
links << %(<#{url}?#{new_params.to_param}>; rel="#{k}")
end
= ApiPagination.config.
= ApiPagination.config.
= ApiPagination.config.
include_total = ApiPagination.config.include_total
'Link', links.join(', ') unless links.empty?
, ApiPagination.total_from(collection) if include_total
, options[:per_page].to_s
, options[:page].to_s unless .nil?
return collection
end
end
base.class_eval do
def self.paginate(options = {})
route_setting :per_page, options[:per_page]
route_setting :max_per_page, options[:max_per_page]
params do
optional :page, :type => Integer, :default => 1,
:desc => 'Page of results to fetch.'
optional :per_page, :type => Integer,
:desc => 'Number of results to return per page.'
end
end
end
end
|