media_type 1.0.2

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b1d94978527d35ad0c0ecdb1fbf78cb1b2cec4cd
4
+ data.tar.gz: 4d367eb5e0ae97c1ae9c8eb91866e7741fad1b9f
5
+ SHA512:
6
+ metadata.gz: 74e29e05cfa760d018cf5bc5a7f4f033fb824a430910629bf9b00ef2c85bd28e13255c0860e937ff0e5768371e859b311972b1d65ea7f87b949b348c983fc829
7
+ data.tar.gz: bfae5e8c5d33b4c1e276cb8763c60a26bba204c819c8144c671ca2cc79218253b39e7406dbcad7972ca4d2af24823c325f599812a7070a9a12d84fc5e0ea08e6
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in media_type.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alyssa Ross
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,75 @@
1
+ # MediaType
2
+
3
+ A tiny library for parsing and generating Internet Media Types (AKA MIME types).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'media_type'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install media_type
20
+
21
+ ## Usage
22
+
23
+ ### Parsing a Media Type
24
+
25
+ ```ruby
26
+ media_type = MediaType.parse("application/vnd.adobe.xdp+xml; charset=utf-8")
27
+ media_type.type #=> "application"
28
+ media_type.tree #=> "vnd"
29
+ media_type.subtype #=> "adobe.xdp"
30
+ media_type.suffix #=> "xml"
31
+ media_type.parameters #=> {"charset"=>"utf-8"}
32
+ ```
33
+
34
+ To disable parameter parsing and leave the parameters as a string,
35
+ set the `parse_parameters` option to `false`:
36
+
37
+ ```ruby
38
+ media_type = MediaType.parse("text/plain; charset=US-ASCII", parse_parameters: false)
39
+ media_type.parameters #=> " charset=US-ASCII"
40
+ ```
41
+
42
+ ### Generating a Media Type
43
+
44
+ ```ruby
45
+ media_type = MediaType.new
46
+ media_type.type = "application"
47
+ media_type.tree = "vnd"
48
+ media_type.subtype = "adobe.xdp"
49
+ media_type.suffix = "xml"
50
+ media_type.parameters = { "charset" => "utf-8" }
51
+ media_type.to_s #=> "application/vnd.adobe.xdp+xml; charset=utf-8"
52
+ ```
53
+
54
+ ### Comparing Media Types
55
+
56
+ The `MediaType` class allows you to check whether two media types are
57
+ *semantically equal*, even if they aren't equal strings.
58
+
59
+ ```ruby
60
+ type1 = MediaType.parse('text/html; charset="utf-8"')
61
+ type2 = MediaType.parse('text/html; charset=utf-8')
62
+ type1 == type2 #=> true
63
+ ```
64
+
65
+ ## Contributing
66
+
67
+ 1. [Fork it](https://github.com/[my-github-username]/media_type/fork)
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create a new Pull Request
72
+
73
+ ---
74
+
75
+ [ ![Codeship Status for alyssais/media_type](https://codeship.com/projects/36ec2d70-622e-0132-f3b2-3a888924fd85/status)](https://codeship.com/projects/52003)
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,75 @@
1
+ class MediaType < Struct.new(:type, :tree, :subtype, :suffix, :parameters)
2
+ VERSION = "1.0.2"
3
+ MATCHER = /\A([^\/]+)\/(?:([^\.\+;]+)\.)?([^\s\+;]+)(?:\+([^\s;]+))?\s*(?:;(.*))?\Z/
4
+
5
+ def initialize(*args)
6
+ if args.length == 1 && (hash = args.first).is_a?(Hash)
7
+ symbolized_hash = hash.map { |k, v| [k.to_sym, v] }.to_h
8
+ super(*symbolized_hash.values_at(*self.class.members))
9
+ else
10
+ super
11
+ end
12
+ end
13
+
14
+ def self.parse(string, parse_parameters: true)
15
+ components = string.match(MATCHER)[1..-1]
16
+ if parse_parameters
17
+ components[-1] = parse_params(components[-1])
18
+ end
19
+ new(*components)
20
+ end
21
+
22
+ def self.parse_params(string)
23
+ return nil if string.nil?
24
+ components = string.split(/(?<!\\)"/) # seperate quoted strings
25
+ components.map!.with_index do |element, i|
26
+ if i.even? # not inside a quoted string
27
+ element.split(?;).
28
+ map(&:strip).
29
+ reject(&:empty?).
30
+ map { |s| s.split(?=).map(&:strip) } # seperate keys and values
31
+ else # inside a quoted string
32
+ element.gsub(/\\(.)/, '\1') # remove escapes
33
+ end
34
+ end
35
+ components.flatten! # now 1D array of strings, alternating key/value
36
+ components.each_slice(2).to_h
37
+ end
38
+
39
+ def self.encode_params(params)
40
+ params.map do |key, value|
41
+ value = %("#{value.gsub(?", '\"')}") if value[/["\s]/]
42
+ "#{key}=#{value}"
43
+ end.join("; ")
44
+ end
45
+
46
+ def to_s
47
+ string = "#{type}/"
48
+ string << "#{tree}." if tree
49
+ string << subtype
50
+ string << "+#{suffix}" if suffix
51
+ string << "; #{string_parameters.gsub(/\A\s+/, "")}" if parameters
52
+ string
53
+ end
54
+
55
+ def inspect
56
+ "#<#{self.class}:#{to_s}>"
57
+ end
58
+
59
+ def ==(other)
60
+ return false unless other.kind_of? MediaType
61
+ [self, other].map do |type|
62
+ hash = type.to_h
63
+ hash[:parameters] &&= type.parsed_parameters
64
+ hash
65
+ end.inject(:==)
66
+ end
67
+
68
+ def string_parameters
69
+ parameters.is_a?(Hash) ? self.class.encode_params(parameters) : parameters
70
+ end
71
+
72
+ def parsed_parameters
73
+ parameters.is_a?(Hash) ? parameters : self.class.parse_params(parameters)
74
+ end
75
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'media_type'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "media_type"
8
+ spec.version = MediaType::VERSION
9
+ spec.authors = ["Alyssa Ross"]
10
+ spec.email = ["hi@alyssa.is"]
11
+ spec.summary = %q{A library for parsing and generating Internet Media (MIME) Types}
12
+ spec.homepage = "https://github.com/alyssais/media_type"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,57 @@
1
+ text/html:
2
+ type: text
3
+ subtype: html
4
+
5
+ application/vnd.oasis.opendocument.text:
6
+ type: application
7
+ tree: vnd
8
+ subtype: oasis.opendocument.text
9
+
10
+ image/svg+xml:
11
+ type: image
12
+ subtype: svg
13
+ suffix: xml
14
+
15
+ text/plain; charset=US-ASCII:
16
+ type: text
17
+ subtype: plain
18
+ parameters:
19
+ charset: US-ASCII
20
+
21
+ application/vnd.etsi.asic-s+zip:
22
+ type: application
23
+ tree: vnd
24
+ subtype: etsi.asic-s
25
+ suffix: zip
26
+
27
+ text/vnd.abc; charset=utf-8:
28
+ type: text
29
+ tree: vnd
30
+ subtype: abc
31
+ parameters:
32
+ charset: utf-8
33
+
34
+ message/example ; foo=bar ; hello=" \"; world "; key= value:
35
+ type: message
36
+ subtype: example
37
+ parameters:
38
+ foo: bar
39
+ hello: ' "; world '
40
+ key: value
41
+
42
+ application/vnd.adobe.xdp+xml; charset=utf-8:
43
+ type: application
44
+ tree: vnd
45
+ subtype: adobe.xdp
46
+ suffix: xml
47
+ parameters:
48
+ charset: utf-8
49
+
50
+ application/vnd.adobe.xdp+xml ; charset=utf-8:
51
+ type: application
52
+ tree: vnd
53
+ subtype: adobe.xdp
54
+ suffix: xml
55
+ parameters:
56
+ charset: utf-8
57
+
@@ -0,0 +1,104 @@
1
+ require 'yaml'
2
+ require 'spec_helper'
3
+
4
+ fixtures = YAML.load_file("#{File.dirname(__FILE__)}/fixtures.yml")
5
+
6
+ describe MediaType do
7
+ it "has a version number" do
8
+ expect(MediaType::VERSION).not_to be nil
9
+ end
10
+
11
+ describe "#to_h" do
12
+ fixtures.each do |string, orig|
13
+ it "reproduces #{string} hash" do
14
+ hash = MediaType.new(orig).to_h
15
+ [:type, :tree, :subtype, :suffix, :parameters].each do |key|
16
+ expect(hash).to have_key(key)
17
+ expect(hash[key]).to eq orig[key.to_s]
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ describe "#parse" do
24
+ context "with parse_parameters enabled" do
25
+ fixtures.each do |string, hash|
26
+ it "correctly parses #{string}" do
27
+ type = MediaType.parse(string)
28
+ [:type, :tree, :subtype, :suffix, :parameters].each do |key|
29
+ expect(type.send(key)).to eq hash[key.to_s]
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ context "with parse_parameters disabled" do
36
+ fixtures.each do |string, hash|
37
+ it "correctly parses #{string}" do
38
+ type = MediaType.parse(string, parse_parameters: false)
39
+ [:type, :tree, :subtype, :suffix].each do |key|
40
+ expect(type.send(key)).to eq hash[key.to_s]
41
+ end
42
+ expect(type.parameters).to eq string.split(?;, 2)[1]
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ describe "#==" do
49
+ context "when types are equal" do
50
+ input1 = %{text/html ; charset=utf-8}
51
+ input2 = %{text/html; charset="utf-8"}
52
+ [true, false].repeated_permutation(2) do |parse1, parse2|
53
+ context "with parse_parameters set to #{parse1} for input1 and #{parse2} for input2" do
54
+ it "returns true" do
55
+ type1 = MediaType.parse(input1, parse_parameters: parse1)
56
+ type2 = MediaType.parse(input2, parse_parameters: parse2)
57
+ expect(type1).to eq type2
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ context "when types are not equal" do
64
+ it "returns false" do
65
+ expect(MediaType.parse("text/html")).not_to eq MediaType.parse("image/png")
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "#to_s" do
71
+ context "with hash parameters" do
72
+ fixtures.each do |string, _|
73
+ it "reproduces #{string}" do
74
+ type = MediaType.parse(string)
75
+ expect(MediaType.parse(type.to_s)).to eq type
76
+ end
77
+ end
78
+ end
79
+
80
+ context "with string parameters" do
81
+ fixtures.each do |string, _|
82
+ it "reproduces an equivalent to #{string}" do
83
+ type = MediaType.parse(string, parse_parameters: false)
84
+ expect(MediaType.parse(type.to_s)).to eq type
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ describe "#inspect" do
91
+ it "is correctly formatted" do
92
+ type = MediaType.parse("text/html")
93
+ expect(type.inspect).to eq "#<MediaType:text/html>"
94
+ end
95
+ end
96
+
97
+ describe "#parsed_parameters" do
98
+ context "when value is nil" do
99
+ it "returns nil" do
100
+ expect(MediaType.new.parsed_parameters).to be_nil
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'media_type'
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: media_type
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Alyssa Ross
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-29 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ description:
56
+ email:
57
+ - hi@alyssa.is
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/media_type.rb
70
+ - media_type.gemspec
71
+ - spec/fixtures.yml
72
+ - spec/media_type_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: https://github.com/alyssais/media_type
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.5.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A library for parsing and generating Internet Media (MIME) Types
98
+ test_files:
99
+ - spec/fixtures.yml
100
+ - spec/media_type_spec.rb
101
+ - spec/spec_helper.rb
102
+ has_rdoc: