class GeoLocationUtility
Public Class Methods
new()
click to toggle source
Method to initialize instance variables
# File lib/geo_location_finder.rb, line 7 def initialize @ip_lookup_url= 'http://bot.whatismyipaddress.com' @location_lookup_url='http://extreme-ip-lookup.com/json/' @city_lookup_url='https://maps.googleapis.com/maps/api/geocode/json?latlng=' end
Public Instance Methods
get_city_lookUp_URL()
click to toggle source
Method to get City Lookup URL
# File lib/geo_location_finder.rb, line 62 def get_city_lookUp_URL return @city_lookup_url end
get_geo_location(ip_address)
click to toggle source
Method to get geographical location from IP Address
# File lib/geo_location_finder.rb, line 21 def get_geo_location(ip_address) location = GeoLocationUtility.new #ip_address= location.get_ip_address location_json = open(location.get_location_lookUp_URL+ip_address).read city=location.get_value_from_json(location_json,'city') if(city.eql?("")) latitude=location.get_value_from_json(location_json,'lat') longitude=location.get_value_from_json(location_json,'lon') google_address_json=open(location.get_city_lookUp_URL+latitude+longitude+'&sensor=true', {ssl_verify_mode: 0}).read address=get_value_from_json_array(google_address_json,'results','formatted_address') else country=location.get_value_from_json(location_json,'country') region=location.get_value_from_json(location_json,'region') address=city+', '+region+', '+country end return address end
get_ip_address()
click to toggle source
Method to get public IP address of Machine
# File lib/geo_location_finder.rb, line 14 def get_ip_address get_public_ip = GeoLocationUtility.new public_ip=open(get_public_ip.get_ip_lookUp_URL).read return public_ip end
get_ip_lookUp_URL()
click to toggle source
Method to get Ip Lookup URL
# File lib/geo_location_finder.rb, line 42 def get_ip_lookUp_URL return @ip_lookup_url end
get_location_lookUp_URL()
click to toggle source
Method to get Geo-Location Lookup URL
# File lib/geo_location_finder.rb, line 52 def get_location_lookUp_URL return @location_lookup_url end
get_value_from_json(json, key)
click to toggle source
Method to get value from given key from given JSON string
# File lib/geo_location_finder.rb, line 72 def get_value_from_json (json, key) data_hash = JSON.parse(json) value=data_hash[key] return value end
get_value_from_json_array(json, array_name, key)
click to toggle source
Method to get value from given key from given JSON array
# File lib/geo_location_finder.rb, line 79 def get_value_from_json_array (json, array_name, key) data_hash = JSON.parse(json) value=data_hash[array_name][0][key] return value end
set_city_lookUp_URL(city_lookup_url)
click to toggle source
Method to set City Lookup URL
# File lib/geo_location_finder.rb, line 67 def set_city_lookUp_URL(city_lookup_url) @city_lookup_url=city_lookup_url end
set_ip_lookUp_URL(ip_lookup_url)
click to toggle source
Method to set Ip Lookup URL
# File lib/geo_location_finder.rb, line 47 def set_ip_lookUp_URL(ip_lookup_url) @ip_lookup_url=ip_lookup_url end
set_location_lookUp_URL(location_lookup_url)
click to toggle source
Method to set Geo-Location Lookup URL
# File lib/geo_location_finder.rb, line 57 def set_location_lookUp_URL(location_lookup_url) @location_lookup_url=location_lookup_url end