Skip to content

Commit c7b093c

Browse files
AMHOLchingor13
authored andcommitted
Make paginator page/per_page param keys configurable (#239)
1 parent 0216d47 commit c7b093c

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,17 @@ end
447447
448448
You can customize how your resources find pagination information from the response.
449449
450+
If the [existing paginator](https://github.com/chingor13/json_api_client/blob/master/lib/json_api_client/paginating/paginator.rb) fits your requirements but you don't use the default `page` and `per_page` params for pagination, you can customise the param keys as follows:
451+
452+
```ruby
453+
JsonApiClient::Paginating::Paginator.page_param = "page[number]"
454+
JsonApiClient::Paginating::Paginator.per_page_param = "page[size]"
455+
```
456+
457+
Please note that this is a global configuration, so library authors should create a custom paginator that inherits `JsonApiClient::Paginating::Paginator` and configure the custom paginator to avoid modifying global config.
458+
459+
If the [existing paginator](https://github.com/chingor13/json_api_client/blob/master/lib/json_api_client/paginating/paginator.rb) does not fit your needs, you can create a custom paginator:
460+
450461
```ruby
451462
class MyPaginator
452463
def initialize(result_set, data); end

lib/json_api_client/paginating/paginator.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
module JsonApiClient
22
module Paginating
33
class Paginator
4+
class_attribute :page_param,
5+
:per_page_param
6+
7+
self.page_param = "page"
8+
self.per_page_param = "per_page"
9+
410
attr_reader :params, :result_set, :links
11+
512
def initialize(result_set, data)
613
@params = params_for_uri(result_set.uri)
714
@result_set = result_set
8-
@links = data['links']
15+
@links = data["links"]
916
end
1017

1118
def next
@@ -28,7 +35,7 @@ def total_pages
2835
if links["last"]
2936
uri = result_set.links.link_url_for("last")
3037
last_params = params_for_uri(uri)
31-
last_params.fetch("page") do
38+
last_params.fetch(page_param) do
3239
current_page
3340
end.to_i
3441
else
@@ -47,13 +54,13 @@ def offset
4754
end
4855

4956
def per_page
50-
params.fetch("per_page") do
57+
params.fetch(per_page_param) do
5158
result_set.length
5259
end.to_i
5360
end
5461

5562
def current_page
56-
params.fetch("page", 1).to_i
63+
params.fetch(page_param, 1).to_i
5764
end
5865

5966
def out_of_bounds?

test/unit/top_level_links_test.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,49 @@ def test_can_parse_pagination_links
7474
assert_pagination
7575
end
7676

77+
def test_can_parse_pagination_links_with_custom_config
78+
JsonApiClient::Paginating::Paginator.page_param = "page[number]"
79+
80+
stub_request(:get, "http://example.com/articles")
81+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
82+
data: [{
83+
type: "articles",
84+
id: "1",
85+
attributes: {
86+
title: "JSON API paints my bikeshed!"
87+
}
88+
}],
89+
links: {
90+
self: "http://example.com/articles",
91+
next: "http://example.com/articles?#{{page: {number: 2}}.to_query}",
92+
prev: nil,
93+
first: "http://example.com/articles",
94+
last: "http://example.com/articles?#{{page: {number: 6}}.to_query}"
95+
}
96+
}.to_json)
97+
stub_request(:get, "http://example.com/articles?#{{page: {number: 2}}.to_query}")
98+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
99+
data: [{
100+
type: "articles",
101+
id: "2",
102+
attributes: {
103+
title: "This is tha BOMB"
104+
}
105+
}],
106+
links: {
107+
self: "http://example.com/articles?#{{page: {number: 2}}.to_query}",
108+
next: "http://example.com/articles?#{{page: {number: 3}}.to_query}",
109+
prev: "http://example.com/articles",
110+
first: "http://example.com/articles",
111+
last: "http://example.com/articles?#{{page: {number: 6}}.to_query}"
112+
}
113+
}.to_json)
114+
115+
assert_pagination
116+
117+
JsonApiClient::Paginating::Paginator.page_param = "page"
118+
end
119+
77120
def test_can_parse_pagination_links_when_no_next_page
78121
stub_request(:get, "http://example.com/articles")
79122
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {

0 commit comments

Comments
 (0)