Skip to content

Instantly share code, notes, and snippets.

@robwilliams
Last active August 29, 2015 14:22
Show Gist options
  • Save robwilliams/acbf698f5b66a4c08d6b to your computer and use it in GitHub Desktop.
Save robwilliams/acbf698f5b66a4c08d6b to your computer and use it in GitHub Desktop.
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