module GeoLocale
Constants
- IP_REGEX
- LCID
- LOCALE
- LOCALHOST_IP
- ROOT
- VERSION
Attributes
config[RW]
Public Class Methods
configure() { |config| ... }
click to toggle source
# File lib/geo_locale.rb, line 15 def self.configure self.config ||= Config.new yield(config) end
country_code(ip: "", request_headers: {})
click to toggle source
# File lib/geo_locale/country_code.rb, line 6 def self.country_code(ip: "", request_headers: {}) # ip = "12.12.12.12" if request_headers.present? cloudflare_country_code = cloudflare_country_code(request_headers) return cloudflare_country_code.downcase if cloudflare_country_code.present? && cloudflare_country_code != "XX" end return GeoLocale.config.localhost_country if ip == GeoLocale::LOCALHOST_IP && GeoLocale.config.localhost_country.present? country_code = GeoLocale.geo_ip_try(ip) if country_code.present? return country_code else country_code = GeoLocale.geokit_try(ip) if country_code.present? return country_code else # final fallback, config default return GeoLocale.config.default_country end end end
locale(ip: "", country_code: nil, lcid: false, request_headers: {})
click to toggle source
# File lib/geo_locale/locale.rb, line 3 def self.locale (ip: "", country_code: nil, lcid: false, request_headers: {}) country_code ||= GeoLocale.country_code(ip: ip, request_headers: request_headers) GeoLocale.get_locale_or_lcid(country_code: country_code, lcid: lcid) end
Private Class Methods
cloudflare_country_code(request_headers)
click to toggle source
# File lib/geo_locale/country_code.rb, line 28 def self.cloudflare_country_code(request_headers) request_headers["HTTP_CF_IPCOUNTRY"] end
figure_out_returnable_lcid(lcid_string: "")
click to toggle source
# File lib/geo_locale/locale.rb, line 77 def self.figure_out_returnable_lcid(lcid_string: "") if lcid_string.present? return lcid_string else return GeoLocale.config.default_lcid end end
figure_out_returnable_locale(locale: "")
click to toggle source
# File lib/geo_locale/locale.rb, line 69 def self.figure_out_returnable_locale(locale: "") if locale.present? return locale else return GeoLocale.config.default_locale end end
geo_ip_try(ip)
click to toggle source
# File lib/geo_locale/country_code.rb, line 32 def self.geo_ip_try(ip) begin return nil unless ip.match(GeoLocale::IP_REGEX).present? code = GeoIP.new("#{GeoLocale::ROOT}/lib/data/GeoIP.dat").country(ip).country_code2.downcase if code == "--" return nil else if GeoLocale.config.overrides["#{code}"].present? return GeoLocale.config.overrides["#{code}"] else return code end end rescue nil end end
geokit_try(ip)
click to toggle source
# File lib/geo_locale/country_code.rb, line 50 def self.geokit_try(ip) return nil if ip.to_s[GeoLocale::IP_REGEX].blank? Timeout::timeout(3) do return Geokit::Geocoders::MultiGeocoder.geocode(ip.to_s).country_code.downcase end rescue return nil end
get_locale_or_lcid(country_code: nil, lcid: false)
click to toggle source
# File lib/geo_locale/locale.rb, line 58 def self.get_locale_or_lcid(country_code: nil, lcid: false) if lcid lcid_string = GeoLocale::LCID[country_code] return figure_out_returnable_lcid(lcid_string: lcid_string) else locale = GeoLocale::LOCALE[country_code] return figure_out_returnable_locale(locale: locale) end end