media_types 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: media_types
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
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: 2018-09-26 00:00:00.000000000 Z
11
+ date: 2018-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: awesome_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -105,6 +119,7 @@ files:
105
119
  - ".gitignore"
106
120
  - ".rubocop.yml"
107
121
  - ".travis.yml"
122
+ - CHANGELOG.md
108
123
  - Gemfile
109
124
  - Gemfile.lock
110
125
  - README.md
@@ -112,17 +127,25 @@ files:
112
127
  - bin/console
113
128
  - bin/setup
114
129
  - 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
130
+ - lib/media_types/constructable.rb
131
+ - lib/media_types/defaults.rb
132
+ - lib/media_types/dsl.rb
133
+ - lib/media_types/hash.rb
118
134
  - lib/media_types/minitest/assert_media_type_format.rb
119
135
  - lib/media_types/minitest/assert_media_types_registered.rb
136
+ - lib/media_types/object.rb
137
+ - lib/media_types/registrar.rb
120
138
  - lib/media_types/scheme.rb
121
139
  - lib/media_types/scheme/allow_nil.rb
140
+ - lib/media_types/scheme/any_of.rb
122
141
  - lib/media_types/scheme/attribute.rb
142
+ - lib/media_types/scheme/enumeration_context.rb
143
+ - lib/media_types/scheme/enumeration_of_type.rb
123
144
  - lib/media_types/scheme/links.rb
124
145
  - lib/media_types/scheme/missing_validation.rb
125
146
  - lib/media_types/scheme/not_strict.rb
147
+ - lib/media_types/scheme/validation_options.rb
148
+ - lib/media_types/validations.rb
126
149
  - lib/media_types/version.rb
127
150
  - media_types.gemspec
128
151
  homepage: https://github.com/SleeplessBytes/media-types-ruby
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'delegate'
4
-
5
- module MediaTypes
6
- class Base
7
- class Collector < SimpleDelegator
8
-
9
- def index(*args, **options)
10
- view(MediaTypes::INDEX_VIEW, *args, **options)
11
- end
12
-
13
- def create(*args, **options, &block)
14
- view(MediaTypes::CREATE_VIEW, *args, **options, &block)
15
- end
16
-
17
- def collection(*args, **options)
18
- view(MediaTypes::COLLECTION_VIEW, *args, **options)
19
- end
20
-
21
- def view(view, *args, **options)
22
- register_type(*args, **options.merge(view: view))
23
- end
24
-
25
- def version(*args, **options, &block)
26
- register_version(*args, **options, &block)
27
- end
28
-
29
- private
30
-
31
- # This is similar to having a decorator / interface only exposing certain methods. In this private section
32
- # the +register_type+ and +register_version+ methods are made available
33
- #
34
-
35
- def register_type(*args, **options, &block)
36
- __getobj__.instance_exec { register_type(*args, options, &block) }
37
- end
38
-
39
- def register_version(*args, **options, &block)
40
- __getobj__.instance_exec { register_version(*args, options, &block) }
41
- end
42
- end
43
- end
44
- end
@@ -1,193 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'media_types/base/collector'
4
- require 'media_types/constructable_mime_type'
5
- require 'media_types/scheme'
6
-
7
- module MediaTypes
8
- class Base
9
-
10
- class << self
11
- ##
12
- # Registers all configured mime types
13
- #
14
- def register(**overrides)
15
- override_version = overrides.delete(:version)
16
- if override_version
17
- self.versions_ = { override_version => { scheme: (overrides.delete(:scheme) || current_scheme_) } }
18
- end
19
-
20
- Array(registers_).map do |registered|
21
- resolvable = registered.merge(Hash(overrides))
22
- mime_type = construct_mime_type(resolvable)
23
-
24
- resolve_versions(resolvable) do |version, symbol:|
25
- MediaTypes.register(
26
- mime_type: mime_type.version(version).to_s,
27
- symbol: symbol,
28
- synonyms: versioned_synonyms(version, resolvable)
29
- )
30
- symbol
31
- end
32
- end.flatten.compact
33
- end
34
-
35
- ##
36
- # Get the constructable mime type for this class
37
- #
38
- # @return [ConstructableMimeType]
39
- #
40
- def mime_type(type: type_, version: current_version_, suffix: suffix_, view: nil)
41
- ConstructableMimeType.new(self, format: base_format, version: version, suffix: suffix, type: type, view: view)
42
- end
43
-
44
- def current_version
45
- current_version_
46
- end
47
-
48
- def valid?(output, version: current_version, **opts)
49
- scheme = version_scheme(version: version)
50
- !scheme || scheme.valid?(output, backtrace: ['.'], **opts)
51
- end
52
-
53
- def validate!(output, version: current_version, **opts)
54
- scheme = version_scheme(version: version)
55
- scheme.validate(output, backtrace: ['.'], **opts)
56
- end
57
-
58
- protected
59
-
60
- attr_accessor :type_, :suffix_, :registers_, :versions_, :type_aliases_, :current_version_, :current_scheme_
61
-
62
- def base_format
63
- NotImplementedError.new('Implementors of MediaType::Base must override base_format')
64
- end
65
-
66
- ##
67
- # Configure the media type
68
- #
69
- # @param [String] media_type the +type+ part of the media type
70
- # @param [Symbol, String, NilClass] suffix the +suffix+ part of the media type
71
- # @param [Array<String>] aliases the aliases of +media_type+
72
- # @param [Number, String] version the latest version
73
- #
74
- def media_type(media_type, suffix: nil, aliases: [], current_version: nil, version: current_version, &block)
75
- self.type_ = media_type
76
- self.type_aliases_ = aliases
77
- self.suffix_ = suffix
78
-
79
- self.current_version_ = version
80
- self.current_scheme_ = current_scheme(&block)
81
- end
82
-
83
- def current_scheme(&block)
84
- scheme = Scheme.new
85
- scheme.instance_exec(&block) if block_given?
86
-
87
- self.current_scheme_ = scheme
88
- end
89
-
90
- ##
91
- # Start registering media types
92
- #
93
- # @param [Symbol] opts optional symbol
94
- # @option options [Symbol] symbol the symbol to register as if not given by +opts+
95
- # @option options [Number] version the version to register for defaults to +current_version_+
96
- # @option options [String, NilClass] view the view of the registered type
97
- # @option options [String] synonyms synonyms to resolve to the same type
98
- #
99
- # @yieldparam [Collector] the collector to collect views for the media type
100
- #
101
- def register_types(*opts, **options, &block)
102
- version = options.delete(:version)
103
- symbol_suffix = options.delete(:symbol_suffix)
104
-
105
- register_type(*opts, **options)
106
- register_version(version || current_version_, symbol_suffix: symbol_suffix, scheme: current_scheme_)
107
-
108
- return unless block_given?
109
- block_collector(&block)
110
- end
111
-
112
- def register_additional_versions(&block)
113
- block_collector(&block)
114
- end
115
-
116
- private
117
-
118
- def block_collector(&block)
119
- collector = Collector.new(self)
120
-
121
- case block.arity
122
- when 1, -1
123
- collector.instance_exec(collector, &block)
124
- else
125
- collector.instance_exec(&block)
126
- end
127
- end
128
-
129
- # @param [Symbol] opts optional symbol
130
- # @param [Symbol] symbol the symbol to register as if not given by +opts+
131
- # @param [String, NilClass] view the view of the registered type
132
- # @param [String] synonyms synonyms to resolve to the same type
133
- # rubocop:disable Metrics/ParameterLists
134
- def register_type(*opts, suffix: suffix_, view: nil, symbol: nil, synonyms: [], version: nil)
135
- symbol = opts&.first || symbol
136
- self.registers_ = Array(registers_).push(
137
- symbol: symbol,
138
- view: view,
139
- synonyms: synonyms,
140
- suffix: suffix,
141
- pinned_version: version
142
- )
143
- end
144
- # rubocop:enable Metrics/ParameterLists
145
-
146
- def construct_mime_type(type: type_, **resolvable)
147
- mime_type(type: type, **MediaTypes::Hash.new(resolvable).slice(:version, :view, :suffix))
148
- end
149
-
150
- def synonyms(resolvable)
151
- aliases = type_aliases_.map { |type_alias| construct_mime_type(type: type_alias, **resolvable) }
152
-
153
- Array(resolvable[:synonyms]).concat(aliases)
154
- end
155
-
156
- def register_version(version, symbol_suffix: :"_v#{version}", scheme: Scheme.new, &block)
157
- scheme.instance_exec(&block) if block_given?
158
-
159
- self.versions_ = Hash(versions_).merge(
160
- version => {
161
- symbol_suffix: symbol_suffix,
162
- scheme: scheme
163
- }
164
- )
165
- end
166
-
167
- def resolve_versions(**resolvable)
168
- pinned_version = resolvable[:pinned_version]
169
-
170
- versions.map do |version, opts|
171
- next if pinned_version && pinned_version != version
172
- yield version, symbol: :"#{resolvable.fetch(:symbol)}#{opts[:symbol_suffix]}"
173
- end
174
- end
175
-
176
- def versioned_synonyms(version, resolvable)
177
- synonyms(resolvable).map do |synonym|
178
- synonym.is_a?(String) ? synonym : synonym.version(version).to_s
179
- end.uniq
180
- end
181
-
182
- def versions
183
- { current_version => { scheme: current_scheme_ } }.merge(Hash(versions_))
184
- end
185
-
186
- def version_scheme(version:)
187
- version_data = versions[version]
188
- return nil unless version_data
189
- version_data[:scheme] || nil
190
- end
191
- end
192
- end
193
- end