jsonapi-utils 0.5.0.beta1 → 0.5.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -27
- data/lib/jsonapi/utils.rb +4 -0
- data/lib/jsonapi/utils/response/formatters.rb +5 -4
- data/lib/jsonapi/utils/response/support.rb +3 -3
- data/lib/jsonapi/utils/support/filter/custom.rb +22 -0
- data/lib/jsonapi/utils/support/filter/default.rb +32 -0
- data/lib/jsonapi/utils/support/pagination.rb +94 -34
- data/lib/jsonapi/utils/version.rb +1 -1
- metadata +46 -32
- data/lib/jsonapi/utils/support/filter.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96d7f50884d79e1dbb367adf611cb2f52d7ebf27
|
4
|
+
data.tar.gz: 67b77e50094f0a223c4bb1220108fcd8f890dcb6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06fe4857feebe4f2fda0419c6b83cb5d85335f069c63e997d5c63746b989dcd251360f765d60047e525d97a15dac0c0ab41f78f7b131f3353299251598e804c0
|
7
|
+
data.tar.gz: e024c8a81831e7a329fb001f676cf24dbec54488514700788bc13a90eb41f0e0c925c629eff0ca09e07e47e8534a1731c7346195e8e526c453eaf523e712756d
|
data/README.md
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# JSONAPI::Utils
|
2
2
|
|
3
|
-
[![Code Climate](https://codeclimate.com/github/
|
3
|
+
[![Code Climate](https://codeclimate.com/github/tiagopog/jsonapi-utils/badges/gpa.svg)](https://codeclimate.com/github/tiagopog/jsonapi-utils)
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/jsonapi-utils.svg)](https://badge.fury.io/rb/jsonapi-utils)
|
5
|
-
[![Build Status](https://travis-ci.org/
|
5
|
+
[![Build Status](https://travis-ci.org/tiagopog/jsonapi-utils.svg?branch=master)](https://travis-ci.org/tiagopog/jsonapi-utils)
|
6
6
|
|
7
7
|
Simple yet powerful way to get your Rails API compliant with [JSON API](http://jsonapi.org).
|
8
8
|
|
9
|
-
JSONAPI::Utils (JU) is built on top of [JSONAPI::Resources](https://github.com/cerebris/jsonapi-resources) taking advantage of its resource-driven style and bringing
|
9
|
+
`JSONAPI::Utils` (JU) is built on top of [JSONAPI::Resources](https://github.com/cerebris/jsonapi-resources) taking advantage of its resource-driven style and bringing an easy way to build modern JSON APIs with no or less learning curve.
|
10
10
|
|
11
|
-
## Contents
|
11
|
+
## Table of Contents
|
12
12
|
|
13
13
|
* [Installation](#installation)
|
14
14
|
* [How does it work?](#how-does-it-work)
|
@@ -21,9 +21,9 @@ JSONAPI::Utils (JU) is built on top of [JSONAPI::Resources](https://github.com/c
|
|
21
21
|
* [Full example](#full-example)
|
22
22
|
* [Models](#models)
|
23
23
|
* [Resources](#resources)
|
24
|
-
* [Routes & Controllers](routes--controllers)
|
24
|
+
* [Routes & Controllers](#routes--controllers)
|
25
25
|
* [Initializer](#initializer)
|
26
|
-
* [Requests & Responses](requests--responses)
|
26
|
+
* [Requests & Responses](#requests--responses)
|
27
27
|
* [Index](#index)
|
28
28
|
* [Index (options)](#index-options)
|
29
29
|
* [Show](#show)
|
@@ -36,17 +36,23 @@ JSONAPI::Utils (JU) is built on top of [JSONAPI::Resources](https://github.com/c
|
|
36
36
|
|
37
37
|
## Installation
|
38
38
|
|
39
|
-
|
39
|
+
Support:
|
40
40
|
|
41
|
-
* Ruby
|
42
|
-
* Ruby
|
41
|
+
* Ruby 2.x with Rails 4.x
|
42
|
+
* Ruby 2.2.2+ with Rails 5
|
43
43
|
|
44
|
-
|
44
|
+
For Rails 4.x add this to your application's Gemfile:
|
45
45
|
|
46
46
|
```ruby
|
47
47
|
gem 'jsonapi-utils', '~> 0.4.6'
|
48
48
|
```
|
49
49
|
|
50
|
+
For Rails 5, specify the beta version in the Gemfile:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
gem 'jsonapi-utils', '0.5.0.beta2'
|
54
|
+
```
|
55
|
+
|
50
56
|
And then execute:
|
51
57
|
|
52
58
|
```shell
|
@@ -55,9 +61,9 @@ $ bundle
|
|
55
61
|
|
56
62
|
## How does it work?
|
57
63
|
|
58
|
-
One of the main motivations behind `JSONAPI::Utils` is to keep things explicit in
|
64
|
+
One of the main motivations behind `JSONAPI::Utils` is to keep things explicit in controllers so that developers can easily understand and maintain code. Unlike `JSONAPI::Resources` (JR), JU doesn't care about controller operations – what happens with resources within actions – dealing only with the request validation and response rendering and providing some useful helpers. This way developers can decide how to actually operate their actions (service objects, interactors etc).
|
59
65
|
|
60
|
-
In both layers (request and response) JU communicates with some
|
66
|
+
In both layers (request and response) JU communicates with some JR's objects in order to validate requests and render responses properly.
|
61
67
|
|
62
68
|
## Usage
|
63
69
|
|
@@ -150,13 +156,13 @@ jsonapi_render_errors json: errors, status: :unprocessable_entity
|
|
150
156
|
|
151
157
|
#### Formatters
|
152
158
|
|
153
|
-
In the backstage those are the guys which actually parse ActiveRecord/Hash objects and build a new Hash compliant with JSON API. They can be called anywhere in controllers being very useful if you need to
|
159
|
+
In the backstage those are the guys which actually parse ActiveRecord/Hash objects and build a new Hash compliant with JSON API. They can be called anywhere in controllers being very useful if you need to do some work with the response's body before rendering the response.
|
154
160
|
|
155
|
-
Note: the resulting Hash from those methods can not be passed as argument to `JSONAPI::Utils#jsonapi_render` or `JSONAPI::Utils#jsonapi_render_error`, instead it needs to be rendered by the usual `ActionController#render`.
|
161
|
+
> Note: the resulting Hash from those methods can not be passed as argument to `JSONAPI::Utils#jsonapi_render` or `JSONAPI::Utils#jsonapi_render_error`, instead it needs to be rendered by the usual `ActionController#render`.
|
156
162
|
|
157
163
|
**jsonapi_format**
|
158
164
|
|
159
|
-
|
165
|
+
> Because of semantic reasons `JSONAPI::Utils#jsonapi_serialize` was renamed to `JSONAPI::Utils#jsonapi_format`.
|
160
166
|
|
161
167
|
```ruby
|
162
168
|
# app/controllers/users_controller.rb
|
@@ -172,7 +178,7 @@ Arguments:
|
|
172
178
|
|
173
179
|
### Request
|
174
180
|
|
175
|
-
Before
|
181
|
+
Before a controller's action gets executed, `JSONAPI::Utils` will validate the request against JSON API specifications as well as evaluating the eventual query string params to check if they match the resource's definition. If something goes wrong during the validation process, JU will render an error response like this examples below:
|
176
182
|
|
177
183
|
```json
|
178
184
|
HTTP/1.1 400 Bad Request
|
@@ -204,7 +210,7 @@ Content-Type: application/vnd.api+json
|
|
204
210
|
|
205
211
|
#### Params helpers
|
206
212
|
|
207
|
-
JU brings helper methods as a shortcut to get values from permitted params based on the resource configuration.
|
213
|
+
JU brings helper methods as a shortcut to get values from permitted params based on the resource's configuration.
|
208
214
|
|
209
215
|
- `resource_params`:
|
210
216
|
- Returns the permitted params present in the `attributes` JSON member;
|
@@ -217,14 +223,14 @@ JU brings helper methods as a shortcut to get values from permitted params based
|
|
217
223
|
|
218
224
|
## Full example
|
219
225
|
|
220
|
-
|
226
|
+
After installing the gem you simply need to:
|
221
227
|
|
222
|
-
1. Include
|
228
|
+
1. Include its module (`include JSONAPI::Utils`) in controller (eg. `BaseController`);
|
223
229
|
2. Define the resources for your models;
|
224
230
|
3. Define routes;
|
225
|
-
4. Use JU's render methods.
|
231
|
+
4. Use JU's render/helper methods.
|
226
232
|
|
227
|
-
|
233
|
+
Ok, it's time for a more complete example. Let's say we have a Rails application for a super simple blog:
|
228
234
|
|
229
235
|
### Models
|
230
236
|
|
@@ -244,13 +250,12 @@ end
|
|
244
250
|
|
245
251
|
### Resources
|
246
252
|
|
247
|
-
Here is where we define how the
|
253
|
+
Here is where we define how the models are exposed as resource on the API:
|
248
254
|
|
249
255
|
```ruby
|
250
256
|
# app/resources/user_resource.rb
|
251
257
|
class UserResource < JSONAPI::Resource
|
252
258
|
attributes :first_name, :last_name, :full_name, :birthday
|
253
|
-
attribute :full_name
|
254
259
|
|
255
260
|
has_many :posts
|
256
261
|
|
@@ -268,7 +273,7 @@ end
|
|
268
273
|
|
269
274
|
### Routes & Controllers
|
270
275
|
|
271
|
-
Let's define
|
276
|
+
Let's define the routes using the `jsonapi_resources` and `jsonapi_links` methods provided by JR:
|
272
277
|
|
273
278
|
```ruby
|
274
279
|
Rails.application.routes.draw do
|
@@ -279,9 +284,9 @@ Rails.application.routes.draw do
|
|
279
284
|
end
|
280
285
|
```
|
281
286
|
|
282
|
-
In
|
287
|
+
In controllers we just need to include the `JSONAPI::Utils` module.
|
283
288
|
|
284
|
-
> Note:
|
289
|
+
> Note: you may want to define some default rendering like the below example using `jsonapi_render_not_found` for when ActiveRecord doesn't find a record.
|
285
290
|
|
286
291
|
```ruby
|
287
292
|
# app/controllers/base_controller.rb
|
@@ -411,7 +416,7 @@ You may want a different configuration for your API. For more information check
|
|
411
416
|
|
412
417
|
### Requests & Responses
|
413
418
|
|
414
|
-
Here
|
419
|
+
Here are examples of requests – based on those sample [controllers](#routes--controllers) – and their respective JSON responses.
|
415
420
|
|
416
421
|
* [Collection](#collection)
|
417
422
|
* [Collection (options)](#collection-options)
|
data/lib/jsonapi/utils.rb
CHANGED
@@ -3,6 +3,10 @@ require 'jsonapi/utils/version'
|
|
3
3
|
require 'jsonapi/utils/exceptions'
|
4
4
|
require 'jsonapi/utils/request'
|
5
5
|
require 'jsonapi/utils/response'
|
6
|
+
require 'jsonapi/utils/support/filter/custom'
|
7
|
+
require 'piped_ruby'
|
8
|
+
|
9
|
+
JSONAPI::Resource.extend JSONAPI::Utils::Support::Filter::Custom
|
6
10
|
|
7
11
|
module JSONAPI
|
8
12
|
module Utils
|
@@ -57,10 +57,11 @@ module JSONAPI
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def build_collection(records, options = {})
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
60
|
+
-> { apply_filter(records, options) }
|
61
|
+
.>> { |filtered| apply_pagination(filtered, options) }
|
62
|
+
.>> { |paginated| apply_sort(paginated) }
|
63
|
+
.>> { |result| result.map { |record| turn_into_resource(record, options) } }
|
64
|
+
.unwrap
|
64
65
|
end
|
65
66
|
|
66
67
|
def turn_into_resource(record, options = {})
|
@@ -1,14 +1,14 @@
|
|
1
|
-
require 'jsonapi/utils/support/
|
1
|
+
require 'jsonapi/utils/support/error'
|
2
|
+
require 'jsonapi/utils/support/filter/default'
|
2
3
|
require 'jsonapi/utils/support/pagination'
|
3
4
|
require 'jsonapi/utils/support/sort'
|
4
|
-
require 'jsonapi/utils/support/error'
|
5
5
|
|
6
6
|
module JSONAPI
|
7
7
|
module Utils
|
8
8
|
module Response
|
9
9
|
module Support
|
10
10
|
include ::JSONAPI::Utils::Support::Error
|
11
|
-
include ::JSONAPI::Utils::Support::Filter
|
11
|
+
include ::JSONAPI::Utils::Support::Filter::Default
|
12
12
|
include ::JSONAPI::Utils::Support::Pagination
|
13
13
|
include ::JSONAPI::Utils::Support::Sort
|
14
14
|
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module JSONAPI::Utils::Support::Filter
|
2
|
+
module Custom
|
3
|
+
def _custom_filters
|
4
|
+
@_allowed_custom_filters || []
|
5
|
+
end
|
6
|
+
|
7
|
+
def custom_filters(*attrs)
|
8
|
+
attrs.each { |attr| custom_filter(attr) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def custom_filter(attr)
|
12
|
+
attr = attr.to_sym
|
13
|
+
@_allowed_filters[attr] = {}
|
14
|
+
|
15
|
+
if !@_allowed_custom_filters.is_a?(Array)
|
16
|
+
@_allowed_custom_filters = Array(attr)
|
17
|
+
elsif @_allowed_custom_filters.include?(attr)
|
18
|
+
@_allowed_custom_filters.push(attr)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module JSONAPI::Utils::Support::Filter
|
2
|
+
module Default
|
3
|
+
def apply_filter(records, options = {})
|
4
|
+
if apply_filter?(records, options)
|
5
|
+
records.where(filter_params)
|
6
|
+
else
|
7
|
+
records
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def apply_filter?(records, options = {})
|
12
|
+
params[:filter].present? && records.respond_to?(:where) &&
|
13
|
+
(options[:filter].nil? || options[:filter])
|
14
|
+
end
|
15
|
+
|
16
|
+
def filter_params
|
17
|
+
@_filter_params ||=
|
18
|
+
case params[:filter]
|
19
|
+
when Hash, ActionController::Parameters
|
20
|
+
default_filters.each_with_object({}) do |resource, hash|
|
21
|
+
hash[resource] = params[:filter][resource]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def default_filters
|
29
|
+
params[:filter].keys.map(&:to_sym) - @request.resource_klass._custom_filters
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -2,64 +2,124 @@ module JSONAPI
|
|
2
2
|
module Utils
|
3
3
|
module Support
|
4
4
|
module Pagination
|
5
|
+
# Apply proper pagination to the records.
|
6
|
+
#
|
7
|
+
# @param records [ActiveRecord::Relation, Array] collection of records
|
8
|
+
# e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }]
|
9
|
+
#
|
10
|
+
# @param options [Hash] JU's options
|
11
|
+
# e.g.: { resource: V2::UserResource, count: 100 }
|
12
|
+
#
|
13
|
+
# @return [ActiveRecord::Relation, Array]
|
14
|
+
#
|
15
|
+
# @api public
|
5
16
|
def apply_pagination(records, options = {})
|
6
17
|
return records unless apply_pagination?(options)
|
18
|
+
records.is_a?(Array) ? records[paginate_with(:range)] : paginate_with(:paginator).apply(records, nil)
|
19
|
+
end
|
7
20
|
|
8
|
-
|
21
|
+
# Mount pagination params for JSONAPI::ResourcesOperationResult.
|
22
|
+
# It can also be used anywhere else as a helper method.
|
23
|
+
#
|
24
|
+
# @param records [ActiveRecord::Relation, Array] collection of records
|
25
|
+
# e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }]
|
26
|
+
#
|
27
|
+
# @param options [Hash] JU's options
|
28
|
+
# e.g.: { resource: V2::UserResource, count: 100 }
|
29
|
+
#
|
30
|
+
# @return [Hash]
|
31
|
+
# e.g.: {"first"=>{"number"=>1, "size"=>2}, "next"=>{"number"=>2, "size"=>2}, "last"=>{"number"=>2, "size"=>2}}
|
32
|
+
#
|
33
|
+
# @api public
|
34
|
+
def pagination_params(records, options)
|
35
|
+
return {} unless JSONAPI.configuration.top_level_links_include_pagination
|
36
|
+
paginator.links_page_params(record_count: count_records(records, options))
|
37
|
+
end
|
9
38
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
39
|
+
private
|
40
|
+
|
41
|
+
# Define the paginator object to be used in the response's pagination.
|
42
|
+
#
|
43
|
+
# @return [PagedPaginator, OffsetPaginator]
|
44
|
+
#
|
45
|
+
# @api private
|
46
|
+
def paginator
|
47
|
+
@paginator ||=
|
48
|
+
if JSONAPI.configuration.default_paginator == :paged
|
49
|
+
PagedPaginator.new(page_params)
|
50
|
+
elsif JSONAPI.configuration.default_paginator == :offset
|
51
|
+
OffsetPaginator.new(page_params)
|
15
52
|
end
|
16
53
|
end
|
17
54
|
|
55
|
+
# Check whether pagination should be applied to the response.
|
56
|
+
#
|
57
|
+
# @return [Boolean]
|
58
|
+
#
|
59
|
+
# @api private
|
18
60
|
def apply_pagination?(options)
|
19
61
|
JSONAPI.configuration.default_paginator != :none &&
|
20
62
|
(options[:paginate].nil? || options[:paginate])
|
21
63
|
end
|
22
64
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
else
|
31
|
-
{}
|
32
|
-
end
|
65
|
+
# Creates an instance of ActionController::Parameters for page params.
|
66
|
+
#
|
67
|
+
# @return [ActionController::Parameters]
|
68
|
+
#
|
69
|
+
# @api private
|
70
|
+
def page_params
|
71
|
+
@page_params ||= ActionController::Parameters.new(@request.params[:page] || {})
|
33
72
|
end
|
34
73
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
74
|
+
# Define the paginator or range according to the pagination strategy.
|
75
|
+
#
|
76
|
+
# @param kind [Symbol] pagination object's kind
|
77
|
+
# e.g.: :paginator or :range
|
78
|
+
#
|
79
|
+
# @return [PagedPaginator, OffsetPaginator, Range]
|
80
|
+
# e.g.: #<PagedPaginator:0x00561ed06dc5a0 @number=1, @size=2>
|
81
|
+
# 0..9
|
82
|
+
#
|
83
|
+
# @api private
|
84
|
+
def paginate_with(kind)
|
85
|
+
@pagination ||=
|
86
|
+
case kind
|
87
|
+
when :paginator then paginator
|
88
|
+
when :range then pagination_range
|
43
89
|
end
|
44
90
|
end
|
45
91
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
92
|
+
# Define a pagination range for objects which quack like Arrays.
|
93
|
+
#
|
94
|
+
# @return [Range]
|
95
|
+
# e.g.: 0..9
|
96
|
+
#
|
97
|
+
# @api private
|
98
|
+
def pagination_range
|
99
|
+
case JSONAPI.configuration.default_paginator
|
100
|
+
when :paged
|
50
101
|
number = page_params['number'].to_i.nonzero? || 1
|
51
102
|
size = page_params['size'].to_i.nonzero? || JSONAPI.configuration.default_page_size
|
52
|
-
|
53
|
-
|
54
|
-
@_paginator ||= OffsetPaginator.new(page_params)
|
103
|
+
(number - 1) * size..number * size - 1
|
104
|
+
when :offset
|
55
105
|
offset = page_params['offset'].to_i.nonzero? || 0
|
56
106
|
limit = page_params['limit'].to_i.nonzero? || JSONAPI.configuration.default_page_size
|
57
|
-
|
58
|
-
else
|
59
|
-
{}
|
107
|
+
offset..offset + limit - 1
|
60
108
|
end
|
61
109
|
end
|
62
110
|
|
111
|
+
# Count records in order to build a proper pagination and to fill up the "record_count" response's member.
|
112
|
+
#
|
113
|
+
# @param records [ActiveRecord::Relation, Array] collection of records
|
114
|
+
# e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }]
|
115
|
+
#
|
116
|
+
# @param options [Hash] JU's options
|
117
|
+
# e.g.: { resource: V2::UserResource, count: 100 }
|
118
|
+
#
|
119
|
+
# @return [Integer]
|
120
|
+
# e.g.: 42
|
121
|
+
#
|
122
|
+
# @api private
|
63
123
|
def count_records(records, options)
|
64
124
|
if options[:count].present?
|
65
125
|
options[:count]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.0.
|
4
|
+
version: 0.5.0.beta2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tiago Guedes
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-09-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: jsonapi-resources
|
@@ -17,138 +17,152 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - '='
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: 0.8.0.
|
20
|
+
version: 0.8.0.beta2
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - '='
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: 0.8.0.
|
27
|
+
version: 0.8.0.beta2
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: piped_ruby
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 0.2.1
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 0.2.1
|
28
42
|
- !ruby/object:Gem::Dependency
|
29
43
|
name: bundler
|
30
44
|
requirement: !ruby/object:Gem::Requirement
|
31
45
|
requirements:
|
32
|
-
- -
|
46
|
+
- - ~>
|
33
47
|
- !ruby/object:Gem::Version
|
34
48
|
version: '1.10'
|
35
49
|
type: :development
|
36
50
|
prerelease: false
|
37
51
|
version_requirements: !ruby/object:Gem::Requirement
|
38
52
|
requirements:
|
39
|
-
- -
|
53
|
+
- - ~>
|
40
54
|
- !ruby/object:Gem::Version
|
41
55
|
version: '1.10'
|
42
56
|
- !ruby/object:Gem::Dependency
|
43
57
|
name: rake
|
44
58
|
requirement: !ruby/object:Gem::Requirement
|
45
59
|
requirements:
|
46
|
-
- -
|
60
|
+
- - ~>
|
47
61
|
- !ruby/object:Gem::Version
|
48
62
|
version: '10.0'
|
49
63
|
type: :development
|
50
64
|
prerelease: false
|
51
65
|
version_requirements: !ruby/object:Gem::Requirement
|
52
66
|
requirements:
|
53
|
-
- -
|
67
|
+
- - ~>
|
54
68
|
- !ruby/object:Gem::Version
|
55
69
|
version: '10.0'
|
56
70
|
- !ruby/object:Gem::Dependency
|
57
71
|
name: sqlite3
|
58
72
|
requirement: !ruby/object:Gem::Requirement
|
59
73
|
requirements:
|
60
|
-
- -
|
74
|
+
- - '>='
|
61
75
|
- !ruby/object:Gem::Version
|
62
76
|
version: '0'
|
63
77
|
type: :development
|
64
78
|
prerelease: false
|
65
79
|
version_requirements: !ruby/object:Gem::Requirement
|
66
80
|
requirements:
|
67
|
-
- -
|
81
|
+
- - '>='
|
68
82
|
- !ruby/object:Gem::Version
|
69
83
|
version: '0'
|
70
84
|
- !ruby/object:Gem::Dependency
|
71
85
|
name: rails
|
72
86
|
requirement: !ruby/object:Gem::Requirement
|
73
87
|
requirements:
|
74
|
-
- -
|
88
|
+
- - ~>
|
75
89
|
- !ruby/object:Gem::Version
|
76
90
|
version: '4.2'
|
77
91
|
type: :development
|
78
92
|
prerelease: false
|
79
93
|
version_requirements: !ruby/object:Gem::Requirement
|
80
94
|
requirements:
|
81
|
-
- -
|
95
|
+
- - ~>
|
82
96
|
- !ruby/object:Gem::Version
|
83
97
|
version: '4.2'
|
84
98
|
- !ruby/object:Gem::Dependency
|
85
99
|
name: rspec-rails
|
86
100
|
requirement: !ruby/object:Gem::Requirement
|
87
101
|
requirements:
|
88
|
-
- -
|
102
|
+
- - ~>
|
89
103
|
- !ruby/object:Gem::Version
|
90
104
|
version: '3.1'
|
91
105
|
type: :development
|
92
106
|
prerelease: false
|
93
107
|
version_requirements: !ruby/object:Gem::Requirement
|
94
108
|
requirements:
|
95
|
-
- -
|
109
|
+
- - ~>
|
96
110
|
- !ruby/object:Gem::Version
|
97
111
|
version: '3.1'
|
98
112
|
- !ruby/object:Gem::Dependency
|
99
113
|
name: factory_girl
|
100
114
|
requirement: !ruby/object:Gem::Requirement
|
101
115
|
requirements:
|
102
|
-
- -
|
116
|
+
- - ~>
|
103
117
|
- !ruby/object:Gem::Version
|
104
118
|
version: '4.5'
|
105
119
|
type: :development
|
106
120
|
prerelease: false
|
107
121
|
version_requirements: !ruby/object:Gem::Requirement
|
108
122
|
requirements:
|
109
|
-
- -
|
123
|
+
- - ~>
|
110
124
|
- !ruby/object:Gem::Version
|
111
125
|
version: '4.5'
|
112
126
|
- !ruby/object:Gem::Dependency
|
113
127
|
name: smart_rspec
|
114
128
|
requirement: !ruby/object:Gem::Requirement
|
115
129
|
requirements:
|
116
|
-
- -
|
130
|
+
- - ~>
|
117
131
|
- !ruby/object:Gem::Version
|
118
132
|
version: 0.1.4
|
119
133
|
type: :development
|
120
134
|
prerelease: false
|
121
135
|
version_requirements: !ruby/object:Gem::Requirement
|
122
136
|
requirements:
|
123
|
-
- -
|
137
|
+
- - ~>
|
124
138
|
- !ruby/object:Gem::Version
|
125
139
|
version: 0.1.4
|
126
140
|
- !ruby/object:Gem::Dependency
|
127
141
|
name: pry
|
128
142
|
requirement: !ruby/object:Gem::Requirement
|
129
143
|
requirements:
|
130
|
-
- -
|
144
|
+
- - ~>
|
131
145
|
- !ruby/object:Gem::Version
|
132
146
|
version: 0.10.3
|
133
147
|
type: :development
|
134
148
|
prerelease: false
|
135
149
|
version_requirements: !ruby/object:Gem::Requirement
|
136
150
|
requirements:
|
137
|
-
- -
|
151
|
+
- - ~>
|
138
152
|
- !ruby/object:Gem::Version
|
139
153
|
version: 0.10.3
|
140
154
|
- !ruby/object:Gem::Dependency
|
141
155
|
name: pry-byebug
|
142
156
|
requirement: !ruby/object:Gem::Requirement
|
143
157
|
requirements:
|
144
|
-
- -
|
158
|
+
- - '>='
|
145
159
|
- !ruby/object:Gem::Version
|
146
160
|
version: '0'
|
147
161
|
type: :development
|
148
162
|
prerelease: false
|
149
163
|
version_requirements: !ruby/object:Gem::Requirement
|
150
164
|
requirements:
|
151
|
-
- -
|
165
|
+
- - '>='
|
152
166
|
- !ruby/object:Gem::Version
|
153
167
|
version: '0'
|
154
168
|
description: A Rails way to get your API's data serialized through JSON API's specs
|
@@ -160,23 +174,24 @@ executables: []
|
|
160
174
|
extensions: []
|
161
175
|
extra_rdoc_files: []
|
162
176
|
files:
|
163
|
-
- CODE_OF_CONDUCT.md
|
164
|
-
- LICENSE.txt
|
165
|
-
- README.md
|
166
177
|
- bin/console
|
167
178
|
- bin/setup
|
168
|
-
- lib/jsonapi/utils.rb
|
169
179
|
- lib/jsonapi/utils/exceptions.rb
|
170
180
|
- lib/jsonapi/utils/request.rb
|
171
|
-
- lib/jsonapi/utils/response.rb
|
172
181
|
- lib/jsonapi/utils/response/formatters.rb
|
173
182
|
- lib/jsonapi/utils/response/renders.rb
|
174
183
|
- lib/jsonapi/utils/response/support.rb
|
184
|
+
- lib/jsonapi/utils/response.rb
|
175
185
|
- lib/jsonapi/utils/support/error.rb
|
176
|
-
- lib/jsonapi/utils/support/filter.rb
|
186
|
+
- lib/jsonapi/utils/support/filter/custom.rb
|
187
|
+
- lib/jsonapi/utils/support/filter/default.rb
|
177
188
|
- lib/jsonapi/utils/support/pagination.rb
|
178
189
|
- lib/jsonapi/utils/support/sort.rb
|
179
190
|
- lib/jsonapi/utils/version.rb
|
191
|
+
- lib/jsonapi/utils.rb
|
192
|
+
- LICENSE.txt
|
193
|
+
- README.md
|
194
|
+
- CODE_OF_CONDUCT.md
|
180
195
|
homepage: https://github.com/b2beauty/jsonapi-utils
|
181
196
|
licenses:
|
182
197
|
- MIT
|
@@ -187,20 +202,19 @@ require_paths:
|
|
187
202
|
- lib
|
188
203
|
required_ruby_version: !ruby/object:Gem::Requirement
|
189
204
|
requirements:
|
190
|
-
- -
|
205
|
+
- - '>='
|
191
206
|
- !ruby/object:Gem::Version
|
192
207
|
version: '0'
|
193
208
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
194
209
|
requirements:
|
195
|
-
- -
|
210
|
+
- - '>'
|
196
211
|
- !ruby/object:Gem::Version
|
197
212
|
version: 1.3.1
|
198
213
|
requirements: []
|
199
214
|
rubyforge_project:
|
200
|
-
rubygems_version: 2.
|
215
|
+
rubygems_version: 2.0.14.1
|
201
216
|
signing_key:
|
202
217
|
specification_version: 4
|
203
218
|
summary: JSON::Utils is a simple way to get a full-featured JSON API serialization
|
204
219
|
for your controller's responses.
|
205
220
|
test_files: []
|
206
|
-
has_rdoc:
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module JSONAPI
|
2
|
-
module Utils
|
3
|
-
module Support
|
4
|
-
module Filter
|
5
|
-
def apply_filter(records, options = {})
|
6
|
-
if apply_filter?(records, options)
|
7
|
-
records.where(filter_params)
|
8
|
-
else
|
9
|
-
records
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def apply_filter?(records, options = {})
|
14
|
-
params[:filter].present? && records.respond_to?(:where) &&
|
15
|
-
(options[:filter].nil? || options[:filter])
|
16
|
-
end
|
17
|
-
|
18
|
-
def filter_params
|
19
|
-
@_filter_params ||=
|
20
|
-
case params[:filter]
|
21
|
-
when Hash, ActionController::Parameters
|
22
|
-
params[:filter].keys.each_with_object({}) do |resource, hash|
|
23
|
-
hash[resource] = params[:filter][resource]
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|