Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Strip all whitepace from Accept-Language header before parsing #18

Merged
merged 2 commits into from Aug 24, 2012
Jump to file or symbol
Failed to load files and symbols.
+6 −1
Split
@@ -17,7 +17,7 @@ def initialize(header)
# # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
#
def user_preferred_languages
- @user_preferred_languages ||= header.split(/\s*,\s*/).collect do |l|
+ @user_preferred_languages ||= header.gsub(/\s+/, '').split(/,/).collect do |l|
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
l.split(';q=')
end.sort do |x,y|
View
@@ -20,6 +20,11 @@ def parser
parser.user_preferred_languages.should eq []
end
+ it "should properly respect whitespace" do
+ parser.header = 'en-us, en-gb; q=0.8,en;q = 0.6,es-419'
+ parser.user_preferred_languages.should eq %w{en-US es-419 en-GB en}
+ end
+
it "should find first available language" do
parser.preferred_language_from(%w{en en-GB}).should eq "en-GB"
end