Last active
August 29, 2015 14:22
-
-
Save robwilliams/acbf698f5b66a4c08d6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CurrentCountry | |
def initialize(session, env, freegeoip) | |
@session = session | |
@env = env | |
@freegeoip = freegeoip | |
end | |
def fetch | |
( | |
Country.find_by(id: session[:country_id]) || | |
Country.find_by(alpha2: country_code) || | |
Country.default | |
).tap do |c| | |
session[:country_id] = c.id | |
end | |
end | |
private | |
attr_reader :session, :env, :freegeoip | |
def country_code | |
freegeoip.locate( | |
env['action_dispatch.remote_ip'] | |
).country_code | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment