media_types-validation 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53701447adf5bc4114c48808916b5d507dabe56c8524ca06fd73a5d23d5e3c1e
4
- data.tar.gz: 96ef62f1a31fb84e326351ab3dd3bc545ea78d4708654b76ff894639c9f506cb
3
+ metadata.gz: 7af4e0eddfb876d2fcf698fab8bd96deac9ac5c34a3b1c60567c21119597db51
4
+ data.tar.gz: 1c6d6482a8a4701ff65dc7deec8d5568bfcdf2ae5fa0d62d7b4fa53dcb5e35d5
5
5
  SHA512:
6
- metadata.gz: 3b0b96ac327396ec47f0a90d84673fd9ca6f9f7d5990282928e4db4c45d2473d68d765339d2b1ff65462f4b4226bef138bf69b6119b30fd34108cd9652c39f7f
7
- data.tar.gz: d771030d27b4de87739c27f5887a4c7c6e258bde5a72468b4f2fb9c0f2837ac58c3c8755e934c0c50e5cb3a528517b3e40002593614bb5ba546f442569671cde
6
+ metadata.gz: e7404580d33f3875931fc4199ed3127048132c03c473599a145b77439e43d4276bf44af5f34ec69b6c6764ca251570877f093fd2e00480ea7a3e5d2ff1be7f38
7
+ data.tar.gz: c1cb7e6869e220d9574801430578f63e624fe31f051175bb6cc8e7d4a05922388a1672e5be471191dbd1d89840d4b064cb37153ea58fafcd79f214b5114e447a
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0
4
+
5
+ - Add `instance_exec` execution for procs and configuration
6
+ - Remove controller argument for validation proc
7
+ - Add tests
8
+
9
+ ## 0.1.0
10
+
11
+ :baby: initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- media_types-validation (0.1.0)
4
+ media_types-validation (0.2.0)
5
5
  activesupport (>= 4.0.0)
6
6
  media_types (>= 0.6.0)
7
7
  oj (>= 3.5.0)
data/README.md CHANGED
@@ -24,7 +24,10 @@ Or install it yourself as:
24
24
 
25
25
  ## Usage
26
26
 
27
- I
27
+ If you add the `MediaTypes::Validation` controller concern, `validate_json_with_media_type` becomes available during
28
+ actions. This does _not_ validate only `JSON` output, but stringifies and then parses the body as `JSON`, so the
29
+ limitations of `JSON` apply. This step is necessary in order to make sure `rails` types and others are first correctly
30
+ casted (and formatted).
28
31
 
29
32
  ```ruby
30
33
  require 'media_types/validation'
@@ -51,19 +54,22 @@ class BookController < ApiController
51
54
  end
52
55
  ```
53
56
 
57
+ By default, this method only outputs to `stderr` when something is wrong; see configuration below if you want to assign
58
+ your own behaviour, such as adding a `Warn` header, or raising a server error.
59
+
54
60
  ### Configuration
55
61
 
56
62
  In an initializer you can set procs in order to change the default behaviour:
57
63
 
58
64
  ```ruby
59
- MediaTypes::Validation.configure do |this|
60
- this.json_invalid_media_proc = proc do |controller, media_type:, err:, body:|
61
- controller.response['Warn'] = '199 media type %s is invalid (%s)' % [media_type, err]
62
- warn controller.response['Warn'] + "\n" + body
65
+ MediaTypes::Validation.configure do
66
+ self.json_invalid_media_proc = proc do |media_type:, err:, body:|
67
+ response['Warn'] = '199 media type %s is invalid (%s)' % [media_type, err]
68
+ warn response['Warn'] + "\n" + body
63
69
  end
64
70
 
65
71
  # Or alternatively you can always raise
66
- this.raise_on_json_invalid_media = true
72
+ self.raise_on_json_invalid_media = true
67
73
  end
68
74
  ```
69
75
 
@@ -10,10 +10,10 @@ module MediaTypes
10
10
 
11
11
  extend ActiveSupport::Concern
12
12
 
13
- attr_accessor :json_invalid_media_proc, :raise_on_json_invalid_media
13
+ mattr_accessor :json_invalid_media_proc, :raise_on_json_invalid_media
14
14
 
15
- def self.configure
16
- yield self
15
+ def self.configure(&block)
16
+ instance_exec self, &block
17
17
  end
18
18
 
19
19
  def validate_json_with_media_type(body, media_type:)
@@ -42,7 +42,7 @@ module MediaTypes
42
42
  json_valid_media_or_throw?(body, media_type: media_type)
43
43
  rescue ::MediaTypes::Scheme::ValidationError => err
44
44
  if json_invalid_media_proc.respond_to?(:call)
45
- json_invalid_media_proc(self, media_type: media_type, err: err, body: body)
45
+ instance_exec(media_type: media_type, err: err, body: body, &json_invalid_media_proc)
46
46
  else
47
47
  message = format(
48
48
  '[media type validation] The data being sent as %<media_type>s is invalid:' + "\n" \
@@ -1,5 +1,5 @@
1
1
  module MediaTypes
2
2
  module Validation
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: media_types-validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derk-Jan Karrenbeld
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-14 00:00:00.000000000 Z
11
+ date: 2019-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -109,6 +109,7 @@ files:
109
109
  - ".idea/modules.xml"
110
110
  - ".idea/vcs.xml"
111
111
  - ".travis.yml"
112
+ - CHANGELOG.md
112
113
  - CODE_OF_CONDUCT.md
113
114
  - Gemfile
114
115
  - Gemfile.lock