media_types-serialization 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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.1'.freeze
3
+ VERSION = '1.2.0'.freeze
4
4
  end
5
5
  end
@@ -6,8 +6,8 @@ require 'media_types/serialization/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'media_types-serialization'
8
8
  spec.version = MediaTypes::Serialization::VERSION
9
- spec.authors = ['Derk-Jan Karrenbeld']
10
- spec.email = ['derk-jan@xpbytes.com']
9
+ spec.authors = ['Derk-Jan Karrenbeld', 'Max Maton']
10
+ spec.email = ['derk-jan@xpbytes.com', 'max@delftsolutions.nl']
11
11
 
12
12
  spec.summary = 'Add media types supported serialization using your favourite serializer'
13
13
  spec.homepage = 'https://github.com/XPBytes/media_types-serialization'
@@ -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,14 +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.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derk-Jan Karrenbeld
8
- autorequire:
8
+ - Max Maton
9
+ autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2020-04-15 00:00:00.000000000 Z
12
+ date: 2021-07-13 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: actionpack
@@ -58,40 +59,6 @@ dependencies:
58
59
  - - "<"
59
60
  - !ruby/object:Gem::Version
60
61
  version: 3.0.0
61
- - !ruby/object:Gem::Dependency
62
- name: http_headers-accept
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- version: 0.2.2
68
- - - "<"
69
- - !ruby/object:Gem::Version
70
- version: 1.0.0
71
- type: :runtime
72
- prerelease: false
73
- version_requirements: !ruby/object:Gem::Requirement
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- version: 0.2.2
78
- - - "<"
79
- - !ruby/object:Gem::Version
80
- version: 1.0.0
81
- - !ruby/object:Gem::Dependency
82
- name: http_headers-link
83
- requirement: !ruby/object:Gem::Requirement
84
- requirements:
85
- - - "<"
86
- - !ruby/object:Gem::Version
87
- version: 1.0.0
88
- type: :runtime
89
- prerelease: false
90
- version_requirements: !ruby/object:Gem::Requirement
91
- requirements:
92
- - - "<"
93
- - !ruby/object:Gem::Version
94
- version: 1.0.0
95
62
  - !ruby/object:Gem::Dependency
96
63
  name: awesome_print
97
64
  requirement: !ruby/object:Gem::Requirement
@@ -176,9 +143,10 @@ dependencies:
176
143
  - - ">="
177
144
  - !ruby/object:Gem::Version
178
145
  version: '0'
179
- description:
146
+ description:
180
147
  email:
181
148
  - derk-jan@xpbytes.com
149
+ - max@delftsolutions.nl
182
150
  executables: []
183
151
  extensions: []
184
152
  extra_rdoc_files: []
@@ -194,6 +162,7 @@ files:
194
162
  - ".idea/modules.xml"
195
163
  - ".idea/runConfigurations/test.xml"
196
164
  - ".idea/vcs.xml"
165
+ - ".prettierrc"
197
166
  - CHANGELOG.md
198
167
  - CODE_OF_CONDUCT.md
199
168
  - Gemfile
@@ -217,6 +186,9 @@ files:
217
186
  - lib/media_types/serialization/serializers/fallback_unsupported_media_type_serializer.rb
218
187
  - lib/media_types/serialization/serializers/input_validation_error_serializer.rb
219
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
220
192
  - lib/media_types/serialization/version.rb
221
193
  - media_types-serialization.gemspec
222
194
  homepage: https://github.com/XPBytes/media_types-serialization
@@ -225,8 +197,8 @@ licenses:
225
197
  metadata:
226
198
  homepage_uri: https://github.com/XPBytes/media_types-serialization
227
199
  source_code_uri: https://github.com/XPBytes/media_types-serialization
228
- changelog_uri: https://github.com/XPBytes/media_types-serialization/CHANGELOG.md
229
- post_install_message:
200
+ changelog_uri: https://github.com/XPBytes/media_types-serialization/blob/master/CHANGELOG.md
201
+ post_install_message:
230
202
  rdoc_options: []
231
203
  require_paths:
232
204
  - lib
@@ -241,8 +213,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
241
213
  - !ruby/object:Gem::Version
242
214
  version: '0'
243
215
  requirements: []
244
- rubygems_version: 3.0.3
245
- signing_key:
216
+ rubygems_version: 3.1.6
217
+ signing_key:
246
218
  specification_version: 4
247
219
  summary: Add media types supported serialization using your favourite serializer
248
220
  test_files: []