media_types 2.2.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/debian.yml +43 -43
- data/.github/workflows/publish-bookworm.yml +33 -0
- data/.github/workflows/publish-sid.yml +33 -0
- data/.github/workflows/ruby.yml +22 -22
- data/.gitignore +20 -10
- data/.rubocop.yml +29 -29
- data/CHANGELOG.md +175 -164
- data/Gemfile +6 -6
- data/LICENSE +21 -21
- data/README.md +666 -664
- data/Rakefile +12 -12
- data/bin/console +15 -15
- data/bin/setup +8 -8
- data/lib/media_types/constructable.rb +161 -160
- data/lib/media_types/dsl/errors.rb +18 -18
- data/lib/media_types/dsl.rb +172 -172
- data/lib/media_types/errors.rb +25 -19
- data/lib/media_types/formatter.rb +56 -56
- data/lib/media_types/hash.rb +21 -21
- data/lib/media_types/object.rb +35 -35
- data/lib/media_types/scheme/allow_nil.rb +30 -30
- data/lib/media_types/scheme/any_of.rb +41 -41
- data/lib/media_types/scheme/attribute.rb +46 -46
- data/lib/media_types/scheme/enumeration_context.rb +18 -18
- data/lib/media_types/scheme/enumeration_of_type.rb +80 -80
- data/lib/media_types/scheme/errors.rb +87 -87
- data/lib/media_types/scheme/links.rb +54 -54
- data/lib/media_types/scheme/missing_validation.rb +41 -41
- data/lib/media_types/scheme/not_strict.rb +15 -15
- data/lib/media_types/scheme/output_empty_guard.rb +45 -45
- data/lib/media_types/scheme/output_iterator_with_predicate.rb +66 -66
- data/lib/media_types/scheme/output_type_guard.rb +39 -39
- data/lib/media_types/scheme/rules.rb +186 -173
- data/lib/media_types/scheme/rules_exhausted_guard.rb +73 -73
- data/lib/media_types/scheme/validation_options.rb +44 -43
- data/lib/media_types/scheme.rb +535 -513
- data/lib/media_types/testing/assertions.rb +20 -20
- data/lib/media_types/validations.rb +118 -105
- data/lib/media_types/version.rb +5 -5
- data/lib/media_types/views.rb +12 -12
- data/lib/media_types.rb +73 -73
- data/media_types.gemspec +33 -33
- metadata +8 -6
data/Rakefile
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'bundler/gem_tasks'
|
4
|
-
require 'rake/testtask'
|
5
|
-
|
6
|
-
Rake::TestTask.new(:test) do |t|
|
7
|
-
t.libs << 'test'
|
8
|
-
t.libs << 'lib'
|
9
|
-
t.test_files = FileList['test/**/*_test.rb']
|
10
|
-
end
|
11
|
-
|
12
|
-
task default: :test
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
task default: :test
|
data/bin/console
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require 'bundler/setup'
|
5
|
-
require 'media_types'
|
6
|
-
|
7
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
-
# with your gem easier. You can also use a different console, if you like.
|
9
|
-
|
10
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
-
# require "pry"
|
12
|
-
# Pry.start
|
13
|
-
|
14
|
-
require 'irb'
|
15
|
-
IRB.start(__FILE__)
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'media_types'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
#!/usr/bin/env bash
|
2
|
-
set -euo pipefail
|
3
|
-
IFS=$'\n\t'
|
4
|
-
set -vx
|
5
|
-
|
6
|
-
bundle install
|
7
|
-
|
8
|
-
# Do any other automated setup that you need to do here
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
set -euo pipefail
|
3
|
+
IFS=$'\n\t'
|
4
|
+
set -vx
|
5
|
+
|
6
|
+
bundle install
|
7
|
+
|
8
|
+
# Do any other automated setup that you need to do here
|
@@ -1,160 +1,161 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'delegate'
|
4
|
-
require 'singleton'
|
5
|
-
|
6
|
-
require 'media_types/formatter'
|
7
|
-
|
8
|
-
module MediaTypes
|
9
|
-
class Constructable < SimpleDelegator
|
10
|
-
|
11
|
-
def initialize(klazz, **opts)
|
12
|
-
super klazz
|
13
|
-
self.opts = opts
|
14
|
-
end
|
15
|
-
|
16
|
-
def type(name = NO_ARG)
|
17
|
-
return opts[:type] if name == NO_ARG
|
18
|
-
with(type: name)
|
19
|
-
end
|
20
|
-
|
21
|
-
def version(version = NO_ARG)
|
22
|
-
return opts[:version] if version == NO_ARG
|
23
|
-
with(version: version)
|
24
|
-
end
|
25
|
-
|
26
|
-
def view(view = NO_ARG)
|
27
|
-
return opts[:view] if view == NO_ARG
|
28
|
-
with(view: view)
|
29
|
-
end
|
30
|
-
|
31
|
-
def collection
|
32
|
-
view(COLLECTION_VIEW)
|
33
|
-
end
|
34
|
-
|
35
|
-
def collection?
|
36
|
-
opts[:view] == COLLECTION_VIEW
|
37
|
-
end
|
38
|
-
|
39
|
-
def create
|
40
|
-
view(CREATE_VIEW)
|
41
|
-
end
|
42
|
-
|
43
|
-
def create?
|
44
|
-
opts[:view] == CREATE_VIEW
|
45
|
-
end
|
46
|
-
|
47
|
-
def index
|
48
|
-
view(INDEX_VIEW)
|
49
|
-
end
|
50
|
-
|
51
|
-
def index?
|
52
|
-
opts[:view] == INDEX_VIEW
|
53
|
-
end
|
54
|
-
|
55
|
-
def ===(other)
|
56
|
-
to_str.send(:===, other)
|
57
|
-
end
|
58
|
-
|
59
|
-
def ==(other)
|
60
|
-
to_str.send(:==, other)
|
61
|
-
end
|
62
|
-
|
63
|
-
def +(other)
|
64
|
-
to_str + other
|
65
|
-
end
|
66
|
-
|
67
|
-
def split(pattern = nil, *limit)
|
68
|
-
to_str.split(pattern, *limit)
|
69
|
-
end
|
70
|
-
|
71
|
-
def as_key
|
72
|
-
[type, view&.to_s, version]
|
73
|
-
end
|
74
|
-
|
75
|
-
def hash
|
76
|
-
as_key.hash
|
77
|
-
end
|
78
|
-
|
79
|
-
def override_suffix(suffix)
|
80
|
-
with(suffix: suffix)
|
81
|
-
end
|
82
|
-
|
83
|
-
def suffix
|
84
|
-
return opts[:suffix] if opts.key?(:suffix)
|
85
|
-
|
86
|
-
schema = schema_for(self)
|
87
|
-
schema.type_attributes.fetch(:suffix, 'json')
|
88
|
-
end
|
89
|
-
|
90
|
-
def to_str(qualifier = nil)
|
91
|
-
qualified(
|
92
|
-
qualifier,
|
93
|
-
__getobj__.media_type_name_for.call(
|
94
|
-
type: opts[:type],
|
95
|
-
view: opts[:view],
|
96
|
-
version: opts[:version],
|
97
|
-
suffix: suffix
|
98
|
-
)
|
99
|
-
)
|
100
|
-
end
|
101
|
-
|
102
|
-
def available_validations
|
103
|
-
return [] if !validatable?
|
104
|
-
[self]
|
105
|
-
end
|
106
|
-
|
107
|
-
def valid?(output, **validation_opts)
|
108
|
-
raise ArgumentError, "Unable to validate #{to_s} type without a corresponding validation. Please mark objects that should be empty with 'empty'." unless validatable?
|
109
|
-
|
110
|
-
__getobj__.valid_unsafe?(
|
111
|
-
output,
|
112
|
-
self,
|
113
|
-
**validation_opts
|
114
|
-
)
|
115
|
-
end
|
116
|
-
|
117
|
-
def validate!(output, **validation_opts)
|
118
|
-
raise ArgumentError, "Unable to validate #{to_s} type without a corresponding validation. Please mark objects that should be empty with 'empty'." unless validatable?
|
119
|
-
|
120
|
-
__getobj__.validate_unsafe!(
|
121
|
-
output,
|
122
|
-
self,
|
123
|
-
**validation_opts
|
124
|
-
)
|
125
|
-
end
|
126
|
-
|
127
|
-
def validatable?
|
128
|
-
return false
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
alias
|
135
|
-
alias
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'delegate'
|
4
|
+
require 'singleton'
|
5
|
+
|
6
|
+
require 'media_types/formatter'
|
7
|
+
|
8
|
+
module MediaTypes
|
9
|
+
class Constructable < SimpleDelegator
|
10
|
+
|
11
|
+
def initialize(klazz, **opts)
|
12
|
+
super klazz
|
13
|
+
self.opts = opts
|
14
|
+
end
|
15
|
+
|
16
|
+
def type(name = NO_ARG)
|
17
|
+
return opts[:type] if name == NO_ARG
|
18
|
+
with(type: name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def version(version = NO_ARG)
|
22
|
+
return opts[:version] if version == NO_ARG
|
23
|
+
with(version: version)
|
24
|
+
end
|
25
|
+
|
26
|
+
def view(view = NO_ARG)
|
27
|
+
return opts[:view] if view == NO_ARG
|
28
|
+
with(view: view)
|
29
|
+
end
|
30
|
+
|
31
|
+
def collection
|
32
|
+
view(COLLECTION_VIEW)
|
33
|
+
end
|
34
|
+
|
35
|
+
def collection?
|
36
|
+
opts[:view] == COLLECTION_VIEW
|
37
|
+
end
|
38
|
+
|
39
|
+
def create
|
40
|
+
view(CREATE_VIEW)
|
41
|
+
end
|
42
|
+
|
43
|
+
def create?
|
44
|
+
opts[:view] == CREATE_VIEW
|
45
|
+
end
|
46
|
+
|
47
|
+
def index
|
48
|
+
view(INDEX_VIEW)
|
49
|
+
end
|
50
|
+
|
51
|
+
def index?
|
52
|
+
opts[:view] == INDEX_VIEW
|
53
|
+
end
|
54
|
+
|
55
|
+
def ===(other)
|
56
|
+
to_str.send(:===, other)
|
57
|
+
end
|
58
|
+
|
59
|
+
def ==(other)
|
60
|
+
to_str.send(:==, other)
|
61
|
+
end
|
62
|
+
|
63
|
+
def +(other)
|
64
|
+
to_str + other
|
65
|
+
end
|
66
|
+
|
67
|
+
def split(pattern = nil, *limit)
|
68
|
+
to_str.split(pattern, *limit)
|
69
|
+
end
|
70
|
+
|
71
|
+
def as_key
|
72
|
+
[type, view&.to_s, version]
|
73
|
+
end
|
74
|
+
|
75
|
+
def hash
|
76
|
+
as_key.hash
|
77
|
+
end
|
78
|
+
|
79
|
+
def override_suffix(suffix)
|
80
|
+
with(suffix: suffix)
|
81
|
+
end
|
82
|
+
|
83
|
+
def suffix
|
84
|
+
return opts[:suffix] if opts.key?(:suffix)
|
85
|
+
|
86
|
+
schema = schema_for(self)
|
87
|
+
schema.type_attributes.fetch(:suffix, 'json')
|
88
|
+
end
|
89
|
+
|
90
|
+
def to_str(qualifier = nil)
|
91
|
+
qualified(
|
92
|
+
qualifier,
|
93
|
+
__getobj__.media_type_name_for.call(
|
94
|
+
type: opts[:type],
|
95
|
+
view: opts[:view],
|
96
|
+
version: opts[:version],
|
97
|
+
suffix: suffix
|
98
|
+
)
|
99
|
+
)
|
100
|
+
end
|
101
|
+
|
102
|
+
def available_validations
|
103
|
+
return [] if !validatable?
|
104
|
+
[self]
|
105
|
+
end
|
106
|
+
|
107
|
+
def valid?(output, **validation_opts)
|
108
|
+
raise ArgumentError, "Unable to validate #{to_s} type without a corresponding validation. Please mark objects that should be empty with 'empty'." unless validatable?
|
109
|
+
|
110
|
+
__getobj__.valid_unsafe?(
|
111
|
+
output,
|
112
|
+
self,
|
113
|
+
**validation_opts
|
114
|
+
)
|
115
|
+
end
|
116
|
+
|
117
|
+
def validate!(output, **validation_opts)
|
118
|
+
raise ArgumentError, "Unable to validate #{to_s} type without a corresponding validation. Please mark objects that should be empty with 'empty'." unless validatable?
|
119
|
+
|
120
|
+
__getobj__.validate_unsafe!(
|
121
|
+
output,
|
122
|
+
self,
|
123
|
+
**validation_opts
|
124
|
+
)
|
125
|
+
end
|
126
|
+
|
127
|
+
def validatable?
|
128
|
+
return false if media_type_combinations.nil?
|
129
|
+
return false unless media_type_combinations.include? as_key
|
130
|
+
|
131
|
+
__getobj__.validatable?(self)
|
132
|
+
end
|
133
|
+
|
134
|
+
alias inspect to_str
|
135
|
+
alias to_s to_str
|
136
|
+
alias identifier to_str
|
137
|
+
|
138
|
+
private
|
139
|
+
|
140
|
+
class NoArgumentGiven
|
141
|
+
include Singleton
|
142
|
+
end
|
143
|
+
|
144
|
+
NO_ARG = NoArgumentGiven.instance
|
145
|
+
|
146
|
+
attr_accessor :opts
|
147
|
+
|
148
|
+
def with(more_opts)
|
149
|
+
merged_options = Kernel::Hash(opts).clone.tap do |cloned|
|
150
|
+
cloned.merge!(more_opts)
|
151
|
+
end
|
152
|
+
|
153
|
+
Constructable.new(__getobj__, **merged_options)
|
154
|
+
end
|
155
|
+
|
156
|
+
def qualified(qualifier, media_type)
|
157
|
+
return media_type unless qualifier
|
158
|
+
format('%<media_type>s; q=%<q>s', media_type: media_type, q: qualifier)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
@@ -1,18 +1,18 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module MediaTypes
|
4
|
-
module Dsl
|
5
|
-
class UninitializedConstructable < RuntimeError
|
6
|
-
def message
|
7
|
-
'Unable to generate constructable without a name, make sure to have called `use_name(name)` before.'
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
# Raised when an error occurs during setting expected key type
|
12
|
-
class KeyTypeExpectationError < StandardError; end
|
13
|
-
|
14
|
-
class MissingValidationError < StandardError; end
|
15
|
-
|
16
|
-
class OrganisationNotSetError < StandardError; end
|
17
|
-
end
|
18
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module MediaTypes
|
4
|
+
module Dsl
|
5
|
+
class UninitializedConstructable < RuntimeError
|
6
|
+
def message
|
7
|
+
'Unable to generate constructable without a name, make sure to have called `use_name(name)` before.'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# Raised when an error occurs during setting expected key type
|
12
|
+
class KeyTypeExpectationError < StandardError; end
|
13
|
+
|
14
|
+
class MissingValidationError < StandardError; end
|
15
|
+
|
16
|
+
class OrganisationNotSetError < StandardError; end
|
17
|
+
end
|
18
|
+
end
|