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
50
51
52
53
54
55 
     | 
    
      # File 'lib/grape/pagination.rb', line 3
def self.included(base)
  Grape::Endpoint.class_eval do
    def paginate(collection)
      per_page = ApiPagination.config.per_page_param(params) || route_setting(:per_page)
      options = {
        :page     => ApiPagination.config.page_param(params),
        :per_page => [per_page, route_setting(:max_per_page)].compact.min
      }
      collection, pagy = ApiPagination.paginate(collection, options)
      links = (['Link'] || "").split(',').map(&:strip)
      url   = request.url.sub(/\?.*$/, '')
      pages = ApiPagination.pages_from(pagy || collection, options)
      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(pagy || collection).to_s 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]
      enforce_max_per_page = options[:max_per_page] && options[:enforce_max_per_page]
      per_page_values = enforce_max_per_page ? 0..options[:max_per_page] : nil
      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.',
                            :values => per_page_values
      end
    end
  end
end
     |