json_api_responders 2.0.2 → 2.1.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: b60a50fdf2f25e998fede1a0427fb04bdb37c630
4
- data.tar.gz: 3611346f4409eedbc1c9c521a8ce9a495cfaafda
3
+ metadata.gz: 7d9223310e0bc824e94e72d5724cb531009adce1
4
+ data.tar.gz: 3fca5112b3b31228a5a9a59772a341c203206bc2
5
5
  SHA512:
6
- metadata.gz: d27abc9d632fbd7844dd555e7b50093793d12f595e3a660a33aa3101e381d9ab6fe6b8bc5805a29b6c96ad2fe5de4329a93edfe5f8a6137ed1b376f56a06afb1
7
- data.tar.gz: a3bf66eaf91447a813551b2c11579bd659d540e2e27e06106b0c8f8e6dacc45edab48a47b16001088574f44badd9b402b6a4cf8cb12628f3d5c63057d650ab5c
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
- respond_with user, serializer: UserSerializer
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
 
@@ -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
 
@@ -1,6 +1,6 @@
1
1
  module JsonApiResponders
2
2
  MAJOR = 2
3
- MINOR = 0
4
- PATCH = 2
3
+ MINOR = 1
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.0.2
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-07 00:00:00.000000000 Z
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