media_types 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rubocop.yml +29 -0
- data/.travis.yml +20 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +42 -0
- data/README.md +150 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/media_types.rb +65 -0
- data/lib/media_types/base.rb +193 -0
- data/lib/media_types/base/collector.rb +44 -0
- data/lib/media_types/constructable_mime_type.rb +126 -0
- data/lib/media_types/minitest/assert_media_type_format.rb +20 -0
- data/lib/media_types/minitest/assert_media_types_registered.rb +143 -0
- data/lib/media_types/scheme.rb +238 -0
- data/lib/media_types/scheme/allow_nil.rb +18 -0
- data/lib/media_types/scheme/attribute.rb +34 -0
- data/lib/media_types/scheme/links.rb +32 -0
- data/lib/media_types/scheme/missing_validation.rb +24 -0
- data/lib/media_types/scheme/not_strict.rb +11 -0
- data/lib/media_types/version.rb +3 -0
- data/media_types.gemspec +32 -0
- metadata +151 -0
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'delegate'
|
4
|
+
|
5
|
+
module MediaTypes
|
6
|
+
class Scheme
|
7
|
+
class CaseEqualityWithNil < SimpleDelegator
|
8
|
+
def ===(other)
|
9
|
+
other.nil? || super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# noinspection RubyInstanceMethodNamingConvention
|
14
|
+
def AllowNil(klazz) # rubocop:disable Naming/MethodName
|
15
|
+
CaseEqualityWithNil.new(klazz)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MediaTypes
|
4
|
+
class Scheme
|
5
|
+
class Attribute
|
6
|
+
def initialize(type, allow_nil: false)
|
7
|
+
self.type = type
|
8
|
+
self.allow_nil = allow_nil
|
9
|
+
|
10
|
+
freeze
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate!(output, options, **_opts)
|
14
|
+
if output.nil?
|
15
|
+
return true if allow_nil
|
16
|
+
end
|
17
|
+
|
18
|
+
return true if type === output # rubocop:disable Style/CaseEquality
|
19
|
+
raise ValidationError,
|
20
|
+
format(
|
21
|
+
'Expected %<type>s, got %<actual>s at [%<backtrace>s]',
|
22
|
+
type: type,
|
23
|
+
actual: output.inspect,
|
24
|
+
backtrace: options.backtrace.join('->')
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
attr_accessor :allow_nil, :type
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MediaTypes
|
4
|
+
class Scheme
|
5
|
+
class Links
|
6
|
+
def initialize
|
7
|
+
self.links = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def link(key, allow_nil: false, &block)
|
11
|
+
scheme = Scheme.new
|
12
|
+
scheme.attribute :href, String, allow_nil: allow_nil
|
13
|
+
scheme.instance_exec(&block) if block_given?
|
14
|
+
|
15
|
+
links[key] = scheme
|
16
|
+
end
|
17
|
+
|
18
|
+
def validate!(output, options, **_opts)
|
19
|
+
links.all? do |key, value|
|
20
|
+
value.validate!(
|
21
|
+
output[key],
|
22
|
+
options.trace(key).exhaustive!
|
23
|
+
)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
attr_accessor :links
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MediaTypes
|
4
|
+
class Scheme
|
5
|
+
class MissingValidation
|
6
|
+
|
7
|
+
def validate!(_output, options, context:, **_opts)
|
8
|
+
# Check that no unknown keys are present
|
9
|
+
return true unless options.strict
|
10
|
+
raise_strict!(key: context.key, strict_keys: context.validations, backtrace: options.backtrace)
|
11
|
+
end
|
12
|
+
|
13
|
+
def raise_strict!(key:, backtrace:, strict_keys:)
|
14
|
+
raise StrictValidationError, format(
|
15
|
+
'Unknown key %<key>s in output at [%<backtrace>s]. Expected one of: %<strict_keys>s',
|
16
|
+
key: key.inspect,
|
17
|
+
backtrace: backtrace.join('->'),
|
18
|
+
strict_keys: strict_keys
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/media_types.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'media_types/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'media_types'
|
9
|
+
spec.version = MediaTypes::VERSION
|
10
|
+
spec.authors = ['Derk-Jan Karrenbeld']
|
11
|
+
spec.email = ['derk-jan+github@karrenbeld.info']
|
12
|
+
|
13
|
+
spec.summary = 'Library to create media type definitions, schemes and validations'
|
14
|
+
spec.description = 'Media Types as mime types are not easily supported by frameworks such as rails. '
|
15
|
+
spec.homepage = 'https://github.com/SleeplessBytes/media-types-ruby'
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
27
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
28
|
+
spec.add_development_dependency 'minitest-ci'
|
29
|
+
spec.add_development_dependency 'minitest-reporters'
|
30
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
31
|
+
spec.add_development_dependency 'simplecov'
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: media_types
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Derk-Jan Karrenbeld
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-09-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest-ci
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-reporters
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: 'Media Types as mime types are not easily supported by frameworks such
|
98
|
+
as rails. '
|
99
|
+
email:
|
100
|
+
- derk-jan+github@karrenbeld.info
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rubocop.yml"
|
107
|
+
- ".travis.yml"
|
108
|
+
- Gemfile
|
109
|
+
- Gemfile.lock
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- lib/media_types.rb
|
115
|
+
- lib/media_types/base.rb
|
116
|
+
- lib/media_types/base/collector.rb
|
117
|
+
- lib/media_types/constructable_mime_type.rb
|
118
|
+
- lib/media_types/minitest/assert_media_type_format.rb
|
119
|
+
- lib/media_types/minitest/assert_media_types_registered.rb
|
120
|
+
- lib/media_types/scheme.rb
|
121
|
+
- lib/media_types/scheme/allow_nil.rb
|
122
|
+
- lib/media_types/scheme/attribute.rb
|
123
|
+
- lib/media_types/scheme/links.rb
|
124
|
+
- lib/media_types/scheme/missing_validation.rb
|
125
|
+
- lib/media_types/scheme/not_strict.rb
|
126
|
+
- lib/media_types/version.rb
|
127
|
+
- media_types.gemspec
|
128
|
+
homepage: https://github.com/SleeplessBytes/media-types-ruby
|
129
|
+
licenses: []
|
130
|
+
metadata: {}
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 2.6.14.1
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: Library to create media type definitions, schemes and validations
|
151
|
+
test_files: []
|