https://github.com/mjackson/rack-accept
HTTP Accept* for Ruby/Rack
https://github.com/mjackson/rack-accept
Keywords from Contributors
rack
Last synced: about 6 hours ago
JSON representation
Repository metadata
HTTP Accept* for Ruby/Rack
- Host: GitHub
- URL: https://github.com/mjackson/rack-accept
- Owner: mjackson
- Created: 2010-04-01T21:50:31.000Z (over 15 years ago)
- Default Branch: master
- Last Pushed: 2022-08-25T15:18:13.000Z (over 3 years ago)
- Last Synced: 2025-12-03T13:47:12.865Z (8 days ago)
- Language: Ruby
- Homepage: http://mjackson.github.com/rack-accept
- Size: 285 KB
- Stars: 48
- Watchers: 1
- Forks: 16
- Open Issues: 6
- Releases: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES
README.md
Rack::Accept is a suite of tools for Ruby/Rack applications that eases the
complexity of building and interpreting the Accept* family of HTTP request headers.
Some features of the library are:
- Strict adherence to RFC 2616, specifically section 14
- Full support for the Accept, Accept-Charset,
Accept-Encoding, and Accept-Language HTTP
request headers - May be used as Rack middleware or standalone
- A comprehensive test suite that covers many edge cases
Installation
Using RubyGems:
$ sudo gem install rack-accept
From a local copy:
$ git clone git://github.com/mjijackson/rack-accept.git
$ cd rack-accept
$ rake package && sudo rake install
Usage
Rack::Accept implements the Rack middleware interface and may be used with any
Rack-based application. Simply insert the Rack::Accept module in your Rack
middleware pipeline and access the Rack::Accept::Request object in the
rack-accept.request environment key, as in the following example.
require 'rack/accept'
use Rack::Accept
app = lambda do |env|
accept = env['rack-accept.request']
response = Rack::Response.new
if accept.media_type?('text/html')
response['Content-Type'] = 'text/html'
response.write "<p>Hello. You accept text/html!</p>"
else
response['Content-Type'] = 'text/plain'
response.write "Apparently you don't accept text/html. Too bad."
end
response.finish
end
run app
Rack::Accept can also construct automatic 406 responses if you set up
the types of media, character sets, encoding, or languages your server is able
to serve ahead of time. If you pass a configuration block to your use
statement it will yield the Rack::Accept::Context object that is used for that
invocation.
require 'rack/accept'
use(Rack::Accept) do |context|
# We only ever serve content in English or Japanese from this site, so if
# the user doesn't accept either of these we will respond with a 406.
context.languages = %w< en jp >
end
app = ...
run app
Note: You should think carefully before using Rack::Accept in this way.
Many user agents are careless about the types of Accept headers they send, and
depend on apps not being too picky. Instead of automatically sending a 406, you
should probably only send one when absolutely necessary.
Additionally, Rack::Accept may be used outside of a Rack context to provide
any Ruby app the ability to construct and interpret Accept headers.
require 'rack/accept'
mtype = Rack::Accept::MediaType.new
mtype.qvalues = { 'text/html' => 1, 'text/*' => 0.8, '*/*' => 0.5 }
mtype.to_s # => "Accept: text/html, text/*;q=0.8, */*;q=0.5"
cset = Rack::Accept::Charset.new('unicode-1-1, iso-8859-5;q=0.8')
cset.best_of(%w< iso-8859-5 unicode-1-1 >) # => "unicode-1-1"
cset.accept?('iso-8859-1') # => true
The very last line in this example may look like a mistake to someone not
familiar with the intricacies of the spec, but it's actually
correct. It just puts emphasis on the convenience of using this library so you
don't have to worry about these kinds of details.
Four-letter Words
- Spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
- Code: http://github.com/mjijackson/rack-accept
- Bugs: http://github.com/mjijackson/rack-accept/issues
- Docs: http://mjijackson.github.com/rack-accept
License
Copyright 2012 Michael Jackson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising from,
out of or in connection with the software or the use or other dealings in
the software.
Owner metadata
- Name: Michael Jackson
- Login: mjackson
- Email: michael@jackson.us
- Kind: user
- Description: Founder @remix-run, creator @unpkg. Thriller.
- Website: https://remix.run
- Location: Carlsbad, California
- Twitter: mjackson
- Company: @Shopify
- Icon url: https://avatars.githubusercontent.com/u/92839?u=2727b5ab382f6b2f3be84a257c2dcdc50243d78f&v=4
- Repositories: 191
- Last ynced at: 2025-10-30T11:45:51.310Z
- Profile URL: https://github.com/mjackson
GitHub Events
Total
- Watch event: 1
- Pull request event: 1
- Fork event: 1
Last Year
- Watch event: 1
- Pull request event: 1
- Fork event: 1
Committers metadata
Last synced: 7 days ago
Total Commits: 67
Total Committers: 6
Avg Commits per committer: 11.167
Development Distribution Score (DDS): 0.104
Commits in past year: 0
Committers in past year: 0
Avg Commits per committer in past year: 0.0
Development Distribution Score (DDS) in past year: 0.0
| Name | Commits | |
|---|---|---|
| Michael Jackson | m****n@g****m | 60 |
| Rémi Prévost | r****i@e****m | 2 |
| Joseph HALTER | j****r@t****m | 2 |
| Simon Prévost | s****t@m****m | 1 |
| Matthias Käppler | m****r@g****m | 1 |
| Daniel Doubrovkine (dB.) @dblockdotorg | d****k@d****g | 1 |
Committer domains:
- dblock.org: 1
- mirego.com: 1
- thetalentbox.com: 1
- exomel.com: 1
Issue and Pull Request metadata
Last synced: 12 days ago
Total issues: 7
Total pull requests: 12
Average time to close issues: almost 2 years
Average time to close pull requests: over 1 year
Total issue authors: 7
Total pull request authors: 10
Average comments per issue: 1.71
Average comments per pull request: 1.42
Merged pull request: 6
Bot issues: 0
Bot pull requests: 0
Past year issues: 0
Past year pull requests: 1
Past year average time to close issues: N/A
Past year average time to close pull requests: N/A
Past year issue authors: 0
Past year pull request authors: 1
Past year average comments per issue: 0
Past year average comments per pull request: 0.0
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- mttkay (1)
- ioquatix (1)
- ioanatia (1)
- sshaw (1)
- saraogi (1)
- semaperepelitsa (1)
- kamui (1)
Top Pull Request Authors
- terceiro (2)
- kamui (2)
- remi (2)
- jerrod (1)
- JosephHalter (1)
- simonprev (1)
- mttkay (1)
- dblock (1)
- sideshowcoder (1)
- tutelaris (1)
Top Issue Labels
Top Pull Request Labels
Dependencies
- rake >= 0 development
- rack >= 0.4
Score: 5.780743515792329