rack-accept 0.4.4 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +144 -0
- data/lib/rack/accept/header.rb +1 -1
- data/lib/rack/accept/language.rb +2 -0
- data/lib/rack/accept/version.rb +1 -1
- data/rack-accept.gemspec +3 -3
- data/test/language_test.rb +12 -0
- data/test/media_type_test.rb +4 -0
- metadata +43 -52
- data/README +0 -127
- data/doc/index.markdown +0 -35
- data/doc/license.markdown +0 -22
- data/doc/usage.markdown +0 -74
data/README.md
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
# Rack::Accept
|
2
|
+
|
3
|
+
**Rack::Accept** is a suite of tools for Ruby/Rack applications that eases the
|
4
|
+
complexity of building and interpreting the Accept* family of [HTTP request headers][rfc].
|
5
|
+
|
6
|
+
Some features of the library are:
|
7
|
+
|
8
|
+
* Strict adherence to [RFC 2616][rfc], specifically [section 14][rfc-sec14]
|
9
|
+
* Full support for the [Accept][rfc-sec14-1], [Accept-Charset][rfc-sec14-2],
|
10
|
+
[Accept-Encoding][rfc-sec14-3], and [Accept-Language][rfc-sec14-4] HTTP
|
11
|
+
request headers
|
12
|
+
* May be used as [Rack][rack] middleware or standalone
|
13
|
+
* A comprehensive [test suite][test] that covers many edge cases
|
14
|
+
|
15
|
+
[rfc]: http://www.w3.org/Protocols/rfc2616/rfc2616.html
|
16
|
+
[rfc-sec14]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
17
|
+
[rfc-sec14-1]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
18
|
+
[rfc-sec14-2]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.2
|
19
|
+
[rfc-sec14-3]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
|
20
|
+
[rfc-sec14-4]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
|
21
|
+
[rack]: http://rack.rubyforge.org/
|
22
|
+
[test]: http://github.com/mjijackson/rack-accept/tree/master/test/
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
**Using [RubyGems](http://rubygems.org/):**
|
27
|
+
|
28
|
+
$ sudo gem install rack-accept
|
29
|
+
|
30
|
+
**From a local copy:**
|
31
|
+
|
32
|
+
$ git clone git://github.com/mjijackson/rack-accept.git
|
33
|
+
$ cd rack-accept
|
34
|
+
$ rake package && sudo rake install
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
**Rack::Accept** implements the Rack middleware interface and may be used with any
|
39
|
+
Rack-based application. Simply insert the `Rack::Accept` module in your Rack
|
40
|
+
middleware pipeline and access the `Rack::Accept::Request` object in the
|
41
|
+
`rack-accept.request` environment key, as in the following example.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
require 'rack/accept'
|
45
|
+
|
46
|
+
use Rack::Accept
|
47
|
+
|
48
|
+
app = lambda do |env|
|
49
|
+
accept = env['rack-accept.request']
|
50
|
+
response = Rack::Response.new
|
51
|
+
|
52
|
+
if accept.media_type?('text/html')
|
53
|
+
response['Content-Type'] = 'text/html'
|
54
|
+
response.write "<p>Hello. You accept text/html!</p>"
|
55
|
+
else
|
56
|
+
response['Content-Type'] = 'text/plain'
|
57
|
+
response.write "Apparently you don't accept text/html. Too bad."
|
58
|
+
end
|
59
|
+
|
60
|
+
response.finish
|
61
|
+
end
|
62
|
+
|
63
|
+
run app
|
64
|
+
```
|
65
|
+
|
66
|
+
**Rack::Accept** can also construct automatic [406][406] responses if you set up
|
67
|
+
the types of media, character sets, encoding, or languages your server is able
|
68
|
+
to serve ahead of time. If you pass a configuration block to your `use`
|
69
|
+
statement it will yield the `Rack::Accept::Context` object that is used for that
|
70
|
+
invocation.
|
71
|
+
|
72
|
+
[406]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
require 'rack/accept'
|
76
|
+
|
77
|
+
use(Rack::Accept) do |context|
|
78
|
+
# We only ever serve content in English or Japanese from this site, so if
|
79
|
+
# the user doesn't accept either of these we will respond with a 406.
|
80
|
+
context.languages = %w< en jp >
|
81
|
+
end
|
82
|
+
|
83
|
+
app = ...
|
84
|
+
|
85
|
+
run app
|
86
|
+
```
|
87
|
+
|
88
|
+
**Note:** _You should think carefully before using Rack::Accept in this way.
|
89
|
+
Many user agents are careless about the types of Accept headers they send, and
|
90
|
+
depend on apps not being too picky. Instead of automatically sending a 406, you
|
91
|
+
should probably only send one when absolutely necessary._
|
92
|
+
|
93
|
+
Additionally, **Rack::Accept** may be used outside of a Rack context to provide
|
94
|
+
any Ruby app the ability to construct and interpret Accept headers.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
require 'rack/accept'
|
98
|
+
|
99
|
+
mtype = Rack::Accept::MediaType.new
|
100
|
+
mtype.qvalues = { 'text/html' => 1, 'text/*' => 0.8, '*/*' => 0.5 }
|
101
|
+
mtype.to_s # => "Accept: text/html, text/*;q=0.8, */*;q=0.5"
|
102
|
+
|
103
|
+
cset = Rack::Accept::Charset.new('unicode-1-1, iso-8859-5;q=0.8')
|
104
|
+
cset.best_of(%w< iso-8859-5 unicode-1-1 >) # => "unicode-1-1"
|
105
|
+
cset.accept?('iso-8859-1') # => true
|
106
|
+
```
|
107
|
+
|
108
|
+
The very last line in this example may look like a mistake to someone not
|
109
|
+
familiar with the intricacies of [the spec][rfc-sec14-3], but it's actually
|
110
|
+
correct. It just puts emphasis on the convenience of using this library so you
|
111
|
+
don't have to worry about these kinds of details.
|
112
|
+
|
113
|
+
## Four-letter Words
|
114
|
+
|
115
|
+
- Spec: [http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html][rfc-sec14]
|
116
|
+
- Code: [http://github.com/mjijackson/rack-accept][code]
|
117
|
+
- Bugs: [http://github.com/mjijackson/rack-accept/issues][bugs]
|
118
|
+
- Docs: [http://mjijackson.github.com/rack-accept][docs]
|
119
|
+
|
120
|
+
[code]: http://github.com/mjijackson/rack-accept
|
121
|
+
[bugs]: http://github.com/mjijackson/rack-accept/issues
|
122
|
+
[docs]: http://mjijackson.github.com/rack-accept
|
123
|
+
|
124
|
+
## License
|
125
|
+
|
126
|
+
Copyright 2012 Michael Jackson
|
127
|
+
|
128
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
129
|
+
of this software and associated documentation files (the "Software"), to deal
|
130
|
+
in the Software without restriction, including without limitation the rights
|
131
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
132
|
+
copies of the Software, and to permit persons to whom the Software is
|
133
|
+
furnished to do so, subject to the following conditions:
|
134
|
+
|
135
|
+
The above copyright notice and this permission notice shall be included in
|
136
|
+
all copies or substantial portions of the Software.
|
137
|
+
|
138
|
+
The software is provided "as is", without warranty of any kind, express or
|
139
|
+
implied, including but not limited to the warranties of merchantability,
|
140
|
+
fitness for a particular purpose and noninfringement. In no event shall the
|
141
|
+
authors or copyright holders be liable for any claim, damages or other
|
142
|
+
liability, whether in an action of contract, tort or otherwise, arising from,
|
143
|
+
out of or in connection with the software or the use or other dealings in
|
144
|
+
the software.
|
data/lib/rack/accept/header.rb
CHANGED
@@ -14,7 +14,7 @@ module Rack::Accept
|
|
14
14
|
m = /^([^\s,]+?)(?:\s*;\s*q\s*=\s*(\d+(?:\.\d+)?))?$/.match(part)
|
15
15
|
|
16
16
|
if m
|
17
|
-
qvalues[m[1]] = normalize_qvalue((m[2] || 1).to_f)
|
17
|
+
qvalues[m[1].downcase] = normalize_qvalue((m[2] || 1).to_f)
|
18
18
|
else
|
19
19
|
raise "Invalid header value: #{part.inspect}"
|
20
20
|
end
|
data/lib/rack/accept/language.rb
CHANGED
@@ -6,6 +6,7 @@ module Rack::Accept
|
|
6
6
|
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
|
7
7
|
class Language
|
8
8
|
include Header
|
9
|
+
attr_writer :first_level_match
|
9
10
|
|
10
11
|
# The name of this header.
|
11
12
|
def name
|
@@ -24,6 +25,7 @@ module Rack::Accept
|
|
24
25
|
# +language+, ordered by precedence.
|
25
26
|
def matches(language)
|
26
27
|
values.select {|v|
|
28
|
+
v = v.match(/^(.+?)-/) ? $1 : v if @first_level_match
|
27
29
|
v == language || v == '*' || (language.match(/^(.+?)-/) && v == $1)
|
28
30
|
}.sort {|a, b|
|
29
31
|
# "*" gets least precedence, any others are compared based on length.
|
data/lib/rack/accept/version.rb
CHANGED
data/rack-accept.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.files = Dir['doc/**/*'] +
|
19
19
|
Dir['lib/**/*.rb'] +
|
20
20
|
Dir['test/*.rb'] +
|
21
|
-
%w< CHANGES rack-accept.gemspec Rakefile README >
|
21
|
+
%w< CHANGES rack-accept.gemspec Rakefile README.md >
|
22
22
|
|
23
23
|
s.test_files = s.files.select {|path| path =~ /^test\/.*_test.rb/ }
|
24
24
|
|
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
|
28
28
|
s.has_rdoc = true
|
29
29
|
s.rdoc_options = %w< --line-numbers --inline-source --title Rack::Accept --main Rack::Accept >
|
30
|
-
s.extra_rdoc_files = %w< CHANGES README >
|
30
|
+
s.extra_rdoc_files = %w< CHANGES README.md >
|
31
31
|
|
32
|
-
s.homepage = 'http://mjijackson.com/rack-accept'
|
32
|
+
s.homepage = 'http://mjijackson.github.com/rack-accept'
|
33
33
|
end
|
data/test/language_test.rb
CHANGED
@@ -31,5 +31,17 @@ class LanguageTest < Test::Unit::TestCase
|
|
31
31
|
assert_equal('en', l.best_of(%w< en da >))
|
32
32
|
assert_equal('en-us', l.best_of(%w< en-us en-au >))
|
33
33
|
assert_equal(nil, l.best_of(%w< da >))
|
34
|
+
|
35
|
+
l = L.new('en;q=0.5, en-GB, fr-CA')
|
36
|
+
assert_equal('en-gb', l.best_of(%w< en en-gb >))
|
37
|
+
|
38
|
+
l = L.new('en-gb;q=0.5, EN, fr-CA')
|
39
|
+
assert_equal('en', l.best_of(%w< en en-gb >))
|
40
|
+
|
41
|
+
l = L.new('en;q=0.5, fr-ca')
|
42
|
+
assert_equal('en', l.best_of(%w< en fr >))
|
43
|
+
|
44
|
+
l.first_level_match = true
|
45
|
+
assert_equal('fr', l.best_of(%w< en fr >))
|
34
46
|
end
|
35
47
|
end
|
data/test/media_type_test.rb
CHANGED
@@ -36,6 +36,10 @@ class MediaTypeTest < Test::Unit::TestCase
|
|
36
36
|
m = M.new('text/*')
|
37
37
|
assert_equal('text/html', m.best_of(%w< text/html text/xml >))
|
38
38
|
assert_equal('text/xml', m.best_of(%w< text/xml text/html >))
|
39
|
+
|
40
|
+
m = M.new('TEXT/*')
|
41
|
+
assert_equal('text/html', m.best_of(%w< text/html text/xml >))
|
42
|
+
assert_equal('text/xml', m.best_of(%w< text/xml text/html >))
|
39
43
|
end
|
40
44
|
|
41
45
|
def test_extension
|
metadata
CHANGED
@@ -1,53 +1,47 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-accept
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.5
|
4
5
|
prerelease:
|
5
|
-
version: 0.4.4
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Michael Jackson
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-06-15 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
17
15
|
name: rack
|
18
|
-
|
19
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &70171348874000 !ruby/object:Gem::Requirement
|
20
17
|
none: false
|
21
|
-
requirements:
|
22
|
-
- -
|
23
|
-
- !ruby/object:Gem::Version
|
24
|
-
version:
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.4'
|
25
22
|
type: :runtime
|
26
|
-
version_requirements: *id001
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
23
|
prerelease: false
|
30
|
-
|
24
|
+
version_requirements: *70171348874000
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70171348873580 !ruby/object:Gem::Requirement
|
31
28
|
none: false
|
32
|
-
requirements:
|
33
|
-
- -
|
34
|
-
- !ruby/object:Gem::Version
|
35
|
-
version:
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
36
33
|
type: :development
|
37
|
-
|
38
|
-
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70171348873580
|
36
|
+
description: HTTP Accept, Accept-Charset, Accept-Encoding, and Accept-Language for
|
37
|
+
Ruby/Rack
|
39
38
|
email: mjijackson@gmail.com
|
40
39
|
executables: []
|
41
|
-
|
42
40
|
extensions: []
|
43
|
-
|
44
|
-
extra_rdoc_files:
|
41
|
+
extra_rdoc_files:
|
45
42
|
- CHANGES
|
46
|
-
- README
|
47
|
-
files:
|
48
|
-
- doc/index.markdown
|
49
|
-
- doc/license.markdown
|
50
|
-
- doc/usage.markdown
|
43
|
+
- README.md
|
44
|
+
files:
|
51
45
|
- lib/rack/accept/charset.rb
|
52
46
|
- lib/rack/accept/context.rb
|
53
47
|
- lib/rack/accept/encoding.rb
|
@@ -69,41 +63,38 @@ files:
|
|
69
63
|
- CHANGES
|
70
64
|
- rack-accept.gemspec
|
71
65
|
- Rakefile
|
72
|
-
- README
|
73
|
-
|
74
|
-
homepage: http://mjijackson.com/rack-accept
|
66
|
+
- README.md
|
67
|
+
homepage: http://mjijackson.github.com/rack-accept
|
75
68
|
licenses: []
|
76
|
-
|
77
69
|
post_install_message:
|
78
|
-
rdoc_options:
|
70
|
+
rdoc_options:
|
79
71
|
- --line-numbers
|
80
72
|
- --inline-source
|
81
73
|
- --title
|
82
74
|
- Rack::Accept
|
83
75
|
- --main
|
84
76
|
- Rack::Accept
|
85
|
-
require_paths:
|
77
|
+
require_paths:
|
86
78
|
- lib
|
87
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
80
|
none: false
|
89
|
-
requirements:
|
90
|
-
- -
|
91
|
-
- !ruby/object:Gem::Version
|
92
|
-
version:
|
93
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
86
|
none: false
|
95
|
-
requirements:
|
96
|
-
- -
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
version:
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
99
91
|
requirements: []
|
100
|
-
|
101
92
|
rubyforge_project:
|
102
|
-
rubygems_version: 1.
|
93
|
+
rubygems_version: 1.8.17
|
103
94
|
signing_key:
|
104
95
|
specification_version: 3
|
105
96
|
summary: HTTP Accept* for Ruby/Rack
|
106
|
-
test_files:
|
97
|
+
test_files:
|
107
98
|
- test/charset_test.rb
|
108
99
|
- test/context_test.rb
|
109
100
|
- test/encoding_test.rb
|
data/README
DELETED
@@ -1,127 +0,0 @@
|
|
1
|
-
Rack::Accept
|
2
|
-
============
|
3
|
-
|
4
|
-
Rack::Accept is a suite of tools for Ruby/Rack applications that eases the
|
5
|
-
complexity of building and interpreting the Accept* family of HTTP request
|
6
|
-
headers.
|
7
|
-
|
8
|
-
Some features of the library are:
|
9
|
-
|
10
|
-
* Strict adherence to RFC 2616, specifically section 14
|
11
|
-
* Full support for the Accept, Accept-Charset, Accept-Encoding, and
|
12
|
-
Accept-Language HTTP request headers
|
13
|
-
* May be used as Rack middleware or standalone
|
14
|
-
* A comprehensive test suite that covers many edge cases
|
15
|
-
|
16
|
-
Installation
|
17
|
-
------------
|
18
|
-
|
19
|
-
Using RubyGems:
|
20
|
-
|
21
|
-
$ sudo gem install rack-accept
|
22
|
-
|
23
|
-
From a local copy:
|
24
|
-
|
25
|
-
$ git clone git://github.com/mjijackson/rack-accept.git
|
26
|
-
$ cd rack-accept
|
27
|
-
$ rake package && sudo rake install
|
28
|
-
|
29
|
-
Usage
|
30
|
-
-----
|
31
|
-
|
32
|
-
Rack::Accept implements the Rack middleware interface and may be used with any
|
33
|
-
Rack-based application. Simply insert the Rack::Accept module in your Rack
|
34
|
-
middleware pipeline and access the Rack::Accept::Request object in the
|
35
|
-
"rack-accept.request" environment key, as in the following example.
|
36
|
-
|
37
|
-
require 'rack/accept'
|
38
|
-
|
39
|
-
use Rack::Accept
|
40
|
-
|
41
|
-
app = lambda do |env|
|
42
|
-
accept = env['rack-accept.request']
|
43
|
-
response = Rack::Response.new
|
44
|
-
|
45
|
-
if accept.media_type?('text/html')
|
46
|
-
response['Content-Type'] = 'text/html'
|
47
|
-
response.write "<p>Hello. You accept text/html!</p>"
|
48
|
-
else
|
49
|
-
response['Content-Type'] = 'text/plain'
|
50
|
-
response.write "Apparently you don't accept text/html. Too bad."
|
51
|
-
end
|
52
|
-
|
53
|
-
response.finish
|
54
|
-
end
|
55
|
-
|
56
|
-
run app
|
57
|
-
|
58
|
-
Rack::Accept can also construct automatic 406 responses if you set up the types
|
59
|
-
of media, character sets, encoding, or languages your server is able to serve
|
60
|
-
ahead of time. If you pass a configuration block to your `use` statement it will
|
61
|
-
yield the Rack::Accept::Context object that is used for that invocation.
|
62
|
-
|
63
|
-
require 'rack/accept'
|
64
|
-
|
65
|
-
use(Rack::Accept) do |context|
|
66
|
-
# We only ever serve content in English or Japanese from this site, so if
|
67
|
-
# the user doesn't accept either of these we will respond with a 406.
|
68
|
-
context.languages = %w< en jp >
|
69
|
-
end
|
70
|
-
|
71
|
-
app = ...
|
72
|
-
|
73
|
-
run app
|
74
|
-
|
75
|
-
Note: You should think carefully before using Rack::Accept in this way.
|
76
|
-
Many user agents are careless about the types of Accept headers they send, and
|
77
|
-
depend on apps not being too picky. Instead of automatically sending a 406, you
|
78
|
-
should probably only send one when absolutely necessary.
|
79
|
-
|
80
|
-
Additionally, Rack::Accept may be used outside of a Rack context to provide
|
81
|
-
any Ruby app the ability to construct and interpret Accept headers.
|
82
|
-
|
83
|
-
require 'rack/accept'
|
84
|
-
|
85
|
-
mtype = Rack::Accept::MediaType.new
|
86
|
-
mtype.qvalues = { 'text/html' => 1, 'text/*' => 0.8, '*/*' => 0.5 }
|
87
|
-
mtype.to_s # => "Accept: text/html, text/*;q=0.8, */*;q=0.5"
|
88
|
-
|
89
|
-
cset = Rack::Accept::Charset.new('unicode-1-1, iso-8859-5;q=0.8')
|
90
|
-
cset.best_of(%w< iso-8859-5 unicode-1-1 >) # => "unicode-1-1"
|
91
|
-
cset.accept?('iso-8859-1') # => true
|
92
|
-
|
93
|
-
The very last line in this example may look like a mistake to someone not
|
94
|
-
familiar with the intricacies of the spec, but it's actually correct. It
|
95
|
-
just puts emphasis on the convenience of using this library so you don't
|
96
|
-
have to worry about these kinds of details.
|
97
|
-
|
98
|
-
Four-letter Words
|
99
|
-
-----------------
|
100
|
-
|
101
|
-
Spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
102
|
-
Code: http://github.com/mjijackson/rack-accept
|
103
|
-
Bugs: http://github.com/mjijackson/rack-accept/issues
|
104
|
-
Docs: http://mjijackson.com/rack-accept
|
105
|
-
|
106
|
-
License
|
107
|
-
-------
|
108
|
-
|
109
|
-
Copyright 2010 Michael Jackson
|
110
|
-
|
111
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
112
|
-
of this software and associated documentation files (the "Software"), to deal
|
113
|
-
in the Software without restriction, including without limitation the rights
|
114
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
115
|
-
copies of the Software, and to permit persons to whom the Software is
|
116
|
-
furnished to do so, subject to the following conditions:
|
117
|
-
|
118
|
-
The above copyright notice and this permission notice shall be included in
|
119
|
-
all copies or substantial portions of the Software.
|
120
|
-
|
121
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
122
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
123
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
124
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
125
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
126
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
127
|
-
THE SOFTWARE.
|
data/doc/index.markdown
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
__Rack::Accept__ is a suite of tools for Ruby/Rack applications that eases the
|
2
|
-
complexity of building and interpreting the Accept* family of [HTTP request
|
3
|
-
headers][rfc].
|
4
|
-
|
5
|
-
Some features of the library are:
|
6
|
-
|
7
|
-
* Strict adherence to [RFC 2616][rfc], specifically [section 14][sec14]
|
8
|
-
* Full support for the [Accept][sec14-1], [Accept-Charset][sec14-2],
|
9
|
-
[Accept-Encoding][sec14-3], and [Accept-Language][sec14-4] HTTP request
|
10
|
-
headers
|
11
|
-
* May be used as [Rack][rack] middleware or standalone
|
12
|
-
* A comprehensive [test suite][test] that covers many edge cases
|
13
|
-
|
14
|
-
Installation
|
15
|
-
------------
|
16
|
-
|
17
|
-
Using [RubyGems][rubygems]:
|
18
|
-
|
19
|
-
$ sudo gem install rack-accept
|
20
|
-
|
21
|
-
From a local copy:
|
22
|
-
|
23
|
-
$ git clone git://github.com/mjijackson/rack-accept.git
|
24
|
-
$ cd rack-accept
|
25
|
-
$ rake package && sudo rake install
|
26
|
-
|
27
|
-
[rfc]: http://www.w3.org/Protocols/rfc2616/rfc2616.html
|
28
|
-
[sec14]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
29
|
-
[sec14-1]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
|
30
|
-
[sec14-2]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.2
|
31
|
-
[sec14-3]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
|
32
|
-
[sec14-4]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
|
33
|
-
[rack]: http://rack.rubyforge.org/
|
34
|
-
[test]: http://github.com/mjijackson/rack-accept/tree/master/test/
|
35
|
-
[rubygems]: http://rubygems.org/
|
data/doc/license.markdown
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
License
|
2
|
-
-------
|
3
|
-
|
4
|
-
Copyright 2010 Michael Jackson
|
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.
|
data/doc/usage.markdown
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
Usage
|
2
|
-
-----
|
3
|
-
|
4
|
-
Rack::Accept implements the Rack middleware interface and may be used with any
|
5
|
-
Rack-based application. Simply insert the Rack::Accept module in your Rack
|
6
|
-
middleware pipeline and access the [Request][req] object in the
|
7
|
-
"rack-accept.request" environment key, as in the following example.
|
8
|
-
|
9
|
-
require 'rack/accept'
|
10
|
-
|
11
|
-
use Rack::Accept
|
12
|
-
|
13
|
-
app = lambda do |env|
|
14
|
-
accept = env['rack-accept.request']
|
15
|
-
response = Rack::Response.new
|
16
|
-
|
17
|
-
if accept.media_type?('text/html')
|
18
|
-
response['Content-Type'] = 'text/html'
|
19
|
-
response.write "<p>Hello. You accept text/html!</p>"
|
20
|
-
else
|
21
|
-
response['Content-Type'] = 'text/plain'
|
22
|
-
response.write "Apparently you don't accept text/html. Too bad."
|
23
|
-
end
|
24
|
-
|
25
|
-
response.finish
|
26
|
-
end
|
27
|
-
|
28
|
-
run app
|
29
|
-
|
30
|
-
Rack::Accept can also construct automatic [406][406] responses if you set up
|
31
|
-
the types of media, character sets, encoding, or languages your server is able
|
32
|
-
to serve ahead of time. If you pass a configuration block to your `use`
|
33
|
-
statement it will yield the [Context][ctx] object that is used for that
|
34
|
-
invocation.
|
35
|
-
|
36
|
-
require 'rack/accept'
|
37
|
-
|
38
|
-
use(Rack::Accept) do |context|
|
39
|
-
# We only ever serve content in English or Japanese from this site, so if
|
40
|
-
# the user doesn't accept either of these we will respond with a 406.
|
41
|
-
context.languages = %w< en jp >
|
42
|
-
end
|
43
|
-
|
44
|
-
app = ...
|
45
|
-
|
46
|
-
run app
|
47
|
-
|
48
|
-
__Note:__ You should think carefully before using Rack::Accept in this way.
|
49
|
-
Many user agents are careless about the types of Accept headers they send, and
|
50
|
-
depend on apps not being too picky. Instead of automatically sending a 406, you
|
51
|
-
should probably only send one when absolutely necessary.
|
52
|
-
|
53
|
-
Additionally, Rack::Accept may be used outside of a Rack context to provide
|
54
|
-
any Ruby app the ability to construct and interpret Accept headers.
|
55
|
-
|
56
|
-
require 'rack/accept'
|
57
|
-
|
58
|
-
mtype = Rack::Accept::MediaType.new
|
59
|
-
mtype.qvalues = { 'text/html' => 1, 'text/*' => 0.8, '*/*' => 0.5 }
|
60
|
-
mtype.to_s # => "Accept: text/html, text/*;q=0.8, */*;q=0.5"
|
61
|
-
|
62
|
-
cset = Rack::Accept::Charset.new('unicode-1-1, iso-8859-5;q=0.8')
|
63
|
-
cset.best_of(%w< iso-8859-5 unicode-1-1 >) # => "unicode-1-1"
|
64
|
-
cset.accept?('iso-8859-1') # => true
|
65
|
-
|
66
|
-
The very last line in this example may look like a mistake to someone not
|
67
|
-
familiar with the intricacies of [the spec][sec14-3], but it's actually
|
68
|
-
correct. It just puts emphasis on the convenience of using this library so you
|
69
|
-
don't have to worry about these kinds of details.
|
70
|
-
|
71
|
-
[req]: api/classes/Rack/Accept/Request.html
|
72
|
-
[406]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7
|
73
|
-
[ctx]: api/classes/Rack/Accept/Context.html
|
74
|
-
[sec14-3]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
|