json_api_client 1.12.2 → 1.13.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf7116009e3f1ebb219cd068a038a984d20561b2e5987f7e913f08cf48d78f35
|
4
|
+
data.tar.gz: 2218b88e39fdaf36324917037f0ed9f6057ef7675436b6e20625cf29c527766f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85d9257d737ac385d2aece0abb97c459312f34588bfa55022cef4e9964959ab9b51fc16126912fbb0dce661a3cdd68bf92212b4ebb1f2562f1942ab392608076
|
7
|
+
data.tar.gz: 26ef5c579c30b8248a3f981cb3e107dd567fb5aa17238781572b176ea6fefbdecf1d39a52dee303dd41999f585eed22e2c7ee9762321ddcf9ef5c9ee3a000d5e
|
data/README.md
CHANGED
@@ -599,6 +599,20 @@ class MyApi::Base < JsonApiClient::Resource
|
|
599
599
|
end
|
600
600
|
```
|
601
601
|
|
602
|
+
### NestedParamPaginator
|
603
|
+
|
604
|
+
The default `JsonApiClient::Paginating::Paginator` is not strict about how it handles the param keys ([#347](https://github.com/JsonApiClient/json_api_client/issues/347)). There is a second paginator that more rigorously adheres to the JSON:API pagination recommendation style of `page[page]=1&page[per_page]=10`.
|
605
|
+
|
606
|
+
If this second style suits your needs better, it is available as a class override:
|
607
|
+
|
608
|
+
```ruby
|
609
|
+
class Order < JsonApiClient::Resource
|
610
|
+
self.paginator = JsonApiClient::Paginating::NestedParamPaginator
|
611
|
+
end
|
612
|
+
```
|
613
|
+
|
614
|
+
You can also extend `NestedParamPaginator` in your custom paginators or assign the `page_param` or `per_page_param` as with the default version above.
|
615
|
+
|
602
616
|
### Custom type
|
603
617
|
|
604
618
|
If your model must be named differently from classified type of resource you can easily customize it.
|
@@ -0,0 +1,140 @@
|
|
1
|
+
module JsonApiClient
|
2
|
+
module Paginating
|
3
|
+
# An alternate, more consistent Paginator that always wraps
|
4
|
+
# pagination query string params in a top-level wrapper_name,
|
5
|
+
# e.g. page[offset]=2, page[limit]=10.
|
6
|
+
class NestedParamPaginator
|
7
|
+
DEFAULT_WRAPPER_NAME = "page".freeze
|
8
|
+
DEFAULT_PAGE_PARAM = "page".freeze
|
9
|
+
DEFAULT_PER_PAGE_PARAM = "per_page".freeze
|
10
|
+
|
11
|
+
# Define class accessors as methods to enforce standard way
|
12
|
+
# of defining pagination related query string params.
|
13
|
+
class << self
|
14
|
+
|
15
|
+
def wrapper_name
|
16
|
+
@_wrapper_name ||= DEFAULT_WRAPPER_NAME
|
17
|
+
end
|
18
|
+
|
19
|
+
def wrapper_name=(param = DEFAULT_WRAPPER_NAME)
|
20
|
+
raise ArgumentError, "don't wrap wrapper_name" unless valid_param?(param)
|
21
|
+
|
22
|
+
@_wrapper_name = param.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
def page_param
|
26
|
+
@_page_param ||= DEFAULT_PAGE_PARAM
|
27
|
+
"#{wrapper_name}[#{@_page_param}]"
|
28
|
+
end
|
29
|
+
|
30
|
+
def page_param=(param = DEFAULT_PAGE_PARAM)
|
31
|
+
raise ArgumentError, "don't wrap page_param" unless valid_param?(param)
|
32
|
+
|
33
|
+
@_page_param = param.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
def per_page_param
|
37
|
+
@_per_page_param ||= DEFAULT_PER_PAGE_PARAM
|
38
|
+
"#{wrapper_name}[#{@_per_page_param}]"
|
39
|
+
end
|
40
|
+
|
41
|
+
def per_page_param=(param = DEFAULT_PER_PAGE_PARAM)
|
42
|
+
raise ArgumentError, "don't wrap per_page_param" unless valid_param?(param)
|
43
|
+
|
44
|
+
@_per_page_param = param
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def valid_param?(param)
|
50
|
+
!(param.nil? || param.to_s.include?("[") || param.to_s.include?("]"))
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
attr_reader :params, :result_set, :links
|
56
|
+
|
57
|
+
def initialize(result_set, data)
|
58
|
+
@params = params_for_uri(result_set.uri)
|
59
|
+
@result_set = result_set
|
60
|
+
@links = data["links"]
|
61
|
+
end
|
62
|
+
|
63
|
+
def next
|
64
|
+
result_set.links.fetch_link("next")
|
65
|
+
end
|
66
|
+
|
67
|
+
def prev
|
68
|
+
result_set.links.fetch_link("prev")
|
69
|
+
end
|
70
|
+
|
71
|
+
def first
|
72
|
+
result_set.links.fetch_link("first")
|
73
|
+
end
|
74
|
+
|
75
|
+
def last
|
76
|
+
result_set.links.fetch_link("last")
|
77
|
+
end
|
78
|
+
|
79
|
+
def total_pages
|
80
|
+
if links["last"]
|
81
|
+
uri = result_set.links.link_url_for("last")
|
82
|
+
last_params = params_for_uri(uri)
|
83
|
+
last_params.fetch(page_param, &method(:current_page)).to_i
|
84
|
+
else
|
85
|
+
current_page
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# this is an estimate, not necessarily an exact count
|
90
|
+
def total_entries
|
91
|
+
per_page * total_pages
|
92
|
+
end
|
93
|
+
def total_count; total_entries; end
|
94
|
+
|
95
|
+
def offset
|
96
|
+
per_page * (current_page - 1)
|
97
|
+
end
|
98
|
+
|
99
|
+
def per_page
|
100
|
+
params.fetch(per_page_param) do
|
101
|
+
result_set.length
|
102
|
+
end.to_i
|
103
|
+
end
|
104
|
+
|
105
|
+
def current_page
|
106
|
+
params.fetch(page_param, 1).to_i
|
107
|
+
end
|
108
|
+
|
109
|
+
def out_of_bounds?
|
110
|
+
current_page > total_pages
|
111
|
+
end
|
112
|
+
|
113
|
+
def previous_page
|
114
|
+
current_page > 1 ? (current_page - 1) : nil
|
115
|
+
end
|
116
|
+
|
117
|
+
def next_page
|
118
|
+
current_page < total_pages ? (current_page + 1) : nil
|
119
|
+
end
|
120
|
+
|
121
|
+
def page_param
|
122
|
+
self.class.page_param
|
123
|
+
end
|
124
|
+
|
125
|
+
def per_page_param
|
126
|
+
self.class.per_page_param
|
127
|
+
end
|
128
|
+
|
129
|
+
alias limit_value per_page
|
130
|
+
|
131
|
+
protected
|
132
|
+
|
133
|
+
def params_for_uri(uri)
|
134
|
+
return {} unless uri
|
135
|
+
uri = Addressable::URI.parse(uri)
|
136
|
+
( uri.query_values || {} ).with_indifferent_access
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -146,7 +146,13 @@ module JsonApiClient
|
|
146
146
|
end
|
147
147
|
|
148
148
|
def pagination_params
|
149
|
-
|
149
|
+
if klass.paginator.ancestors.include?(Paginating::Paginator)
|
150
|
+
# Original Paginator inconsistently wraps pagination params here. Keeping
|
151
|
+
# default behavior for now so as not to break backward compatibility.
|
152
|
+
@pagination_params.empty? ? {} : {page: @pagination_params}
|
153
|
+
else
|
154
|
+
@pagination_params
|
155
|
+
end
|
150
156
|
end
|
151
157
|
|
152
158
|
def includes_params
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_api_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Ching
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -164,6 +164,7 @@ files:
|
|
164
164
|
- lib/json_api_client/middleware/parse_json.rb
|
165
165
|
- lib/json_api_client/middleware/status.rb
|
166
166
|
- lib/json_api_client/paginating.rb
|
167
|
+
- lib/json_api_client/paginating/nested_param_paginator.rb
|
167
168
|
- lib/json_api_client/paginating/paginator.rb
|
168
169
|
- lib/json_api_client/parsers.rb
|
169
170
|
- lib/json_api_client/parsers/parser.rb
|