Permalink
Browse files

Merge pull request #35 from kostyantyn/master

Improves language parser
  • Loading branch information...
2 parents c7cfea5 + ce19295 commit 0432aaff29ece1089bc5ee21cc7b0d41f623c5b9 @DouweM DouweM committed Jan 26, 2014
Showing with 14 additions and 10 deletions.
  1. +14 −10 lib/http_accept_language/parser.rb
View
24 lib/http_accept_language/parser.rb
@@ -17,17 +17,21 @@ def initialize(header)
# # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
#
def user_preferred_languages
- @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|
- raise "Not correctly formatted" unless x.first =~ /^[a-z\-0-9]+$/i
- y.last.to_f <=> x.last.to_f
- end.collect do |l|
- l.first.downcase.gsub(/-[a-z0-9]+$/i) { |x| x.upcase }
+ @user_preferred_languages ||= begin
+ header.to_s.gsub(/\s+/, '').split(',').map do |language|
+ locale, quality = language.split(';q=')
+ raise ArgumentError, 'Not correctly formatted' unless locale =~ /^[a-z\-0-9]+$/i
+
+ locale = locale.downcase.gsub(/-[a-z0-9]+$/i) { |territory| territory.upcase }
+ quality = quality ? quality.to_f : 1.0
+
+ [locale, quality]
+ end.sort do |(_, left), (_, right)|
+ right <=> left
+ end.map(&:first)
+ rescue ArgumentError # Just rescue anything if the browser messed up badly.
+ []
end
- rescue # Just rescue anything if the browser messed up badly.
- []
end
# Sets the user languages preference, overiding the browser

0 comments on commit 0432aaf

Please sign in to comment.