json_api_responders 2.2.0 → 2.3.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
  SHA1:
3
- metadata.gz: 200e399a5830497129bc96622521fb1babb4439a
4
- data.tar.gz: 3e6e7b7f11eee794f26cb7716e82810b82c11256
3
+ metadata.gz: 6082cfc85b741c549db6f0d8c2a3c430c4723c96
4
+ data.tar.gz: d60acf9a61be1307044fb219dded909308438b0d
5
5
  SHA512:
6
- metadata.gz: 52b66e6ad1823e222a2b12ee78ff147dda45b87ff085a47eadfbb439edfd99d340304d7390cf34d037978983c7aafd4053b2a47b2c179203d6365fe3f35c051b
7
- data.tar.gz: 28b6d03dc79cc014aac159f5cc1058d17823d0fcc7fd170971374192110f39870da102f71cf53726a45d85728824f8eb7e5e6e941e782eecf27fc575e8d9cb43
6
+ metadata.gz: 120db92c0eeb5c5daec62e49903df4561ea56a24661c67fcbe873511a9a68569181eb40332d76f1abc75aac8a272b098ffccbeca0b4cce1bcb7e43470da09d03
7
+ data.tar.gz: b580a033b90d92a053ab0ea37de03d6c809613343230f9b2b6de11641119452572f2b4d0a25886d201e9dfd02fbbf4c16dfd017fb00531ec62da65314177dd4e
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # JsonApiResponders
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/json_api_responders.svg)](https://badge.fury.io/rb/json_api_responders)
4
- [![Build Status](https://semaphoreci.com/api/v1/infinum/json_api_responders/branches/features-missing_responses/shields_badge.svg)](https://semaphoreci.com/infinum/json_api_responders)
4
+ [![Build Status](https://semaphoreci.com/api/v1/infinum/json_api_responders/branches/master/shields_badge.svg)](https://semaphoreci.com/infinum/json_api_responders)
5
5
  [![Code Climate](https://codeclimate.com/github/infinum/json_api_responders/badges/gpa.svg)](https://codeclimate.com/github/infinum/json_api_responders)
6
6
  [![Test Coverage](https://codeclimate.com/github/infinum/json_api_responders/badges/coverage.svg)](https://codeclimate.com/github/infinum/json_api_responders/coverage)
7
7
 
8
- This gem gives a few convinient methods for working with JSONAPI. It is inspired by responders gem.
8
+ This gem gives a few convenient methods for working with JSONAPI. It is inspired by the [responders](https://github.com/plataformatec/responders) gem.
9
9
 
10
10
  ## Installation
11
11
 
@@ -19,16 +19,43 @@ And then execute:
19
19
 
20
20
  $ bundle
21
21
 
22
+ Inside your base controller, include the module:
23
+
24
+ ```ruby
25
+ module Api
26
+ module V1
27
+ class BaseController < ApplicationController
28
+ include JsonApiResponders
29
+ end
30
+ end
31
+ end
32
+ ```
33
+
22
34
  ## Usage
23
35
 
36
+ This gem comes with the two following methods `respond_with` and `respond_with_error`.
37
+
38
+ #### `respond_with(resource, options = {}) `
39
+ This method requires a resource as a parameter, and you can pass some options if you wish. Any options you do choose to pass into `respond_with` will be passed on to the `controller.render` method. In the [Configuration section](#configuration) you can learn how to set mandatory options. Bellow you will find a few examples on how to use this method:
40
+
24
41
  user = User.first
25
42
  respond_with user
26
- respond_with user, on_error: { status: :unauthorized, detail: 'Invalid user or password' }
27
- respond_with_error, status: 401, detail: 'Bad credentials'
28
43
 
29
- whatever can be passed to controller.render can be passed here:
44
+ The above example will render the **User** object.
45
+
46
+ user = User.first
47
+ respond_with user, on_error: {
48
+ : :unauthorized, detail: 'Invalid user or password' }
49
+
50
+ The above example will render an **Error** response if an error would occur.
51
+
52
+ #### `respond_with_error(status, detail = nil)`
53
+ This method requires HTTP status code and an optional parameter explaining the error. This method will render an error message as described in the JSON API specification. Below you can see an example of how it should be used:
54
+
55
+ respond_with_error(401, 'Bad credentials')
56
+ respond_with_error(404, 'Not found')
57
+ respond_with_error(400, 'Bad request')
30
58
 
31
- respond_with user, serializer: UserSerializer
32
59
 
33
60
  ## Configuration
34
61
  Currently you can only configure which options are required to be passed through the `respond_with` method. These required options are categorized by the controller's actions. Bellow you can find an example:
@@ -75,6 +102,30 @@ If `:serializer` was left out of the above `respond_with` method you would see t
75
102
 
76
103
  head status: 204
77
104
 
105
+ ## Error translations
106
+
107
+ `json_api_responders` has translation support for error title and details. Copy & paste this file to your `config/locales` folder:
108
+
109
+ ```yml
110
+ en:
111
+ json_api:
112
+ errors:
113
+ not_found:
114
+ title: Not found
115
+ detail: Resource not found
116
+ forbidden:
117
+ title: Unauthorized
118
+ detail: User is not authorized to use this resource
119
+ unprocessable_entity:
120
+ title: Unprocessable Entity
121
+ details: Cannot process request
122
+ conflict:
123
+ title: Invalid Attribute
124
+ details: Something is missing
125
+ ```
126
+
127
+ It translates using the format `I18n.t("json_api.errors.#{human_readable_status_code}.title")`
128
+
78
129
  ## Contributing
79
130
 
80
131
  Bug reports and pull requests are welcome on GitHub at https://github.com/infinum/json_api_responders.
@@ -1,6 +1,8 @@
1
1
  module JsonApiResponders
2
2
  class Config
3
3
  attr_reader :required_options
4
+ DEFAULT_RENDER_METHOD = :json
5
+ RENDER_METHODS = [:jsonapi, :json]
4
6
 
5
7
  def required_options=(opts = {})
6
8
  @required_options = opts
@@ -17,6 +19,18 @@ module JsonApiResponders
17
19
  end
18
20
  end
19
21
 
22
+ def render_method
23
+ @render_method || DEFAULT_RENDER_METHOD
24
+ end
25
+
26
+ def render_method=(render_method)
27
+ if RENDER_METHODS.include?(render_method)
28
+ @render_method = render_method
29
+ else
30
+ raise JsonApiResponders::Errors::InvalidRenderMethodError, render_method
31
+ end
32
+ end
33
+
20
34
  private
21
35
 
22
36
  def action(options)
@@ -33,6 +33,19 @@ module JsonApiResponders
33
33
  end
34
34
  end
35
35
 
36
+ class InvalidRenderMethodError < StandardError
37
+ attr_reader :render_method
38
+
39
+ def initialize(render_method)
40
+ @render_method = render_method
41
+ super(message)
42
+ end
43
+
44
+ def message
45
+ "#{render_method} render method is invalid, must be one of: #{JsonApiResponders::Config::RENDER_METHODS}"
46
+ end
47
+ end
48
+
36
49
  class UnknownAction < StandardError
37
50
  attr_reader :action
38
51
 
@@ -43,7 +43,7 @@ module JsonApiResponders
43
43
  end
44
44
 
45
45
  def resource_render_options
46
- render_options.merge(json: resource, **options)
46
+ render_options.merge(Hash[render_method, resource].merge(**options))
47
47
  end
48
48
 
49
49
  private
@@ -52,6 +52,10 @@ module JsonApiResponders
52
52
  resource.respond_to?(:errors) && resource.errors.any?
53
53
  end
54
54
 
55
+ def render_method
56
+ JsonApiResponders.config.render_method
57
+ end
58
+
55
59
  end
56
60
  end
57
61
  end
@@ -1,6 +1,6 @@
1
1
  module JsonApiResponders
2
2
  MAJOR = 2
3
- MINOR = 2
3
+ MINOR = 3
4
4
  PATCH = 0
5
5
  VERSION = [MAJOR, MINOR, PATCH].join('.').freeze
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_responders
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stanko Krtalić Rusendić
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-14 00:00:00.000000000 Z
11
+ date: 2017-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  version: '0'
154
154
  requirements: []
155
155
  rubyforge_project:
156
- rubygems_version: 2.5.1
156
+ rubygems_version: 2.6.11
157
157
  signing_key:
158
158
  specification_version: 4
159
159
  summary: Automatically respond to JSON::API requests