rack-acceptable 0.1.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,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Paul Sadauskas
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ rack-acceptable
2
+ ===============
3
+
4
+ Adds a #acceptable_media_types method to Rack::Request objects so that full-featured content-negotiation can be performed.
5
+
6
+ Examples
7
+ --------
8
+
9
+ env['HTTP_ACCEPT'] #=> 'application/xml;q=0.8,text/html,text/plain;q=0.9'
10
+
11
+ req = Rack::Request.new(env)
12
+ req.acceptable_media_types #=> ['text/html', 'text/plain', 'application/xml']
13
+
14
+ req.acceptable_media_types.prioritize('application/xml', 'text/html') #=> ['text/html', application/xml']
15
+ req.acceptable_media_types.preference_of('text/plain', 'text/html') #=> 'text/html'
16
+ req.acceptable_media_types.first_acceptable('image/png', 'text/html') #=> 'text/html'
17
+
18
+
19
+ See spec/acceptable_media_types_spec.rb for more.
20
+
21
+
22
+
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-acceptable"
8
+ gem.version = "0.1.0"
9
+ gem.summary = "Rack::Request extension to handle conneg"
10
+ gem.description = "Ditto"
11
+ gem.email = "psadauskas@gmail.com"
12
+ gem.homepage = "http://github.com/paul/rack-acceptable"
13
+ gem.authors = ["Paul Sadauskas"]
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+ gem.add_development_dependency "yard", ">= 0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+
18
+ gem.add_dependency "rack", ">= 1.1.0"
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+ require 'spec/rake/spectask'
26
+ Spec::Rake::SpecTask.new(:spec) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :spec => :check_dependencies
38
+
39
+ task :default => :spec
40
+
41
+ begin
42
+ require 'yard'
43
+ YARD::Rake::YardocTask.new
44
+ rescue LoadError
45
+ task :yardoc do
46
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
47
+ end
48
+ end
@@ -0,0 +1,175 @@
1
+ # Much of this code was stolen from http://github.com/mynyml/rack-accept-media-types
2
+ #
3
+ # Enhancement added by me to include subtype wildcards and handling for client
4
+ # preferred media type.
5
+
6
+ module Rack
7
+ class Request
8
+ # The media types of the HTTP_ACCEPT header ordered according to their
9
+ # "quality" (preference level), without any media type parameters.
10
+ #
11
+ # ===== Examples
12
+ #
13
+ # env['HTTP_ACCEPT'] #=> 'application/xml;q=0.8,text/html,text/plain;q=0.9'
14
+ #
15
+ # req = Rack::Request.new(env)
16
+ # req.acceptable_media_types #=> ['text/html', 'text/plain', 'application/xml']
17
+ #
18
+ # req.acceptable_media_types.prioritize('application/xml', 'text/html') #=> ['text/html', application/xml']
19
+ # req.acceptable_media_types.preference_of('text/plain', 'text/html') #=> 'text/html'
20
+ # req.acceptable_media_types.first_acceptable('image/png', 'text/html') #=> 'text/html'
21
+ #
22
+ # For more information, see:
23
+ # * Accept header: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
24
+ # * Quality values: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.9
25
+ #
26
+ # ===== Returns
27
+ #
28
+ # AcceptableMediaTypes:: ordered list of accept header's media types
29
+ #
30
+ def acceptable_media_types
31
+ @acceptable_media_types ||= AcceptableMediaTypes.new(@env['HTTP_ACCEPT'])
32
+ end
33
+
34
+ class AcceptableMediaTypes < Array
35
+ #--
36
+ # NOTE
37
+ # Reason for special handling of nil accept header:
38
+ #
39
+ # "If no Accept header field is present, then it is assumed that the client
40
+ # accepts all media types."
41
+ #
42
+ def initialize(*media_types)
43
+ media_types = media_types.flatten
44
+ if media_types.empty?
45
+ replace(['*/*'])
46
+ elsif media_types.size == 1
47
+ replace(media_types.first.split(','))
48
+ else
49
+ replace(media_types)
50
+ end
51
+ end
52
+
53
+ def replace(media_types = [])
54
+ super(order(media_types))
55
+ end
56
+
57
+ # Prioritize a list of media types in order of client preference
58
+ def prioritize(*types)
59
+ types = self.class.new(*types)
60
+ select { |acceptable_type| types.include?(acceptable_type) }
61
+ end
62
+
63
+ # Pick the first acceptable of a list of media types. This is
64
+ # useful for serving html to broken browsers (>_> Safari) that
65
+ # provide a wrong Accept header.
66
+ #
67
+ # Example:
68
+ #
69
+ # accept = AcceptableMediaTypes.new("application/xml;q=1.0,text/html;q=0.1")
70
+ # accept.first_acceptable('text/html,application/xml')
71
+ # #=> 'text/html'
72
+ #
73
+ def first_acceptable(*types)
74
+ types = self.class.new(*types)
75
+ types.detect { |type| include?(type) }
76
+ end
77
+
78
+ # The prefered type out of list of possible media types
79
+ def preference_of(*types)
80
+ types = self.class.new(*types)
81
+ detect { |acceptable_type| types.include?(acceptable_type) }
82
+ end
83
+
84
+ private
85
+
86
+ # Order media types by quality values, and remove invalid types
87
+ def order(media_types = [])
88
+ media_types.map! { |t| t.is_a?(MediaType) ? t : MediaType.new(t) }
89
+ media_types.sort!
90
+ media_types.select { |t| t.valid? }
91
+ end
92
+
93
+ class MediaType
94
+ include Comparable
95
+
96
+ attr_accessor :media_type, :range, :quality
97
+
98
+ ANY = "*"
99
+
100
+ def initialize(media_type)
101
+ @media_type = media_type
102
+ end
103
+
104
+ # media-range = ( "*/*"
105
+ # | ( type "/" "*" )
106
+ # | ( type "/" subtype )
107
+ # ) *( ";" parameter )
108
+ def range
109
+ @range ||= media_type.split(';').first
110
+ end
111
+
112
+ # qvalue = ( "0" [ "." 0*3DIGIT ] )
113
+ # | ( "1" [ "." 0*3("0") ] )
114
+ def quality
115
+ @quality ||= extract_quality(media_type.split(';')[1..-1])
116
+ end
117
+
118
+ def <=>(other)
119
+ other.quality <=> quality
120
+ end
121
+
122
+ def ==(other_media_type)
123
+ return false unless other_media_type.is_a?(String) || other_media_type.is_a?(MediaType)
124
+ other_media_type = MediaType.new(other_media_type) unless other_media_type.is_a?(MediaType)
125
+
126
+ if type == ANY
127
+ true
128
+ elsif type == other_media_type.type
129
+ subtype == ANY || subtype == other_media_type.subtype
130
+ else
131
+ false
132
+ end
133
+ end
134
+
135
+ def inspect
136
+ %Q{#<#{self.class}:0x%1x #{to_s}>} % object_id
137
+ end
138
+
139
+ def to_s
140
+ @media_type
141
+ end
142
+
143
+ # "A weight is normalized to a real number in the range 0 through 1,
144
+ # where 0 is the minimum and 1 the maximum value. If a parameter has a
145
+ # quality value of 0, then content with this parameter is `not
146
+ # acceptable' for the client."
147
+ #
148
+ def valid?
149
+ quality > 0 && quality <= 1
150
+ end
151
+
152
+ def type
153
+ @type ||= range.split('/').first
154
+ end
155
+
156
+ def subtype
157
+ @subtype ||= range.split('/').last
158
+ end
159
+
160
+ private
161
+
162
+ # Extract value from 'q=FLOAT' parameter if present, otherwise assume 1
163
+ #
164
+ # "The default value is q=1."
165
+ #
166
+ def extract_quality(params)
167
+ q = params.detect {|p| p.match(/q=\d\.?\d{0,3}/) }
168
+ q ? q.split('=').last.to_f : 1.0
169
+ end
170
+
171
+ end
172
+
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rack-acceptable}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Paul Sadauskas"]
12
+ s.date = %q{2010-02-06}
13
+ s.description = %q{Ditto}
14
+ s.email = %q{psadauskas@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.mkd"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.mkd",
24
+ "Rakefile",
25
+ "lib/rack/acceptable.rb",
26
+ "rack-acceptable.gemspec",
27
+ "spec/acceptable_media_types_spec.rb",
28
+ "spec/spec.opts",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/paul/rack-acceptable}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{Rack::Request extension to handle conneg}
36
+ s.test_files = [
37
+ "spec/acceptable_media_types_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
47
+ s.add_development_dependency(%q<yard>, [">= 0"])
48
+ s.add_runtime_dependency(%q<rack>, [">= 1.1.0"])
49
+ else
50
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
51
+ s.add_dependency(%q<yard>, [">= 0"])
52
+ s.add_dependency(%q<rack>, [">= 1.1.0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ s.add_dependency(%q<yard>, [">= 0"])
57
+ s.add_dependency(%q<rack>, [">= 1.1.0"])
58
+ end
59
+ end
60
+
@@ -0,0 +1,92 @@
1
+ require File.expand_path("../spec_helper", __FILE__)
2
+
3
+ describe "Acceptable media types hander" do
4
+
5
+ describe "#include?" do
6
+ it 'should know if it includes a media type' do
7
+ mt = Rack::Request::AcceptableMediaTypes.new("text/html")
8
+ mt.include?("text/html").should be_true
9
+ mt.include?("application/xml").should be_false
10
+ end
11
+
12
+ it 'should know if */* includes any media type' do
13
+ mt = Rack::Request::AcceptableMediaTypes.new("*/*")
14
+ mt.include?("text/html").should be_true
15
+ mt.include?("application/xml").should be_true
16
+ end
17
+
18
+ it 'should know if text/* includes any media subtype' do
19
+ mt = Rack::Request::AcceptableMediaTypes.new("text/*")
20
+ mt.include?("text/html").should be_true
21
+ mt.include?("application/xml").should be_false
22
+ end
23
+ end
24
+
25
+ describe "#preference_of?" do
26
+ it 'should pick the first one by order' do
27
+ mt = Rack::Request::AcceptableMediaTypes.new("text/html,application/xml")
28
+ mt.preference_of('application/xml,text/html').should == 'text/html'
29
+ end
30
+
31
+ it 'should pick by quality' do
32
+ mt = Rack::Request::AcceptableMediaTypes.new("text/html;q=0.9,application/xml;q=1.0")
33
+ mt.preference_of('application/xml,text/html').should == 'application/xml'
34
+ end
35
+ end
36
+
37
+ describe "#prioritize" do
38
+ it 'should order them by client order' do
39
+ mt = Rack::Request::AcceptableMediaTypes.new("text/html,application/xml")
40
+ mt.prioritize('application/xml,text/html').should == ["text/html", "application/xml"]
41
+ end
42
+
43
+ it 'should order them by quality' do
44
+ mt = Rack::Request::AcceptableMediaTypes.new("text/html;q=0.9,application/xml;q=1.0")
45
+ mt.prioritize('application/xml,text/html').should == ['application/xml', 'text/html']
46
+ end
47
+ end
48
+
49
+ describe "#first_acceptable" do
50
+ it 'should pick the first media type that is acceptable' do
51
+ mt = Rack::Request::AcceptableMediaTypes.new("text/html,application/xml")
52
+ mt.first_acceptable('image/png,application/xml').should == 'application/xml'
53
+ end
54
+
55
+ it 'should pick the first given media type, even if its not the most preferred by quality' do
56
+ mt = Rack::Request::AcceptableMediaTypes.new("text/html;q=0.5,application/xml")
57
+ mt.first_acceptable('text/html,application/xml').should == 'text/html'
58
+ end
59
+
60
+ it 'should serve html to safari\'s broken-ass Accept header' do
61
+ safari_accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
62
+ mt = Rack::Request::AcceptableMediaTypes.new(safari_accept)
63
+ mt.first_acceptable('text/html,application/xml').should == 'text/html'
64
+ end
65
+ end
66
+
67
+ describe 'initialization' do
68
+ it 'should take a http accept header string' do
69
+ mt = Rack::Request::AcceptableMediaTypes.new("application/xml,text/html")
70
+ mt.should have(2).items
71
+ end
72
+
73
+ it 'should take an array of media type strings' do
74
+ mt = Rack::Request::AcceptableMediaTypes.new("application/xml", "text/html")
75
+ mt.should have(2).items
76
+ end
77
+
78
+ it 'should take an array of MediaType objects' do
79
+ xml = Rack::Request::AcceptableMediaTypes::MediaType.new("application/xml")
80
+ html = Rack::Request::AcceptableMediaTypes::MediaType.new("text/html")
81
+ mt = Rack::Request::AcceptableMediaTypes.new(xml, html)
82
+ mt.should have(2).items
83
+ end
84
+
85
+ it 'should not arbitrarily reorder media types with no quality param' do
86
+ mt = Rack::Request::AcceptableMediaTypes.new("application/xml,text/html")
87
+ mt.first.should == "application/xml"
88
+ mt.last.should == "text/html"
89
+ end
90
+ end
91
+
92
+ end
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format nested
3
+ --loadby random
4
+ --backtrace
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rack/acceptable'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'pp'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-acceptable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Sadauskas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-06 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: yard
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rack
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.1.0
44
+ version:
45
+ description: Ditto
46
+ email: psadauskas@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.mkd
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README.mkd
59
+ - Rakefile
60
+ - lib/rack/acceptable.rb
61
+ - rack-acceptable.gemspec
62
+ - spec/acceptable_media_types_spec.rb
63
+ - spec/spec.opts
64
+ - spec/spec_helper.rb
65
+ has_rdoc: true
66
+ homepage: http://github.com/paul/rack-acceptable
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --charset=UTF-8
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ version:
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.5
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Rack::Request extension to handle conneg
93
+ test_files:
94
+ - spec/acceptable_media_types_spec.rb
95
+ - spec/spec_helper.rb