Class: Rack::AcceptLanguage
- Inherits:
-
Object
- Object
- Rack::AcceptLanguage
- Defined in:
- lib/rack/accept_language.rb
Constant Summary collapse
- QVALUE =
/(?:0(?:\.\d{0,3})?)|(?:1(?:\.0{0,3})?)/
- ALPHABET =
/[a-z]/i
- LANGUAGE_TAG =
/(?:#{ALPHABET}{1,8}(?:-#{ALPHABET}{1,8})*)|\*/o
- LANGUAGE_RANGE =
/(#{LANGUAGE_TAG})(?:;q=(#{QVALUE}))?/o
Instance Method Summary collapse
- #accept_language(env) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app, *args, &block) ⇒ AcceptLanguage
constructor
A new instance of AcceptLanguage.
- #parse(hal) ⇒ Object
Constructor Details
#initialize(app, *args, &block) ⇒ AcceptLanguage
Returns a new instance of AcceptLanguage.
7 8 9 |
# File 'lib/rack/accept_language.rb', line 7 def initialize(app, *args, &block) @app = app end |
Instance Method Details
#accept_language(env) ⇒ Object
15 16 17 |
# File 'lib/rack/accept_language.rb', line 15 def accept_language(env) env.tap{ |e| e['rack.accept_language'] = parse env['HTTP_ACCEPT_LANGUAGE'] } end |
#call(env) ⇒ Object
11 12 13 |
# File 'lib/rack/accept_language.rb', line 11 def call(env) @app.call(accept_language env) end |
#parse(hal) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/rack/accept_language.rb', line 19 def parse(hal) hal.to_s.scan(LANGUAGE_RANGE).inject([]) { |r, (l, q)| r << [l, [1 - (q || 1).to_f, -l.size, r.size]] }.sort_by{ |e| e[1] }.map{ |e| l, c = e[0].split(/-/, 2) c ? "#{l.downcase}-#{c.upcase}" : l.downcase } end |