Permalink
Browse files

Improves language parser

1. If header is malformed, pre-cache empty array instead of parsing it each time
2. Validate locale and type cast its quality during first map iteration to reduce number of operations
3. Split locales by string "," instead of using regex /,/ to increase performance
  • Loading branch information...
1 parent c7cfea5 commit ce19295fd2842498e3b5185d39cd3487d8e20408 @kostyantyn kostyantyn committed Jan 26, 2014
Showing with 14 additions and 10 deletions.
  1. +14 −10 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 ce19295

Please sign in to comment.