Skip to content

Commit d069fd9

Browse files
author
Doug Smith
committed
fix: make page[] param wrap consistent fixes #347
1 parent 287dcb0 commit d069fd9

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

lib/json_api_client/paginating/paginator.rb

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11
module JsonApiClient
22
module Paginating
33
class Paginator
4-
class_attribute :page_param,
5-
:per_page_param
4+
# Define class accessors as methods to enforce standard way
5+
# of defining pagination related query string params.
6+
class << self
7+
DEFAULT_PAGE_PARAM = "page".freeze
8+
DEFAULT_PER_PAGE_PARAM = "per_page".freeze
9+
10+
def page_param
11+
@_page_param ||= DEFAULT_PAGE_PARAM
12+
"page[#{@_page_param}]"
13+
end
14+
15+
def page_param=(param = DEFAULT_PAGE_PARAM)
16+
raise ArgumentError, "don't wrap page_param" unless valid_param?(param)
17+
18+
@_page_param = param.to_s
19+
end
20+
21+
def per_page_param
22+
@_per_page_param ||= DEFAULT_PER_PAGE_PARAM
23+
"page[#{@_per_page_param}]"
24+
end
25+
26+
def per_page_param=(param = DEFAULT_PER_PAGE_PARAM)
27+
raise ArgumentError, "don't wrap per_page_param" unless valid_param?(param)
28+
29+
@_per_page_param = param
30+
end
631

7-
self.page_param = "page"
8-
self.per_page_param = "per_page"
32+
private
33+
34+
def valid_param?(param)
35+
!(param.nil? || param.to_s.include?("[") || param.to_s.include?("]"))
36+
end
37+
38+
end
939

1040
attr_reader :params, :result_set, :links
1141

@@ -75,6 +105,14 @@ def next_page
75105
current_page < total_pages ? (current_page + 1) : nil
76106
end
77107

108+
def page_param
109+
self.class.page_param
110+
end
111+
112+
def per_page_param
113+
self.class.per_page_param
114+
end
115+
78116
alias limit_value per_page
79117

80118
protected

lib/json_api_client/query/builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def primary_key_params
146146
end
147147

148148
def pagination_params
149-
@pagination_params.empty? ? {} : {page: @pagination_params}
149+
@pagination_params
150150
end
151151

152152
def includes_params

test/unit/top_level_links_test.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ def test_can_parse_pagination_links
4747
}],
4848
links: {
4949
self: "http://example.com/articles",
50-
next: "http://example.com/articles?page=2",
50+
next: "http://example.com/articles?page[page]=2",
5151
prev: nil,
5252
first: "http://example.com/articles",
53-
last: "http://example.com/articles?page=6"
53+
last: "http://example.com/articles?page[page]=6"
5454
}
5555
}.to_json)
56-
stub_request(:get, "http://example.com/articles?page=2")
56+
stub_request(:get, "http://example.com/articles?page[page]=2")
5757
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
5858
data: [{
5959
type: "articles",
@@ -63,19 +63,19 @@ def test_can_parse_pagination_links
6363
}
6464
}],
6565
links: {
66-
self: "http://example.com/articles?page=2",
67-
next: "http://example.com/articles?page=3",
66+
self: "http://example.com/articles?page[page]=2",
67+
next: "http://example.com/articles?page[page]=3",
6868
prev: "http://example.com/articles",
6969
first: "http://example.com/articles",
70-
last: "http://example.com/articles?page=6"
70+
last: "http://example.com/articles?page[page]=6"
7171
}
7272
}.to_json)
7373

7474
assert_pagination
7575
end
7676

7777
def test_can_parse_pagination_links_with_custom_config
78-
JsonApiClient::Paginating::Paginator.page_param = "page[number]"
78+
JsonApiClient::Paginating::Paginator.page_param = "number"
7979

8080
stub_request(:get, "http://example.com/articles")
8181
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
@@ -114,6 +114,7 @@ def test_can_parse_pagination_links_with_custom_config
114114

115115
assert_pagination
116116

117+
ensure
117118
JsonApiClient::Paginating::Paginator.page_param = "page"
118119
end
119120

@@ -131,7 +132,7 @@ def test_can_parse_pagination_links_when_no_next_page
131132
self: "http://example.com/articles",
132133
prev: nil,
133134
first: "http://example.com/articles",
134-
last: "http://example.com/articles?page=1"
135+
last: "http://example.com/articles?page[page]=1"
135136
}
136137
}.to_json)
137138

@@ -154,7 +155,7 @@ def test_can_parse_complex_pagination_links
154155
meta: {}
155156
},
156157
next: {
157-
href: "http://example.com/articles?page=2",
158+
href: "http://example.com/articles?page[page]=2",
158159
meta: {}
159160
},
160161
prev: nil,
@@ -163,12 +164,12 @@ def test_can_parse_complex_pagination_links
163164
meta: {}
164165
},
165166
last: {
166-
href: "http://example.com/articles?page=6",
167+
href: "http://example.com/articles?page[page]=6",
167168
meta: {}
168169
}
169170
}
170171
}.to_json)
171-
stub_request(:get, "http://example.com/articles?page=2")
172+
stub_request(:get, "http://example.com/articles?page[page]=2")
172173
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
173174
data: [{
174175
type: "articles",
@@ -179,11 +180,11 @@ def test_can_parse_complex_pagination_links
179180
}],
180181
links: {
181182
self: {
182-
href: "http://example.com/articles?page=2",
183+
href: "http://example.com/articles?page[page]=2",
183184
meta: {}
184185
},
185186
next: {
186-
href: "http://example.com/articles?page=3",
187+
href: "http://example.com/articles?page[page]=3",
187188
meta: {}
188189
},
189190
prev: {
@@ -195,7 +196,7 @@ def test_can_parse_complex_pagination_links
195196
meta: {}
196197
},
197198
last: {
198-
href: "http://example.com/articles?page=6",
199+
href: "http://example.com/articles?page[page]=6",
199200
meta: {}
200201
}
201202
}

0 commit comments

Comments
 (0)