http_accept 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ http_accept
2
+ ===========
3
+
4
+ Simple library for HTTP Accept header parsing and ordering.
5
+
6
+ ``` ruby
7
+ HTTPAccept::Parser.new("text/*, text/html, text/html;level=1, */*").run.map(&:to_s)
8
+ => ["text/html; level=1", "text/html", "text/*", "*/*"]
9
+ ```
10
+
11
+ Testing
12
+ -------
13
+
14
+ rake test
@@ -0,0 +1,7 @@
1
+ require "rake/testtask"
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs.push "lib", "test"
5
+ t.test_files = FileList['test/**/*_test.rb']
6
+ t.verbose = true
7
+ end
@@ -0,0 +1,2 @@
1
+ require "http_accept/media_type"
2
+ require "http_accept/parser"
@@ -0,0 +1,48 @@
1
+ module HTTPAccept
2
+ class MediaType
3
+ include Comparable
4
+
5
+ attr_accessor :format, :params
6
+
7
+ def initialize(args = {})
8
+ self.format = args[:format]
9
+ self.params = args[:params] || {}
10
+ end
11
+
12
+ def <=>(other)
13
+ # return -1 if more specific than other, so we end up at the front of the
14
+ # array
15
+ if !all_subtypes? && other.all_subtypes? || !all_types? && other.all_types?
16
+ -1
17
+ elsif all_subtypes? && !other.all_subtypes? || all_types? && !other.all_types?
18
+ 1
19
+ elsif params.count == other.params.count
20
+ 0
21
+ elsif params.count > other.params.count
22
+ -1
23
+ else
24
+ 1
25
+ end
26
+ end
27
+
28
+ def all_subtypes?
29
+ format.match(/\/\*/, format.length-2) != nil
30
+ end
31
+
32
+ def all_types?
33
+ format == "*/*"
34
+ end
35
+
36
+ def to_h
37
+ { :format => format, :params => params }
38
+ end
39
+
40
+ def to_s
41
+ if params.count > 0
42
+ format + "; " + params.map { |k, v| "#{k}=#{v}" }.join("; ")
43
+ else
44
+ format
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,23 @@
1
+ module HTTPAccept
2
+ class Parser
3
+ def initialize(content)
4
+ @content = content
5
+ end
6
+
7
+ def run
8
+ content = @content.gsub(/\AAccept:\s*/, '')
9
+ content.split(',').map { |s| s.strip }.map do |segment|
10
+ format, params = parse_params(segment)
11
+ MediaType.new(:format => format, :params => params)
12
+ end.sort
13
+ end
14
+
15
+ private
16
+
17
+ def parse_params(segment)
18
+ format, *params = segment.split(";")
19
+ params = Hash[*params.map { |p| p.scan(/(\w+)=([\w.]+)/) }.flatten]
20
+ return [format, params]
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_accept
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brandur
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: brandur@mutelight.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - Rakefile
22
+ - lib/http_accept/media_type.rb
23
+ - lib/http_accept/parser.rb
24
+ - lib/http_accept.rb
25
+ homepage: https://github.com/brandur/http_accept
26
+ licenses:
27
+ - MIT
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.23
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Simple library for HTTP Accept header parsing and ordering.
50
+ test_files: []