Skip to content

Instantly share code, notes, and snippets.

@cgallagher
Created November 8, 2012 10:44
Show Gist options
  • Save cgallagher/4038089 to your computer and use it in GitHub Desktop.
Save cgallagher/4038089 to your computer and use it in GitHub Desktop.
Geo Restriction Logic using geocode gem
def check_user_location
# need to intro a whitelist for IP addresses
user_ip = request.ip
unless user_ip.nil?
logger.info("User IP Address is: #{user_ip}")
user_country = request.location.country
logger.info("User Country is: #{user_country}")
# if in production mode and the user country doesnt match one on the whitelist
# then throw them out to a restricted page.
redirect_to restricted_path() unless knock_knock(user_ip, user_country)
end
end
def knock_knock(user_ip, user_country)
country_whitelist = ["Brazil"]
ip_whitelist = ["127.0.0.1"]
country_whitelist.include?(user_country) or ip_whitelist.include?(user_ip) or (["development"]).include?(Rails.env)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment