content-type 0.0.1

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,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,11 @@
1
+ StringLiterals:
2
+ EnforcedStyle: double_quotes
3
+
4
+ EmptyLines:
5
+ Enabled: false
6
+
7
+ Alias:
8
+ Enabled: false
9
+
10
+ Documentation:
11
+ Enabled: false
@@ -0,0 +1,2 @@
1
+ language: ruby
2
+ rvm: [ 1.9.3, 2.0.0, 2.1.0 ]
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "https://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rake"
5
+ gem "guard"
6
+ gem "guard-rspec"
7
+ end
8
+
9
+ group :test do
10
+ gem "coveralls", :require => false
11
+ gem "simplecov", :require => false
12
+
13
+ gem "rspec", ">= 2.14"
14
+ gem "rubocop", ">= 0.16"
15
+ gem "yardstick"
16
+ end
17
+
18
+ # Specify your gem's dependencies in content-type.gemspec
19
+ gemspec
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Aleksey V Zapparov
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,37 @@
1
+ # Content::Type
2
+
3
+ RFC-compliant Content-Type parser.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'content-type'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install content-type
18
+
19
+ ## Usage
20
+
21
+ ``` ruby
22
+ content_type = ContentType.parse "application/JSON; charset=utf-8; foo=bar"
23
+ content_type.mime_type # => "application/json"
24
+ content_type.type # => "application"
25
+ content_type.subtype # => "json"
26
+ content_type.charset # => "utf-8"
27
+ content_type.parameters["charset"] # => "utf-8"
28
+ content_type.parameters["foo"] # => "bar"
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( http://github.com/<my-github-username>/content-type/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :test => :spec
8
+
9
+ begin
10
+ require 'rubocop/rake_task'
11
+ Rubocop::RakeTask.new
12
+ rescue LoadError
13
+ task :rubocop do
14
+ $stderr.puts 'Rubocop is disabled'
15
+ end
16
+ end
17
+
18
+ require 'yardstick/rake/measurement'
19
+ Yardstick::Rake::Measurement.new do |measurement|
20
+ measurement.output = 'measurement/report.txt'
21
+ end
22
+
23
+ require 'yardstick/rake/verify'
24
+ Yardstick::Rake::Verify.new do |verify|
25
+ verify.threshold = 52.0
26
+ end
27
+
28
+ task :default => [:spec, :rubocop, :verify_measurements]
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'content_type/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "content-type"
8
+ spec.version = ContentType::VERSION
9
+ spec.authors = ["Aleksey V Zapparov"]
10
+ spec.email = ["ixti@member.fsf.org"]
11
+ spec.summary = "content-type-#{ContentType::VERSION}"
12
+ spec.description = %q{ContentType parser}
13
+ spec.homepage = "https://github.com/ixti/content-type"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "parslet", "~> 1.5"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ end
@@ -0,0 +1,2 @@
1
+ # coding: utf-8
2
+ require "content_type"
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+
3
+
4
+ require "content_type/parser"
5
+ require "content_type/version"
6
+
7
+
8
+ # ContentType structure
9
+ class ContentType
10
+ attr_reader :type, :subtype, :parameters
11
+
12
+ def initialize(parsed)
13
+ parsed = [parsed] unless parsed.is_a? Array
14
+
15
+ @type = parsed.first[:type].to_s.downcase
16
+ @subtype = parsed.first[:subtype].to_s.downcase
17
+ @parameters = Hash[parse_parameters parsed[1..-1]]
18
+ end
19
+
20
+ def mime_type
21
+ "#{type}/#{subtype}"
22
+ end
23
+
24
+ def charset
25
+ parameters["charset"]
26
+ end
27
+
28
+ def inspect
29
+ "#<ContentType #{mime_type} #{parameters.inspect}>"
30
+ end
31
+
32
+ def to_s
33
+ (["#{mime_type}"] + parameters.map { |k, v| "#{k}=#{v.to_s.inspect}" })
34
+ .compact.join("; ")
35
+ end
36
+ alias :to_str :to_s
37
+
38
+ def self.parse(str)
39
+ new Parser.new.parse str
40
+ end
41
+
42
+ protected
43
+
44
+ def parse_parameters(list)
45
+ Array(list).map do |hash|
46
+ [
47
+ hash[:parameter][:attribute].to_s.downcase,
48
+ hash[:parameter][:value].to_s
49
+ ]
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,75 @@
1
+ # coding: utf-8
2
+
3
+
4
+ # 3-rd party
5
+ require "parslet"
6
+
7
+
8
+ class ContentType
9
+ # ContentType string parser
10
+ class Parser < ::Parslet::Parser
11
+ class CharList # :nodoc:
12
+ def initialize(list = nil)
13
+ @list = list || yield
14
+ end
15
+
16
+ def -(other)
17
+ CharList.new @list - other.to_a
18
+ end
19
+
20
+ def +(other)
21
+ CharList.new @list + other.to_a
22
+ end
23
+
24
+ def to_a
25
+ @list.dup
26
+ end
27
+ alias :to_ary :to_a
28
+
29
+ def to_s
30
+ @list.join
31
+ end
32
+ alias :to_str :to_s
33
+
34
+ def size
35
+ to_s.size
36
+ end
37
+ end
38
+
39
+
40
+ def stri(s)
41
+ s.split(//).map { |c| match["#{c.upcase}#{c.downcase}"] }.reduce :>>
42
+ end
43
+
44
+
45
+ # rubocop:disable LineLength
46
+ # rubocop:disable Blocks
47
+ # rubocop:disable BlockAlignment
48
+
49
+ CHAR = CharList.new { (0..127).to_a.map(&:chr) }
50
+ CTLS = CharList.new { (0..31).to_a.map(&:chr) << 127.chr }
51
+ CR = CharList.new { [13.chr] }
52
+ LF = CharList.new { [10.chr] }
53
+ SPACE = CharList.new { [" "] }
54
+ HTAB = CharList.new { [9.chr] }
55
+ CRLF = CharList.new { [13.chr + 10.chr] }
56
+ SPECIALS = CharList.new { ["(", ")", "<", ">", "@", ",", ";", ":", "\\", "\"", ".", "[", "]"] }
57
+ TSPECIALS = CharList.new { SPECIALS + ["/", "?", "="] }
58
+
59
+ rule(:quoted_pair) { str("\\") >> match[Regexp.escape CHAR] }
60
+ rule(:linear_ws) { (str(CRLF).repeat(0, 1) >> (str(SPACE) | str(HTAB))).repeat(1) }
61
+ rule(:qtext) { match[Regexp.escape CHAR - ["\"", "\\"] - CR] }
62
+ rule(:quoted_string) { str('"') >> (qtext | quoted_pair).repeat.as(:value) >> str("\"") }
63
+ rule(:token) { match[Regexp.escape CHAR - SPACE - CTLS - TSPECIALS].repeat(1) }
64
+ rule(:space) { str(SPACE) }
65
+ rule(:x_token) { stri("x") >> str("-") >> token }
66
+ rule(:type) { stri("application") | stri("audio") | stri("mage") | stri("message") | stri("multipart") | stri("text") | stri("video") | x_token }
67
+ rule(:subtype) { token }
68
+ rule(:attribute) { token }
69
+ rule(:value) { token.as(:value) | quoted_string }
70
+ rule(:parameter) { attribute.as(:attribute) >> str("=") >> value }
71
+ rule(:parameters) { space.repeat >> str(";") >> space.repeat >> parameter.as(:parameter) }
72
+ rule(:content_type) { type.as(:type) >> str("/") >> subtype.as(:subtype) >> parameters.repeat }
73
+ root(:content_type)
74
+ end
75
+ end
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+
3
+
4
+ class ContentType
5
+ VERSION = "0.0.1"
6
+ end
@@ -0,0 +1,56 @@
1
+ # coding: utf-8
2
+
3
+
4
+ require "spec_helper"
5
+
6
+
7
+ describe ContentType do
8
+ subject(:content_type) { ContentType.parse raw }
9
+
10
+ describe ".parse" do
11
+ context "application/json" do
12
+ let(:raw) { "application/json" }
13
+
14
+ its(:type) { should eq "application" }
15
+ its(:subtype) { should eq "json" }
16
+ its(:charset) { should be_nil }
17
+ end
18
+
19
+ context "application/json; charset=utf-8" do
20
+ let(:raw) { "application/json; charset=utf-8" }
21
+
22
+ its(:type) { should eq "application" }
23
+ its(:subtype) { should eq "json" }
24
+ its(:charset) { should eq "utf-8" }
25
+ end
26
+
27
+ context "x-dead/beef; any=thing" do
28
+ let(:raw) { "x-dead/beef; any=thing" }
29
+
30
+ its(:type) { should eq "x-dead" }
31
+ its(:subtype) { should eq "beef" }
32
+ its(:parameters) { should eq "any" => "thing" }
33
+ end
34
+ end
35
+
36
+ describe "#type" do
37
+ let(:raw) { "APPLICATION/jSOn" }
38
+ it "is downcased" do
39
+ expect(content_type.type).to eq "application"
40
+ end
41
+ end
42
+
43
+ describe "#subtype" do
44
+ let(:raw) { "APPLICATION/jSOn" }
45
+ it "is downcased" do
46
+ expect(content_type.subtype).to eq "json"
47
+ end
48
+ end
49
+
50
+ describe "#parameters keys" do
51
+ let(:raw) { "APPLICATION/jSOn; CHARset=utf-8" }
52
+ it "are downcased" do
53
+ expect(content_type.parameters).to include "charset"
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,2 @@
1
+ # coding: utf-8
2
+ require "content_type"
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: content-type
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aleksey V Zapparov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: parslet
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.5'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.5'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.5'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.5'
46
+ description: ContentType parser
47
+ email:
48
+ - ixti@member.fsf.org
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rubocop.yml
55
+ - .travis.yml
56
+ - Gemfile
57
+ - Guardfile
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - content-type.gemspec
62
+ - lib/content-type.rb
63
+ - lib/content_type.rb
64
+ - lib/content_type/parser.rb
65
+ - lib/content_type/version.rb
66
+ - spec/lib/content_type_spec.rb
67
+ - spec/spec_helper.rb
68
+ homepage: https://github.com/ixti/content-type
69
+ licenses:
70
+ - MIT
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ segments:
82
+ - 0
83
+ hash: -979255587859896353
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ segments:
91
+ - 0
92
+ hash: -979255587859896353
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.23
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: content-type-0.0.1
99
+ test_files:
100
+ - spec/lib/content_type_spec.rb
101
+ - spec/spec_helper.rb
102
+ has_rdoc: