media_types 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +44 -44
- data/README.md +4 -4
- data/lib/media_types/scheme/allow_nil.rb +13 -1
- data/lib/media_types/scheme/any_of.rb +13 -1
- data/lib/media_types/scheme/attribute.rb +2 -7
- data/lib/media_types/scheme/missing_validation.rb +9 -2
- data/lib/media_types/scheme/not_strict.rb +1 -1
- data/lib/media_types/scheme/rules.rb +37 -4
- data/lib/media_types/scheme.rb +7 -2
- data/lib/media_types/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60e6f4948af77b72e970849c287e89e9f1666470
|
4
|
+
data.tar.gz: 5f2e3f281075c813da57bfe85e391559b17fc02c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22592d00055a373b235c543968cf88631becab9b7d97ee60c371661af5eca417c7a5e120f8f7fc2a7a0bea5fb21535b0fc94d89691a55f81dc229e5fa548b54e
|
7
|
+
data.tar.gz: cd4799ba03f74d0d10aa8c6de20dafb1cc836c85ca609ee273d34fdbea0266f174adbe29a6570099659d52ff551d8cd0b6bcf132fbc6f75a0e08984d626f94d9
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,44 +1,44 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
media_types (0.5.
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
ansi (1.5.0)
|
10
|
-
awesome_print (1.8.0)
|
11
|
-
builder (3.2.3)
|
12
|
-
docile (1.3.1)
|
13
|
-
json (2.1.0)
|
14
|
-
minitest (5.11.3)
|
15
|
-
minitest-ci (3.4.0)
|
16
|
-
minitest (>= 5.0.6)
|
17
|
-
minitest-reporters (1.3.4)
|
18
|
-
ansi
|
19
|
-
builder
|
20
|
-
minitest (>= 5.0)
|
21
|
-
ruby-progressbar
|
22
|
-
rake (10.5.0)
|
23
|
-
ruby-progressbar (1.10.0)
|
24
|
-
simplecov (0.16.1)
|
25
|
-
docile (~> 1.1)
|
26
|
-
json (>= 1.8, < 3)
|
27
|
-
simplecov-html (~> 0.10.0)
|
28
|
-
simplecov-html (0.10.2)
|
29
|
-
|
30
|
-
PLATFORMS
|
31
|
-
x64-mingw32
|
32
|
-
|
33
|
-
DEPENDENCIES
|
34
|
-
awesome_print
|
35
|
-
bundler (~> 1.16)
|
36
|
-
media_types!
|
37
|
-
minitest (~> 5.0)
|
38
|
-
minitest-ci
|
39
|
-
minitest-reporters
|
40
|
-
rake (~> 10.0)
|
41
|
-
simplecov
|
42
|
-
|
43
|
-
BUNDLED WITH
|
44
|
-
1.16.4
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
media_types (0.5.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ansi (1.5.0)
|
10
|
+
awesome_print (1.8.0)
|
11
|
+
builder (3.2.3)
|
12
|
+
docile (1.3.1)
|
13
|
+
json (2.1.0)
|
14
|
+
minitest (5.11.3)
|
15
|
+
minitest-ci (3.4.0)
|
16
|
+
minitest (>= 5.0.6)
|
17
|
+
minitest-reporters (1.3.4)
|
18
|
+
ansi
|
19
|
+
builder
|
20
|
+
minitest (>= 5.0)
|
21
|
+
ruby-progressbar
|
22
|
+
rake (10.5.0)
|
23
|
+
ruby-progressbar (1.10.0)
|
24
|
+
simplecov (0.16.1)
|
25
|
+
docile (~> 1.1)
|
26
|
+
json (>= 1.8, < 3)
|
27
|
+
simplecov-html (~> 0.10.0)
|
28
|
+
simplecov-html (0.10.2)
|
29
|
+
|
30
|
+
PLATFORMS
|
31
|
+
x64-mingw32
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
awesome_print
|
35
|
+
bundler (~> 1.16)
|
36
|
+
media_types!
|
37
|
+
minitest (~> 5.0)
|
38
|
+
minitest-ci
|
39
|
+
minitest-reporters
|
40
|
+
rake (~> 10.0)
|
41
|
+
simplecov
|
42
|
+
|
43
|
+
BUNDLED WITH
|
44
|
+
1.16.4
|
data/README.md
CHANGED
@@ -37,6 +37,10 @@ You can define media types by inheriting from this base type, or create your own
|
|
37
37
|
require 'media_types'
|
38
38
|
|
39
39
|
class Venue < MediaTypes::Base
|
40
|
+
def self.base_format
|
41
|
+
'application/vnd.mydomain.%<type>s.v%<version>.s%<view>s+%<suffix>s'
|
42
|
+
end
|
43
|
+
|
40
44
|
media_type 'venue', defaults: { suffix: :json, version: 2 }
|
41
45
|
|
42
46
|
validations do
|
@@ -85,10 +89,6 @@ class Venue < MediaTypes::Base
|
|
85
89
|
suffix :json
|
86
90
|
suffix :xml
|
87
91
|
end
|
88
|
-
|
89
|
-
def self.base_format
|
90
|
-
'application/vnd.mydomain.%<type>s.v%<version>.s%<view>s+%<suffix>s'
|
91
|
-
end
|
92
92
|
end
|
93
93
|
```
|
94
94
|
|
@@ -4,6 +4,18 @@ require 'delegate'
|
|
4
4
|
|
5
5
|
module MediaTypes
|
6
6
|
class Scheme
|
7
|
+
class << self
|
8
|
+
# noinspection RubyClassMethodNamingConvention
|
9
|
+
##
|
10
|
+
# Allows the wrapped +klazz+ to be nil
|
11
|
+
#
|
12
|
+
# @param [Class] klazz the class that +it+ must be the if +it+ is not NilClass
|
13
|
+
# @return [CaseEqualityWithList]
|
14
|
+
def AllowNil(klazz) # rubocop:disable Naming/MethodName
|
15
|
+
AnyOf(NilClass, klazz)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
7
19
|
# noinspection RubyInstanceMethodNamingConvention
|
8
20
|
##
|
9
21
|
# Allows the wrapped +klazz+ to be nil
|
@@ -11,7 +23,7 @@ module MediaTypes
|
|
11
23
|
# @param [Class] klazz the class that +it+ must be the if +it+ is not NilClass
|
12
24
|
# @return [CaseEqualityWithList]
|
13
25
|
def AllowNil(klazz) # rubocop:disable Naming/MethodName
|
14
|
-
|
26
|
+
self.class.AllowNil(klazz)
|
15
27
|
end
|
16
28
|
end
|
17
29
|
end
|
@@ -16,6 +16,18 @@ module MediaTypes
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
+
class << self
|
20
|
+
# noinspection RubyClassMethodNamingConvention
|
21
|
+
##
|
22
|
+
# Allows +it+ to be any of the wrapped +klazzes+
|
23
|
+
#
|
24
|
+
# @param [Array<Class>] klazzes the classes that are valid for +it+
|
25
|
+
# @return [CaseEqualityWithList]
|
26
|
+
def AnyOf(*klazzes) # rubocop:disable Naming/MethodName
|
27
|
+
CaseEqualityWithList.new(klazzes)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
19
31
|
# noinspection RubyInstanceMethodNamingConvention
|
20
32
|
##
|
21
33
|
# Allows +it+ to be any of the wrapped +klazzes+
|
@@ -23,7 +35,7 @@ module MediaTypes
|
|
23
35
|
# @param [Array<Class>] klazzes the classes that are valid for +it+
|
24
36
|
# @return [CaseEqualityWithList]
|
25
37
|
def AnyOf(*klazzes) # rubocop:disable Naming/MethodName
|
26
|
-
|
38
|
+
self.class.AnyOf(*klazzes)
|
27
39
|
end
|
28
40
|
end
|
29
41
|
end
|
@@ -14,17 +14,12 @@ module MediaTypes
|
|
14
14
|
# @param [TrueClass, FalseClass] allow_nil if true, nil? is allowed
|
15
15
|
#
|
16
16
|
def initialize(type, allow_nil: false)
|
17
|
-
self.type = type
|
18
|
-
self.allow_nil = allow_nil
|
17
|
+
self.type = allow_nil ? AllowNil(type) : type
|
19
18
|
|
20
19
|
freeze
|
21
20
|
end
|
22
21
|
|
23
22
|
def validate!(output, options, **_opts)
|
24
|
-
if output.nil?
|
25
|
-
return true if allow_nil
|
26
|
-
end
|
27
|
-
|
28
23
|
return true if type === output # rubocop:disable Style/CaseEquality
|
29
24
|
raise ValidationError,
|
30
25
|
format(
|
@@ -36,7 +31,7 @@ module MediaTypes
|
|
36
31
|
end
|
37
32
|
|
38
33
|
def inspect
|
39
|
-
"[Scheme::Attribute
|
34
|
+
"[Scheme::Attribute of #{type.inspect}]"
|
40
35
|
end
|
41
36
|
|
42
37
|
private
|
@@ -12,13 +12,20 @@ module MediaTypes
|
|
12
12
|
|
13
13
|
def raise_strict!(key:, backtrace:, strict_keys:)
|
14
14
|
raise StrictValidationError, format(
|
15
|
-
|
15
|
+
"Unknown key %<key>s in data.\n" \
|
16
|
+
"\tFound at: %<backtrace>s\n" \
|
17
|
+
"\tExpected:\n\n" \
|
18
|
+
'%<strict_keys>s',
|
16
19
|
key: key.inspect,
|
17
20
|
backtrace: backtrace.join('->'),
|
18
|
-
strict_keys: strict_keys
|
21
|
+
strict_keys: strict_keys.inspect(1)
|
19
22
|
)
|
20
23
|
end
|
21
24
|
|
25
|
+
def inspect
|
26
|
+
'((raise when strict))'
|
27
|
+
end
|
28
|
+
|
22
29
|
end
|
23
30
|
end
|
24
31
|
end
|
@@ -45,6 +45,14 @@ module MediaTypes
|
|
45
45
|
self
|
46
46
|
end
|
47
47
|
|
48
|
+
##
|
49
|
+
# Returns the keys that are not options
|
50
|
+
#
|
51
|
+
# @see #add
|
52
|
+
# #see #merge
|
53
|
+
#
|
54
|
+
# @return [Array<Symbol>] required keys
|
55
|
+
#
|
48
56
|
def required
|
49
57
|
clone.tap do |cloned|
|
50
58
|
optional_keys.each do |key|
|
@@ -59,13 +67,38 @@ module MediaTypes
|
|
59
67
|
end
|
60
68
|
end
|
61
69
|
|
70
|
+
##
|
71
|
+
# Merges another set of rules into a clone of this one
|
72
|
+
#
|
73
|
+
# @param [Rules, ::Hash] the other rules
|
74
|
+
# @return [Rules] a clone
|
75
|
+
#
|
62
76
|
def merge(rules)
|
63
|
-
|
64
|
-
|
77
|
+
clone.instance_exec do
|
78
|
+
__setobj__(__getobj__.merge(rules))
|
79
|
+
if rules.respond_to?(:optional_keys, true)
|
80
|
+
optional_keys.push(*rules.send(:optional_keys))
|
81
|
+
end
|
82
|
+
|
83
|
+
self
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def inspect(indent = 0)
|
88
|
+
prefix = ' ' * indent
|
89
|
+
[
|
90
|
+
"#{prefix}[Rules n=#{keys.length} optional=#{optional_keys.length}]",
|
91
|
+
"#{prefix} #{inspect_format_attribute(indent, '*', default)}",
|
92
|
+
*keys.map { |key| "#{prefix} #{inspect_format_attribute(indent, key)}" },
|
93
|
+
"#{prefix}[/Rules]"
|
94
|
+
].join("\n")
|
65
95
|
end
|
66
96
|
|
67
|
-
def
|
68
|
-
|
97
|
+
def inspect_format_attribute(indent, key, value = self[key])
|
98
|
+
[
|
99
|
+
[key.to_s, optional_keys.include?(key) && '(optional)' || nil].compact.join(' '),
|
100
|
+
value.is_a?(Scheme) || value.is_a?(Rules) ? "\n#{value.inspect(indent + 2)}" : value.inspect
|
101
|
+
].join(': ')
|
69
102
|
end
|
70
103
|
|
71
104
|
alias get []
|
data/lib/media_types/scheme.rb
CHANGED
@@ -351,8 +351,13 @@ module MediaTypes
|
|
351
351
|
end.link(*args, **opts, &block)
|
352
352
|
end
|
353
353
|
|
354
|
-
def inspect
|
355
|
-
|
354
|
+
def inspect(indentation = 0)
|
355
|
+
tabs = ' ' * indentation
|
356
|
+
[
|
357
|
+
"#{tabs}[Scheme]",
|
358
|
+
rules.inspect(indentation + 1),
|
359
|
+
"#{tabs}[/Scheme]"
|
360
|
+
].join("\n")
|
356
361
|
end
|
357
362
|
|
358
363
|
private
|
data/lib/media_types/version.rb
CHANGED