module GeoipRails

Constants

VERSION

Public Instance Methods

area_code(ip) click to toggle source
# File lib/geoip_rails.rb, line 77
def area_code ip
      find_by_ip(ip)["area_code"]
end
city(ip) click to toggle source
# File lib/geoip_rails.rb, line 49
def city ip
      find_by_ip(ip)["city"]
end
country(ip) click to toggle source

def geolocation

ip = request.remote_ip
HTTParty.get("http://freegeoip.net/json/#{ip}")

end

# File lib/geoip_rails.rb, line 41
def country ip
      find_by_ip(ip)["country_name"]
end
country_code(ip) click to toggle source
# File lib/geoip_rails.rb, line 45
def country_code ip
      find_by_ip(ip)["country_code"]
end
find_by_ip(ip) click to toggle source
# File lib/geoip_rails.rb, line 7
def find_by_ip ip
  HTTParty.get("http://freegeoip.net/json/#{ip}")
end
find_by_ip_and_format(ip, format) click to toggle source
# File lib/geoip_rails.rb, line 11
def find_by_ip_and_format ip, format
  ip = ip.downcase
  format = format.downcase
  `wget "http://freegeoip.net/#{format}/#{ip}"`
  response = ""
  File.open("#{ip}").each { |line|  response += line}#.gsub("\n", " ")
  `rm -rf #{ip}`
  return response
end
geolocate(*args) click to toggle source

def print ip, format

puts find_by_ip_and_format ip, format

end

# File lib/geoip_rails.rb, line 25
def geolocate(*args)
  supported_formats = ["json", "xml", "csv"]
      if args.length == 1
    find_by_ip(args[0])
  else
    format = args[1].strip.downcase
    format = "json" if !supported_formats.include?(format)
    find_by_ip_and_format(args[0], format)
  end
end
latitude(ip) click to toggle source
# File lib/geoip_rails.rb, line 65
def latitude ip
      find_by_ip(ip)["latitude"]
end
longitude(ip) click to toggle source
# File lib/geoip_rails.rb, line 69
def longitude ip
      find_by_ip(ip)["longitude"]
end
metro_code(ip) click to toggle source
# File lib/geoip_rails.rb, line 73
def metro_code ip
      find_by_ip(ip)["metro_code"]
end
region_code(ip) click to toggle source
# File lib/geoip_rails.rb, line 53
def region_code ip
      find_by_ip(ip)["region_code"]
end
region_name(ip) click to toggle source
# File lib/geoip_rails.rb, line 57
def region_name ip
      find_by_ip(ip)["region_name"]
end
zipcode(ip) click to toggle source
# File lib/geoip_rails.rb, line 61
def zipcode ip
      find_by_ip(ip)["zipcode"]
end