jsonapi-utils 0.5.1 → 0.5.2

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
  SHA1:
3
- metadata.gz: d5fa572a99867ebcee2a95e424b1f249c44603fd
4
- data.tar.gz: a7859fa30c45aff2cb66e43bce219cc691114310
3
+ metadata.gz: 95cb09b9a0147e9a991c8ac9b07cea55092560ab
4
+ data.tar.gz: a26c53529ce6737659dd75a1ae5f70f223317522
5
5
  SHA512:
6
- metadata.gz: 6351e38706a14d05e137f0daf257a44f98f3140eb4edc3b80306666b88581df8d069cdad8e264dce682d6c255a44c2b6d22c8a6188855240a31c43a3c91b23dc
7
- data.tar.gz: fd1fae5c0b4d81d21f52688ad5e44d1e92c3bb0302bcc6346bedf637b78db7c5e8165f85230d5559b5e1ca1e85f5b449c09362ff9d9fd425b7ed080cc617c89b
6
+ metadata.gz: 90bfc9584d1bca838113bd148dafff85cd31507f22b17279e87c544eed151a2ce68eaa2cbede848df4e95c0aa292cec590df7118ea5847a2f608891eb87483d2
7
+ data.tar.gz: ae340b7a231721a3505c67a8146bcaae2c03ba2a33a04dfa203f199f2839e49e6bffb826e314b83b782bb768947921e9675ba2d7ca5019d3e66c8551725e4931
data/README.md CHANGED
@@ -25,7 +25,7 @@ end
25
25
  ## Table of Contents
26
26
 
27
27
  * [Installation](#installation)
28
- * [How does it work?](#how-does-it-work)
28
+ * [Why JSONAPI::Utils?](#why-jsonapiutils)
29
29
  * [Usage](#usage)
30
30
  * [Response](#response)
31
31
  * [Renders](#renders)
@@ -53,19 +53,19 @@ end
53
53
 
54
54
  Support:
55
55
 
56
- * Ruby 2.x with Rails 4.x
56
+ * Ruby 1.9+ with Rails 4
57
57
  * Ruby 2.3+ with Rails 5
58
58
 
59
- For Rails 4.x add this to your application's Gemfile:
59
+ For Rails 4 add this to your application's Gemfile:
60
60
 
61
61
  ```ruby
62
- gem 'jsonapi-utils', '~> 0.4.8'
62
+ gem 'jsonapi-utils', '~> 0.4.9'
63
63
  ```
64
64
 
65
- For Rails 5, specify the `0.5.x` version in the Gemfile:
65
+ For Rails 5:
66
66
 
67
67
  ```ruby
68
- gem 'jsonapi-utils', '0.5.1'
68
+ gem 'jsonapi-utils', '~> 0.5.2'
69
69
  ```
70
70
 
71
71
  And then execute:
@@ -74,11 +74,11 @@ And then execute:
74
74
  $ bundle
75
75
  ```
76
76
 
77
- ## How does it work?
77
+ ## Why JSONAPI::Utils?
78
78
 
79
- 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).
79
+ One of the main motivations behind `JSONAPI::Utils` is to keep things explicit in controllers (no hidden actions :-) so that developers can easily understand and maintain their code.
80
80
 
81
- In both layers (request and response) JU communicates with some JR's objects in order to validate requests and render responses properly.
81
+ Unlike `JSONAPI::Resources` (JR), JU doesn't care about how you will operate your controller actions. The gem deals only with the request validation and response rendering (via JR's objects) and provides a set of helpers (renders, formatters etc) along the way. Thus developers can decide how to actually operate their actions: service objects, interactors etc.
82
82
 
83
83
  ## Usage
84
84
 
@@ -93,7 +93,7 @@ JU brings two main renders to the game, working pretty much the same way as Rail
93
93
 
94
94
  **jsonapi_render**
95
95
 
96
- It takes the arguments and generates a JSON API-compliant response.
96
+ It renders a JSON API-compliant response.
97
97
 
98
98
  ```ruby
99
99
  # app/controllers/users_controller.rb
@@ -138,7 +138,7 @@ jsonapi_render json: { data: [{ id: 1, first_name: 'Tiago' }, { id: 2, first_nam
138
138
 
139
139
  **jsonapi_render_errors**
140
140
 
141
- It takes arguments and generates a JSON API-compliant error response.
141
+ It renders a JSON API-compliant error response.
142
142
 
143
143
  ```ruby
144
144
  # app/controllers/users_controller.rb
@@ -171,7 +171,7 @@ jsonapi_render_errors json: errors, status: :unprocessable_entity
171
171
 
172
172
  #### Formatters
173
173
 
174
- 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.
174
+ In the backstage these are the guys which actually parse the ActiveRecord/Hash object to build a new Hash compliant with JSON API's specs. Formatters can be called anywhere in controllers being very useful if you need to do some work with the response's body before rendering the actual response.
175
175
 
176
176
  > 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`.
177
177
 
@@ -193,14 +193,35 @@ Arguments:
193
193
 
194
194
  #### Paginators
195
195
 
196
- If you are rendering a collection of hashes, you'll need to implement the `#pagination_range` method, which returns a range for your resources. For example:
196
+ Pagination works out of the box on JU, you just need to decide which kind of paginator you'd like to use.
197
+
198
+ It's really easy to work with pagination on JU, actually it's just a matter of chosing the [paginator you wish](http://jsonapi-resources.com/v0.8/guide/configuration.html#Defaults) in your JR's config file:
199
+
200
+ ```ruby
201
+ # config/initializers/jsonapi_resources.rb
202
+ JSONAPI.configure do |config|
203
+ # :none, :offset, :paged, or a custom paginator name
204
+ config.default_paginator = :paged
205
+
206
+ # Output pagination links at top level
207
+ config.top_level_links_include_pagination = true
208
+
209
+ # Default sizes
210
+ config.default_page_size = 70
211
+ config.maximum_page_size = 100
212
+ end
213
+ ```
214
+
215
+ As you may have noticed above, it's possible to use custom paginators. In order to create your own paginator your just need to define a class which inherits from `JSONAPI::Paginator` and implements the `#pagination_range` method which in turn must return the range to be applied over the resulting collection.
216
+
217
+ For example, if you would like to paginate over a collection of hashes, you may implement the `#pagination_range` method as below:
197
218
 
198
219
  ```ruby
199
220
  class CustomPaginator < JSONAPI::Paginator
200
221
  def pagination_range(page_params)
201
222
  offset = page_params['offset']
202
223
  limit = JSONAPI.configuration.default_page_size
203
- offset..offset + limit - 1
224
+ offset..offset + limit - 1 # resulting range
204
225
  end
205
226
  ```
206
227
 
@@ -215,7 +236,7 @@ end
215
236
 
216
237
  ### Request
217
238
 
218
- 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:
239
+ Before a controller action gets executed, `JSONAPI::Utils` will validate the request against JSON API's specs 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:
219
240
 
220
241
  ```json
221
242
  HTTP/1.1 400 Bad Request
@@ -338,48 +359,51 @@ Finally, having inhirited `JSONAPI::Utils` methods from the `BaseController` we
338
359
 
339
360
  ```ruby
340
361
  # app/controllers/users_controller.rb
341
- # GET /users
342
- def index
343
- users = User.all
344
- jsonapi_render json: users
345
- end
362
+ class UsersController < BaseController
363
+ # GET /users
364
+ def index
365
+ users = User.all
366
+ jsonapi_render json: users
367
+ end
346
368
 
347
- # GET /users/:id
348
- def show
349
- user = User.find(params[:id])
350
- jsonapi_render json: user
351
- end
369
+ # GET /users/:id
370
+ def show
371
+ user = User.find(params[:id])
372
+ jsonapi_render json: user
373
+ end
352
374
 
353
- # POST /users
354
- def create
355
- user = User.new(resource_params)
356
- if user.save
357
- jsonapi_render json: user, status: :created
358
- else
359
- jsonapi_render_errors json: user, status: :unprocessable_entity
375
+ # POST /users
376
+ def create
377
+ user = User.new(resource_params)
378
+ if user.save
379
+ jsonapi_render json: user, status: :created
380
+ else
381
+ jsonapi_render_errors json: user, status: :unprocessable_entity
382
+ end
360
383
  end
361
- end
362
384
 
363
- # PATCH /users/:id
364
- def update
365
- user = User.find(params[:id])
366
- if user.update(resource_params)
367
- jsonapi_render json: user
368
- else
369
- jsonapi_render_errors json: user, status: :unprocessable_entity
385
+ # PATCH /users/:id
386
+ def update
387
+ user = User.find(params[:id])
388
+ if user.update(resource_params)
389
+ jsonapi_render json: user
390
+ else
391
+ jsonapi_render_errors json: user, status: :unprocessable_entity
392
+ end
370
393
  end
371
- end
372
394
 
373
- # DELETE /users/:id
374
- def destroy
375
- User.find(params[:id]).destroy
376
- head :no_content
395
+ # DELETE /users/:id
396
+ def destroy
397
+ User.find(params[:id]).destroy
398
+ head :no_content
399
+ end
377
400
  end
378
401
  ```
379
402
 
380
403
  And:
381
404
 
382
405
  ```ruby
406
+ # app/controllers/posts_controller.rb
383
407
  class PostsController < BaseController
384
408
  before_action :load_user, except: :create
385
409
 
@@ -393,7 +417,7 @@ class PostsController < BaseController
393
417
  jsonapi_render json: @user.posts.find(params[:id])
394
418
  end
395
419
 
396
- # POST /users
420
+ # POST /posts
397
421
  def create
398
422
  post = Post.new(post_params)
399
423
  if post.save
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Utils
3
- VERSION = '0.5.1'.freeze
3
+ VERSION = '0.5.2'.freeze
4
4
  end
5
5
  end
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.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tiago Guedes
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-01-20 00:00:00.000000000 Z
12
+ date: 2017-03-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jsonapi-resources
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - '='
19
19
  - !ruby/object:Gem::Version
20
- version: 0.8.0
20
+ version: 0.8.3
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.3
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: bundler
30
30
  requirement: !ruby/object:Gem::Requirement