Created
July 25, 2009 21:43
-
-
Save SSDany/155055 to your computer and use it in GitHub Desktop.
HTTP Accept parsers for Rack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#moved to http://github.com/SSDany/rack-acceptable/tree/master | |
#some examples: | |
require 'rubygems' | |
require 'rack' | |
require 'rack/acceptable' | |
class Request < Rack::Request | |
include Rack::Acceptable::Media | |
end | |
env = Rack::MockRequest.env_for('/','HTTP_ACCEPT' => 'text/*') | |
request = Request.new(env) | |
request.accept_media?('text/plain') #=> true | |
env = Rack::MockRequest.env_for('/','HTTP_ACCEPT' => 'text/plain;q=0,text/*') | |
request = Request.new(env) | |
request.accept_media?('text/css') #=> true | |
request.accept_media?('text/plain') #=> false | |
request.accept_media?('video/quicktime') #=> false | |
env = Rack::MockRequest.env_for('/','HTTP_ACCEPT' => 'text/x-json;q=0.9,text/*;q=1.0') | |
request = Request.new(env) | |
request.preferred_media_from('text/x-json','text/css') #=> "text/css" | |
env = Rack::MockRequest.env_for('/','HTTP_ACCEPT' => 'text/*,text/x-json') | |
request = Request.new(env) | |
request.preferred_media_from('text/css','text/x-json') #=> "text/x-json" | |
request.preferred_media_from('text/css','text/x-JSON') #=> "text/x-JSON" | |
env = Rack::MockRequest.env_for('/','HTTP_ACCEPT' => 'text/*,text/x-json,text/html;p=whatever') | |
request = Request.new(env) | |
request.preferred_media_from('text/css','text/html') #=> "text/css" | |
request.preferred_media_from('text/x-json','text/html;p=whatever') #=> "text/html;p=whatever" # most specific wins | |
request.preferred_media_from('text/x-json','text/html;p=Whatever') #=> "text/x-json" # parameter values are case-sensitive. | |
# RFC2616 example | |
env = Rack::MockRequest.env_for('/','HTTP_ACCEPT' => 'text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5') | |
request = Request.new(env) | |
accepts = request.acceptable_media | |
Rack::Acceptable::MIMETypes.qualify_mime_type('text/html;level=1',accepts) #=> 1.0 | |
Rack::Acceptable::MIMETypes.qualify_mime_type('text/html',accepts) #=> 0.7 | |
Rack::Acceptable::MIMETypes.qualify_mime_type('text/plain',accepts) #=> 0.3 | |
Rack::Acceptable::MIMETypes.qualify_mime_type('image/jpeg',accepts) #=> 0.5 | |
Rack::Acceptable::MIMETypes.qualify_mime_type('text/html;level=2',accepts) #=> 0.4 | |
Rack::Acceptable::MIMETypes.qualify_mime_type('text/html;level=3',accepts) #=> 0.7 | |
Rack::Acceptable::LanguageTag.extract_language_info('SL-LATN-ROZAJ') #=> ["sl",nil,"Latn",nil,["rozaj"]] | |
langtag = Rack::Acceptable::LanguageTag.parse('SL-LATN-ROZAJ') | |
langtag.variants #=> ["rozaj"] | |
langtag.script #=> "Latn" | |
langtag.has_prefix?('sl-Latn') #=> true | |
langtag.has_prefix?('sl-Latn-ro') #=> false | |
langtag.has_prefix?('sl') #=> true | |
langtag.nicecased #=> "sl-Latn-rozaj" | |
# recomposition | |
langtag.variants = ['nedis'] | |
langtag.privateuse = ['whatever'] | |
langtag.extensions = {'a' => %w(xxx yyy), 'b' => %w(zzz)} | |
langtag.tag #=> "sl-Latn-rozaj" | |
langtag.recompose | |
langtag.tag #=> "sl-Latn-nedis-a-xxx-yyy-b-xxx-x-whatever" | |
# EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment