lib/rack/geo_locale.rb in rack-geo-locale-0.0.1 vs lib/rack/geo_locale.rb in rack-geo-locale-0.0.2
- old
+ new
@@ -1,47 +1,68 @@
-require 'ostruct'
+require 'geoip'
module Rack
class GeoLocale
def initialize(app)
@app = app
-
- @geoip = GeoIP.new(ENV["GEOIP_DATABASE"])
end
def call(env)
- env["locale.country"] = parse_country(env)
- env["locale.languages"] = parse_languages(env)
+ env["locale.language"], env["locale.country"] = parse_locale(env)
+ if country = parse_country(env)
+ env["locale.country"] = country
+ end
+
@app.call(env)
end
private
def parse_country(env)
remote_addr = env["REMOTE_ADDR"]
return nil unless remote_addr
- result = @geoip.country(remote_addr).country_code2
+ result = geoip.country(remote_addr).country_code2
if result != "--"
result
else
nil
end
end
- def parse_languages(env)
+ def parse_locale(env)
env["HTTP_ACCEPT_LANGUAGE"] ||= ""
language_ranges = env["HTTP_ACCEPT_LANGUAGE"].split(",")
language_ranges.map do |language_range|
language_range += ';q=1.0' unless language_range =~ /;q=\d+\.\d+$/
locale, q = language_range.split(";q=")
- language = locale.strip.split("-").first
+ language, country = locale.strip.split("-")
- {:language => language, :q => q}
- end.sort {|x, y| y[:q] <=> x[:q]}.map{|lr| lr[:language]}
+ {:language => language, :country => country, :q => q}
+ end.sort {|x, y| y[:q] <=> x[:q]}.map{|o| [o[:language], o[:country]]}.first
+ end
+
+ def database?
+ if ENV["GEOIP_DATABASE"]
+ ::File.exist? ENV["GEOIP_DATABASE"]
+ else
+ false
+ end
+ end
+
+ def database
+ ENV["GEOIP_DATABASE"]
+ end
+
+ def geoip
+ if database?
+ GeoIP.new(database)
+ else
+ nil
+ end
end
end
end