Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #18 from socialcast/master

Strip all whitepace from Accept-Language header before parsing
  • Loading branch information...
commit 7738e45ba154015597fca749b7d0257861c14a02 2 parents 3333337 + 890bcb2
@iain authored
Showing with 6 additions and 1 deletion.
  1. +1 −1  lib/http_accept_language/parser.rb
  2. +5 −0 spec/parser_spec.rb
View
2  lib/http_accept_language/parser.rb
@@ -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
5 spec/parser_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.