|
1 | 1 | require 'active_support' |
2 | 2 |
|
3 | | -# = Helper To Make Resource APIs Paginatable |
| 3 | +# = Paginatable |
4 | 4 | # |
5 | | -# Paginating the requested items can avoid returning too much information |
6 | | -# in a single response. API callers can iterate over the results using |
7 | | -# pagination instead of rerteving all the data in one time, ruining the |
8 | | -# database connection or network. |
| 5 | +# Paginating the requested items can avoid returning too much data in a single |
| 6 | +# response. API clients can iterate over the results with pagination instead of |
| 7 | +# rerteving all the data in one time, ruining the database connection or |
| 8 | +# network. |
9 | 9 | # |
10 | | -# There are two parameters clients can use: +per_page+ and +page+. The former |
11 | | -# is used for setting how many data will be returned in each page, there will |
| 10 | +# There are two available URL parameters: +per_page+ and +page+. The former is |
| 11 | +# used for setting how many resources will be returned in each page, there will |
12 | 12 | # be a maxium limit and default value for each API: |
13 | 13 | # |
14 | 14 | # GET /posts?per_page=10 |
15 | 15 | # |
16 | | -# <em>The server will respond 10 items at a time.</em> |
| 16 | +# <em>The server will respond 10 resources in a request.</em> |
17 | 17 | # |
18 | | -# Use the +page+ parameter to specify which to retrieve: |
| 18 | +# Use the +page+ parameter to specify which to page get: |
19 | 19 | # |
20 | 20 | # GET /posts?page=5 |
21 | 21 | # |
|
30 | 30 | # |
31 | 31 | # Which follows the proposed RFC 5988 standard. |
32 | 32 | # |
| 33 | +# An aditional header, +X-Items-Count+, will also be set to the total pages |
| 34 | +# count. |
| 35 | +# |
33 | 36 | # == Usage |
34 | 37 | # |
35 | 38 | # Include this +Concern+ in your Action Controller: |
|
41 | 44 | # or in your Grape API class: |
42 | 45 | # |
43 | 46 | # class SampleAPI < Grape::API |
44 | | -# include APIHelper::Paginatable |
| 47 | +# helpers APIHelper::Paginatable |
45 | 48 | # end |
46 | 49 | # |
47 | | -# then set the options for pagination in the grape method: |
| 50 | +# then set the options for pagination in the grape method, as the following as |
| 51 | +# an example: |
48 | 52 | # |
49 | 53 | # resources :posts do |
50 | 54 | # get do |
51 | | -# pagination User.count, default_per_page: 25, maxium_per_page: 100 |
| 55 | +# collection = current_user.posts |
| 56 | +# pagination collection.count, default_per_page: 25, maxium_per_page: 100 |
52 | 57 | # |
53 | 58 | # # ... |
54 | 59 | # end |
55 | 60 | # end |
56 | 61 | # |
57 | | -# Then use the helper methods, like this: |
| 62 | +# Then use the helper methods like this: |
58 | 63 | # |
| 64 | +# # this example uses kaminari |
59 | 65 | # User.page(page).per(per_page) |
60 | 66 | # |
61 | | -# HTTP Link header will be automatically set. |
| 67 | +# HTTP Link header will be automatically set by the way. |
62 | 68 | module APIHelper::Paginatable |
63 | 69 | extend ActiveSupport::Concern |
64 | 70 |
|
65 | | - def pagination(items_count, default_per_page: 20, maxium_per_page: 100, set_header: true) |
| 71 | + # Set pagination for the request |
| 72 | + # |
| 73 | + # Params: |
| 74 | + # |
| 75 | + # +items_count+:: |
| 76 | + # +Symbol+ name of resource to receive the inclusion |
| 77 | + # |
| 78 | + # +default_per_page+:: |
| 79 | + # +Integer+ default per_page |
| 80 | + # |
| 81 | + # +maxium_per_page+:: |
| 82 | + # +Integer+ maximum results do return on a single page |
| 83 | + # |
| 84 | + def pagination(items_count, default_per_page: 20, |
| 85 | + maxium_per_page: 100, |
| 86 | + set_header: true) |
66 | 87 | items_count = items_count.count if items_count.respond_to? :count |
67 | 88 |
|
68 | | - @per_page = (params[:per_page] || default_per_page).to_i |
69 | | - @per_page = maxium_per_page if @per_page > maxium_per_page |
70 | | - @per_page = 1 if @per_page < 1 |
| 89 | + @pagination_per_page = (params[:per_page] || default_per_page).to_i |
| 90 | + @pagination_per_page = maxium_per_page if @pagination_per_page > maxium_per_page |
| 91 | + @pagination_per_page = 1 if @pagination_per_page < 1 |
71 | 92 |
|
72 | 93 | items_count = 0 if items_count < 0 |
73 | | - pages_count = (items_count.to_f / @per_page).ceil |
| 94 | + pages_count = (items_count.to_f / @pagination_per_page).ceil |
74 | 95 | pages_count = 1 if pages_count < 1 |
75 | 96 |
|
76 | | - @page = (params[:page] || 1).to_i |
77 | | - @page = pages_count if @page > pages_count |
78 | | - @page = 1 if @page < 1 |
| 97 | + @pagination_page = (params[:page] || 1).to_i |
| 98 | + @pagination_page = pages_count if @pagination_page > pages_count |
| 99 | + @pagination_page = 1 if @pagination_page < 1 |
79 | 100 |
|
80 | | - link_headers ||= [] |
| 101 | + if set_header |
| 102 | + link_headers ||= [] |
81 | 103 |
|
82 | | - if current_page < pages_count |
83 | | - link_headers << "<#{add_or_replace_uri_param(request.url, :page, current_page + 1)}>; rel=\"next\"" |
84 | | - link_headers << "<#{add_or_replace_uri_param(request.url, :page, pages_count)}>; rel=\"last\"" |
85 | | - end |
86 | | - if current_page > 1 |
87 | | - link_headers << "<#{add_or_replace_uri_param(request.url, :page, (current_page > pages_count ? pages_count : current_page - 1))}>; rel=\"prev\"" |
88 | | - link_headers << "<#{add_or_replace_uri_param(request.url, :page, 1)}>; rel=\"first\"" |
89 | | - end |
| 104 | + if current_page > 1 |
| 105 | + link_headers << "<#{add_or_replace_uri_param(request.url, :page, 1)}>; rel=\"first\"" |
| 106 | + link_headers << "<#{add_or_replace_uri_param(request.url, :page, (current_page > pages_count ? pages_count : current_page - 1))}>; rel=\"prev\"" |
| 107 | + end |
90 | 108 |
|
91 | | - link_header = link_headers.join(', ') |
| 109 | + if current_page < pages_count |
| 110 | + link_headers << "<#{add_or_replace_uri_param(request.url, :page, current_page + 1)}>; rel=\"next\"" |
| 111 | + link_headers << "<#{add_or_replace_uri_param(request.url, :page, pages_count)}>; rel=\"last\"" |
| 112 | + end |
| 113 | + |
| 114 | + link_header = link_headers.join(', ') |
92 | 115 |
|
93 | | - if set_header |
94 | 116 | if self.respond_to?(:header) |
95 | 117 | self.header('Link', link_header) |
96 | 118 | self.header('X-Items-Count', items_count.to_s) |
| 119 | + self.header('X-Pages-Count', pages_count.to_s) |
97 | 120 | end |
98 | 121 |
|
99 | 122 | if defined?(response) && response.respond_to?(:headers) |
100 | 123 | response.headers['Link'] = link_header |
101 | 124 | response.headers['X-Items-Count'] = items_count.to_s |
| 125 | + response.headers['X-Pages-Count'] = pages_count.to_s |
102 | 126 | end |
103 | 127 | end |
104 | | - |
105 | | - link_header |
106 | 128 | end |
107 | 129 |
|
108 | 130 | # Getter for the current page |
109 | | - def page |
110 | | - @page |
| 131 | + def pagination_page |
| 132 | + @pagination_page |
111 | 133 | end |
112 | 134 |
|
113 | | - alias_method :current_page, :page |
| 135 | + alias_method :current_page, :pagination_page |
114 | 136 |
|
115 | 137 | # Getter for per_page |
116 | | - def per_page |
117 | | - @per_page |
| 138 | + def pagination_per_page |
| 139 | + @pagination_per_page |
118 | 140 | end |
119 | 141 |
|
120 | | - alias_method :page_with, :per_page |
| 142 | + alias_method :paginate_with, :pagination_per_page |
121 | 143 |
|
122 | | - def add_or_replace_uri_param(url, param_name, param_value) |
| 144 | + def add_or_replace_uri_param(url, param_name, param_value) # :nodoc: |
123 | 145 | uri = URI(url) |
124 | 146 | params = URI.decode_www_form(uri.query || '') |
125 | 147 | params.delete_if { |param| param[0].to_s == param_name.to_s } |
|
0 commit comments