Skip to content

Instantly share code, notes, and snippets.

@thoughtpunch
Created November 14, 2011 15:55
Show Gist options
  • Save thoughtpunch/1364251 to your computer and use it in GitHub Desktop.
Save thoughtpunch/1364251 to your computer and use it in GitHub Desktop.
How I know I've been up too late coding....
require 'net/http'
require 'json'
require 'hashie'
class Geolocate
def self.ip(ip)
#REGEX FOR VALID IP ADDRESSES
if /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/.match(ip).nil?
raise "Not a valid IP address (e.x. '123.456.78.9')"
#OH GOD OH GOD OH GOD OH GOD OH GOD OH GOD OH GOD OH GOD I'M SO SCARED
elsif ip == "127.0.0.1"
puts "ITS COMING FROM INSIDE THE HOUSE!"
return Hashie::Mash.new(:city= "Your City",
:ip="127.0.0.1", :latitude="45",:longitude="-75",:region_name="Your House",
:region_code => "XX",:zipcode="12345", :location => 'YOUR ATTIC',
:person => 'ANALRAPIST', :intent => 'deathgasm')
#RESULTS
else
uri = "http://freegeoip.net/json/#{ip}"
resp = Net::HTTP.get_response(URI.parse(uri))
if resp.code == "404"
raise "No results for IP: #{ip}"
elsif resp.code == "200"
return Hashie::Mash.new(JSON.parse(resp.body))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment