json_api_responders 2.0.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +13 -1
- data/lib/json_api_responders.rb +14 -0
- data/lib/json_api_responders/config.rb +16 -0
- data/lib/json_api_responders/errors.rb +13 -0
- data/lib/json_api_responders/version.rb +2 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d9223310e0bc824e94e72d5724cb531009adce1
|
4
|
+
data.tar.gz: 3fca5112b3b31228a5a9a59772a341c203206bc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f4bde721bbf3b652dc37157227cc5d50756ef87cb337c156b39a6d6c0b0d74007500494e4b10cc7f00086fba4eafadb6bae13bcdc108c999f1a05e10e7085487
|
7
|
+
data.tar.gz: 42a5cd9abeecc0ffed50c3eb7e28e960d025c1289e62210c272a6127281bde6843ad1da9f6731bddfdf9b959551d0526acbdb6cc3b5534b5b1353931de31ea7f
|
data/README.md
CHANGED
@@ -28,7 +28,19 @@ And then execute:
|
|
28
28
|
|
29
29
|
whatever can be passed to controller.render can be passed here:
|
30
30
|
|
31
|
-
|
31
|
+
respond_with user, serializer: UserSerializer
|
32
|
+
|
33
|
+
## Configuration
|
34
|
+
Currently you can only configure which options are required to be passed through the `respond_with` method. Bellow you can find an example:
|
35
|
+
|
36
|
+
JsonApiResponders.configure do |config|
|
37
|
+
config.required_options = [:foo]
|
38
|
+
end
|
39
|
+
...
|
40
|
+
user = User.first
|
41
|
+
respond_with user, foo: :bar
|
42
|
+
|
43
|
+
If `:foo` was left out of the `respond_with` method you would see the `JsonApiResponders::Errors::RequiredOptionMissingError` be raised.
|
32
44
|
|
33
45
|
## Responses
|
34
46
|
|
data/lib/json_api_responders.rb
CHANGED
@@ -1,8 +1,21 @@
|
|
1
1
|
require 'json_api_responders/version'
|
2
2
|
require 'json_api_responders/errors'
|
3
3
|
require 'json_api_responders/responder'
|
4
|
+
require 'json_api_responders/config'
|
4
5
|
|
5
6
|
module JsonApiResponders
|
7
|
+
class << self
|
8
|
+
attr_writer :config
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configure
|
12
|
+
yield(config) if block_given?
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.config
|
16
|
+
@config ||= JsonApiResponders::Config.new
|
17
|
+
end
|
18
|
+
|
6
19
|
def self.included(base)
|
7
20
|
base.rescue_from ActiveRecord::RecordNotFound, with: :record_not_found!
|
8
21
|
base.rescue_from ActionController::ParameterMissing, with: :parameter_missing!
|
@@ -10,6 +23,7 @@ module JsonApiResponders
|
|
10
23
|
|
11
24
|
def respond_with(resource, options = {})
|
12
25
|
options = { params: params }.merge(options)
|
26
|
+
JsonApiResponders.config.check_required_options(options)
|
13
27
|
Responder.new(self, resource, options).respond!
|
14
28
|
end
|
15
29
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module JsonApiResponders
|
2
|
+
class Config
|
3
|
+
attr_reader :required_options
|
4
|
+
|
5
|
+
def required_options=(opts = [])
|
6
|
+
@required_options = opts
|
7
|
+
end
|
8
|
+
|
9
|
+
def check_required_options(options)
|
10
|
+
return if @required_options.nil?
|
11
|
+
@required_options.each do |key|
|
12
|
+
raise JsonApiResponders::Errors::RequiredOptionMissingError, key unless options.key? key
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -20,6 +20,19 @@ module JsonApiResponders
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
+
class RequiredOptionMissingError < StandardError
|
24
|
+
attr_reader :required_option
|
25
|
+
|
26
|
+
def initialize(required_option)
|
27
|
+
@required_option = required_option
|
28
|
+
super(message)
|
29
|
+
end
|
30
|
+
|
31
|
+
def message
|
32
|
+
"Option '#{required_option}' is missing."
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
23
36
|
class UnknownAction < StandardError
|
24
37
|
attr_reader :action
|
25
38
|
|
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.0
|
4
|
+
version: 2.1.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-
|
11
|
+
date: 2016-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -127,6 +127,7 @@ files:
|
|
127
127
|
- bin/setup
|
128
128
|
- json_api_responders.gemspec
|
129
129
|
- lib/json_api_responders.rb
|
130
|
+
- lib/json_api_responders/config.rb
|
130
131
|
- lib/json_api_responders/errors.rb
|
131
132
|
- lib/json_api_responders/responder.rb
|
132
133
|
- lib/json_api_responders/responder/actions.rb
|