Skip to content

SSDany/rack-acceptable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DESCRIPTION:

Provides definitive support for Accept-* headers, strictly compliant with RFC 2616. Includes negotiation algorithms for Media-Types, Charsets and Content-Codings. Also realizes perfectionistic LanguageTags support with basic/extended filtering (RFC 5646, RFC 4647).

FEATURES:

  • Accept-* headers support, strictly compliant with RFC 2616

  • Media-Type negotiation

  • Charset negotiation

  • Content-coding negotiation

  • LanguageTags (RFC 5646, RFC 4647)

EXAMPLES:

Helpers:

env = Rack::MockRequest.env_for('/',
  'HTTP_ACCEPT'           => 'text/*;q=0.8,text/x-json;q=0.8,text/html;p=whatever,image/png;q=0.2',
  'HTTP_ACCEPT_LANGUAGE'  => 'de-de,sl-nedis',
  'HTTP_ACCEPT_CHARSET'   => 'utf-8;q=0.9,iso-8859-5;q=0.8'
  )

request = Rack::Acceptable::Request.new(env)

Media:

request.accept_media?('text/css') #=> true
request.accept_media?('text/plain') #=> true
request.accept_media?('image/jpeg') #=> false
request.accept_media?('image/png') #=> true
request.accept_media?('video/quicktime') #=> false

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"
request.preferred_media_from('text/x-json','text/html;p=Whatever') #=> "text/x-json" #parameter values are case-sensitive.

Languages:

request.accept_language?("de-de") #=> true
request.accept_language?("de-DE") #=> true
request.accept_language?("de-Latn-de") #=> true
request.accept_language?("de-Latf-DE") #=> true
request.accept_language?("de-DE-x-goethe") #=> true
request.accept_language?("de-Latn-DE-1996") #=> true
request.accept_language?("de-Deva-DE") #=> true

request.accept_language?("de") #=> false
request.accept_language?("de-x-DE") #=> false
request.accept_language?("de-Deva") #=> false

request.accept_language?("sl-nedis") #=> true
request.accept_language?("sl-Latn-nedis") #=> true
request.accept_language?("sl-rozaj") #=> false

Charsets:

request.accept_charset?("utf-8") #=> # true
request.accept_charset?("iso-8859-1") #=> # true

request.preferred_charset_from("utf-8","iso-8859-5") #=> # "utf-8"
request.preferred_charset_from("utf-8","iso-8859-1") #=> # "iso-8859-1"

LanguageTags:

langtag = Rack::Acceptable::LanguageTag.parse('SL-LATN-ROZAJ')

langtag.variants #=> ["rozaj"]
langtag.script #=> "Latn"

# basic filtering (RFC 4647)
langtag.has_prefix?('sl-Latn') #=> true
langtag.matched_by_basic_range?('sl-Latn') #=> true
langtag.has_prefix?('sl-Latn-ro') #=> false
langtag.has_prefix?('sl') #=> true

# extended filtering (RFC 4647)
langtag.matched_by_extended_range?('*') #=> true
langtag.matched_by_extended_range?('sl-*') #=> true
langtag.matched_by_extended_range?('*-Latn') #=> true
langtag.matched_by_extended_range?('sl-rozaj') #=> true
langtag.matched_by_extended_range?('sl-nedis') #=> false

INSTALLATION:

rack-acceptable is available via rubygems:

$gem in acceptable

REQUIREMENTS:

rack (>=1.0.0)

LICENSE:

(The MIT License)

Copyright © 2010

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.

About

HTTP Accept parsers for Rack

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages