media_types-serialization 1.0.3 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,77 @@
1
+ =begin
2
+ The MIT License (MIT)
3
+
4
+ Copyright (c) 2019 Derk-Jan Karrenbeld
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ =end
24
+
25
+ require 'media_types/serialization/utils/header_list'
26
+
27
+ module MediaTypes
28
+ module Serialization
29
+ module Utils
30
+ class AcceptHeader < DelegateClass(Array)
31
+ def initialize(value)
32
+ __setobj__ HeaderList.new(value, entry_klazz: AcceptHeader::Entry)
33
+ end
34
+
35
+ class Entry
36
+ def initialize(media_type, index:, parameters:)
37
+ self.media_type = media_type
38
+ self.parameters = parameters
39
+ self.index = index
40
+
41
+ freeze
42
+ end
43
+
44
+ attr_reader :media_type
45
+
46
+ # noinspection RubyInstanceMethodNamingConvention
47
+ def q
48
+ parameters.fetch(:q) { 1.0 }.to_f
49
+ end
50
+
51
+ def <=>(other)
52
+ quality = other.q <=> q
53
+ return quality unless quality.zero?
54
+ index <=> other.send(:index)
55
+ end
56
+
57
+ def [](parameter)
58
+ parameters.fetch(String(parameter).to_sym)
59
+ end
60
+
61
+ def to_header
62
+ to_s
63
+ end
64
+
65
+ def to_s
66
+ [media_type].concat(parameters.map { |k, v| "#{k}=#{v}" }).compact.reject(&:empty?).join('; ')
67
+ end
68
+
69
+ private
70
+
71
+ attr_writer :media_type
72
+ attr_accessor :parameters, :index
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,82 @@
1
+ =begin
2
+ The MIT License (MIT)
3
+
4
+ Copyright (c) 2019 Derk-Jan Karrenbeld
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ =end
24
+
25
+ require 'media_types/serialization/utils/header_list'
26
+
27
+ module MediaTypes
28
+ module Serialization
29
+ module Utils
30
+ class AcceptLanguageHeader < DelegateClass(Array)
31
+ def initialize(value)
32
+ __setobj__ HeaderList.new(value, entry_klazz: Entry)
33
+ end
34
+
35
+ class Entry
36
+
37
+ DELIMITER = '-'
38
+
39
+ attr_reader :locale, :region, :language
40
+
41
+ def initialize(locale, index:, parameters:)
42
+ self.locale = locale
43
+ # TODO: support extlang correctly, maybe we don't even need this
44
+ self.language, self.region = locale.split(DELIMITER)
45
+ self.parameters = parameters
46
+ self.index = index
47
+
48
+ freeze
49
+ end
50
+
51
+ # noinspection RubyInstanceMethodNamingConvention
52
+ def q
53
+ parameters.fetch(:q) { 1.0 }.to_f
54
+ end
55
+
56
+ def <=>(other)
57
+ quality = other.q <=> q
58
+ return quality unless quality.zero?
59
+ index <=> other.send(:index)
60
+ end
61
+
62
+ def [](parameter)
63
+ parameters.fetch(String(parameter).to_sym)
64
+ end
65
+
66
+ def to_header
67
+ to_s
68
+ end
69
+
70
+ def to_s
71
+ [locale].concat(parameters.map { |k, v| "#{k}=#{v}" }).compact.reject(&:empty?).join('; ')
72
+ end
73
+
74
+ private
75
+
76
+ attr_writer :locale, :region, :language
77
+ attr_accessor :parameters, :index
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,89 @@
1
+ =begin
2
+ The MIT License (MIT)
3
+
4
+ Copyright (c) 2019 Derk-Jan Karrenbeld
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ =end
24
+
25
+ module MediaTypes
26
+ module Serialization
27
+ module Utils
28
+ ##
29
+ # @example Accept values
30
+ #
31
+ # class AcceptHeader < DelegateClass(Array)
32
+ # def initialize(value)
33
+ # super MediaTypes::Serialization::Utils::HeaderList.new(value, entry_klazz: AcceptHeader::Entry)
34
+ # end
35
+ #
36
+ # class Entry
37
+ # def initialize(media_type, index: parameters:)
38
+ # ...
39
+ # end
40
+ #
41
+ # def q
42
+ # parameters.fetch(:q) { 1.0 }.to_f
43
+ # end
44
+ #
45
+ # def <=>(other)
46
+ # quality = other.q <=> q
47
+ # return quality unless quality.zero?
48
+ # index <=> other.index
49
+ # end
50
+ # end
51
+ # end
52
+ #
53
+ # Accept.new(['*/*; q=0.1', 'application/json, text/html; q=0.8'])
54
+ # # => List['application/json', 'text/html', '*/*']
55
+ #
56
+ module HeaderList
57
+ HEADER_DELIMITER = ','
58
+ PARAMETER_DELIMITER = ';'
59
+
60
+ module_function
61
+
62
+ def parse(combined, entry_klazz:)
63
+ Array(combined).map { |line| line.split(HEADER_DELIMITER) }.flatten.each_with_index.map do |entry, index|
64
+ value, *parameters = entry.strip.split(PARAMETER_DELIMITER)
65
+ indexed_parameters = ::Hash[Array(parameters).map { |p| p.strip.split('=') }].transform_keys!(&:to_sym)
66
+ entry_klazz.new(value, index: index, parameters: indexed_parameters)
67
+ end
68
+ end
69
+
70
+ def new(combined, entry_klazz:)
71
+ result = parse(combined, entry_klazz: entry_klazz)
72
+ entry_klazz.instance_methods(false).include?(:<=>) ? result.sort! : result
73
+ end
74
+
75
+ def to_header(list)
76
+ # noinspection RubyBlockToMethodReference
77
+ list.map { |entry| stringify_entry(entry) }
78
+ .join("#{HEADER_DELIMITER} ")
79
+ end
80
+
81
+ def stringify_entry(entry)
82
+ return entry.to_header if entry.respond_to?(:to_header)
83
+ return entry.to_s if entry.respond_to?(:to_s)
84
+ entry.inspect
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -1,5 +1,5 @@
1
1
  module MediaTypes
2
2
  module Serialization
3
- VERSION = '1.0.3'.freeze
3
+ VERSION = '1.3.1'.freeze
4
4
  end
5
5
  end
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.metadata['homepage_uri'] = spec.homepage
22
22
  spec.metadata['source_code_uri'] = spec.homepage
23
- spec.metadata['changelog_uri'] = spec.homepage + '/CHANGELOG.md'
23
+ spec.metadata['changelog_uri'] = spec.homepage + '/blob/master/CHANGELOG.md'
24
24
  else
25
25
  raise 'RubyGems 2.0 or newer is required to protect against ' \
26
26
  'public gem pushes.'
@@ -38,8 +38,6 @@ Gem::Specification.new do |spec|
38
38
  spec.add_dependency 'actionpack', '>= 4.0.0'
39
39
  spec.add_dependency 'activesupport', '>= 4.0.0'
40
40
  spec.add_dependency 'media_types', '>= 2.0.0', '< 3.0.0'
41
- spec.add_dependency 'http_headers-accept', '>= 0.2.2', '< 1.0.0'
42
- spec.add_dependency 'http_headers-link', '< 1.0.0'
43
41
 
44
42
  spec.add_development_dependency 'awesome_print'
45
43
  spec.add_development_dependency 'bundler'
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: media_types-serialization
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derk-Jan Karrenbeld
8
8
  - Max Maton
9
- autorequire:
9
+ autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2020-04-17 00:00:00.000000000 Z
12
+ date: 2021-08-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: actionpack
@@ -59,40 +59,6 @@ dependencies:
59
59
  - - "<"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 3.0.0
62
- - !ruby/object:Gem::Dependency
63
- name: http_headers-accept
64
- requirement: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: 0.2.2
69
- - - "<"
70
- - !ruby/object:Gem::Version
71
- version: 1.0.0
72
- type: :runtime
73
- prerelease: false
74
- version_requirements: !ruby/object:Gem::Requirement
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- version: 0.2.2
79
- - - "<"
80
- - !ruby/object:Gem::Version
81
- version: 1.0.0
82
- - !ruby/object:Gem::Dependency
83
- name: http_headers-link
84
- requirement: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - "<"
87
- - !ruby/object:Gem::Version
88
- version: 1.0.0
89
- type: :runtime
90
- prerelease: false
91
- version_requirements: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - "<"
94
- - !ruby/object:Gem::Version
95
- version: 1.0.0
96
62
  - !ruby/object:Gem::Dependency
97
63
  name: awesome_print
98
64
  requirement: !ruby/object:Gem::Requirement
@@ -177,7 +143,7 @@ dependencies:
177
143
  - - ">="
178
144
  - !ruby/object:Gem::Version
179
145
  version: '0'
180
- description:
146
+ description:
181
147
  email:
182
148
  - derk-jan@xpbytes.com
183
149
  - max@delftsolutions.nl
@@ -196,6 +162,7 @@ files:
196
162
  - ".idea/modules.xml"
197
163
  - ".idea/runConfigurations/test.xml"
198
164
  - ".idea/vcs.xml"
165
+ - ".prettierrc"
199
166
  - CHANGELOG.md
200
167
  - CODE_OF_CONDUCT.md
201
168
  - Gemfile
@@ -219,6 +186,9 @@ files:
219
186
  - lib/media_types/serialization/serializers/fallback_unsupported_media_type_serializer.rb
220
187
  - lib/media_types/serialization/serializers/input_validation_error_serializer.rb
221
188
  - lib/media_types/serialization/serializers/problem_serializer.rb
189
+ - lib/media_types/serialization/utils/accept_header.rb
190
+ - lib/media_types/serialization/utils/accept_language_header.rb
191
+ - lib/media_types/serialization/utils/header_list.rb
222
192
  - lib/media_types/serialization/version.rb
223
193
  - media_types-serialization.gemspec
224
194
  homepage: https://github.com/XPBytes/media_types-serialization
@@ -227,8 +197,8 @@ licenses:
227
197
  metadata:
228
198
  homepage_uri: https://github.com/XPBytes/media_types-serialization
229
199
  source_code_uri: https://github.com/XPBytes/media_types-serialization
230
- changelog_uri: https://github.com/XPBytes/media_types-serialization/CHANGELOG.md
231
- post_install_message:
200
+ changelog_uri: https://github.com/XPBytes/media_types-serialization/blob/master/CHANGELOG.md
201
+ post_install_message:
232
202
  rdoc_options: []
233
203
  require_paths:
234
204
  - lib
@@ -243,8 +213,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
243
213
  - !ruby/object:Gem::Version
244
214
  version: '0'
245
215
  requirements: []
246
- rubygems_version: 3.0.3
247
- signing_key:
216
+ rubygems_version: 3.1.6
217
+ signing_key:
248
218
  specification_version: 4
249
219
  summary: Add media types supported serialization using your favourite serializer
250
220
  test_files: []