Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Pass only header itself to parser

  • Loading branch information...
commit 4fd147d05853496018fd82034ac27bdddf858d02 1 parent 3977c5b
@iain authored
View
8 lib/http_accept_language/parser.rb
@@ -2,10 +2,10 @@ module HttpAcceptLanguage
class Parser
- attr_reader :env
+ attr_accessor :header
- def initialize(env)
- @env = env
+ def initialize(header)
+ @header = header
end
# Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE.
@@ -17,7 +17,7 @@ def initialize(env)
# # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
#
def user_preferred_languages
- @user_preferred_languages ||= env['HTTP_ACCEPT_LANGUAGE'].split(/\s*,\s*/).collect do |l|
+ @user_preferred_languages ||= header.split(/\s*,\s*/).collect do |l|
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
l.split(';q=')
end.sort do |x,y|
View
2  lib/http_accept_language/rack.rb
@@ -7,7 +7,7 @@ def initialize(app)
def call(env)
def env.http_accept_language
- @http_accept_language ||= Parser.new(self)
+ @http_accept_language ||= Parser.new(self['HTTP_ACCEPT_LANGUAGE'])
end
@app.call(env)
end
View
4 lib/http_accept_language/rails.rb
@@ -5,8 +5,8 @@ module Rails
extend Forwardable
- def http_accept_language_parser
- @http_accept_language_parser ||= Parser.new(env)
+ def http_accept_language
+ @http_accept_language_parser ||= Parser.new(env['HTTP_ACCEPT_LANGUAGE'])
end
def_delegators :http_accept_language_parser,
View
12 spec/parser_spec.rb
@@ -3,11 +3,11 @@
describe HttpAcceptLanguage::Parser do
def parser
- @parser ||= HttpAcceptLanguage::Parser.new('HTTP_ACCEPT_LANGUAGE' => 'en-us,en-gb;q=0.8,en;q=0.6,es-419')
+ @parser ||= HttpAcceptLanguage::Parser.new('en-us,en-gb;q=0.8,en;q=0.6,es-419')
end
it "should return empty array" do
- parser.env['HTTP_ACCEPT_LANGUAGE'] = nil
+ parser.header = nil
parser.user_preferred_languages.should eq []
end
@@ -16,7 +16,7 @@ def parser
end
it "should ignore jambled header" do
- parser.env['HTTP_ACCEPT_LANGUAGE'] = 'odkhjf89fioma098jq .,.,'
+ parser.header = 'odkhjf89fioma098jq .,.,'
parser.user_preferred_languages.should eq []
end
@@ -30,12 +30,12 @@ def parser
end
it "should find first compatible from user preferred" do
- parser.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,de-de'
+ parser.header = 'en-us,de-de'
parser.compatible_language_from(%w{de en}).should eq 'en'
end
it "should accept symbols as available languages" do
- parser.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us'
+ parser.header = 'en-us'
parser.compatible_language_from([:"en-HK"]).should eq :"en-HK"
end
@@ -44,7 +44,7 @@ def parser
end
it "should find most compatible language from user preferred" do
- parser.env['HTTP_ACCEPT_LANGUAGE'] = 'ja,en-gb,en-us,fr-fr'
+ parser.header = 'ja,en-gb,en-us,fr-fr'
parser.language_region_compatible_from(%w{en-UK en-US ja-JP}).should eq "ja-JP"
end
Please sign in to comment.
Something went wrong with that request. Please try again.