Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added lookup_ip.

  • Loading branch information...
commit 0549f2badf5e31baddaa60733eca17db9b28dd4c 1 parent 79e088d
Postmodern postmodern authored
Showing with 14 additions and 24 deletions.
  1. +14 −24 lib/geoip.rb
38 lib/geoip.rb
View
@@ -200,12 +200,7 @@ def country(hostname)
return city(hostname)
end
- ip = hostname
- if ip.kind_of?(String) && ip !~ /^[0-9.]*$/
- # Lookup IP address, we were given a name
- ip = IPSocket.getaddress(hostname)
- ip = '0.0.0.0' if ip == '::1'
- end
+ ip = lookup_ip(hostname)
# Convert numeric IP address to an integer
ipnum = iptonum(ip)
@@ -247,12 +242,7 @@ def country(hostname)
# * The timezone name, if known
#
def city(hostname)
- ip = hostname
- if ip.kind_of?(String) && ip !~ /^[0-9.]*$/
- # Lookup IP address, we were given a name
- ip = IPSocket.getaddress(hostname)
- ip = '0.0.0.0' if ip == '::1'
- end
+ ip = lookup_ip(hostname)
# Convert numeric IP address to an integer
ipnum = iptonum(ip)
@@ -279,12 +269,7 @@ def city(hostname)
# Return the ISP name
#
def isp(hostname)
- ip = hostname
- if ip.kind_of?(String) && ip !~ /^[0-9.]*$/
- # Lookup IP address, we were given a name
- ip = IPSocket.getaddress(hostname)
- ip = '0.0.0.0' if ip == '::1'
- end
+ ip = lookup_ip(hostname)
# Convert numeric IP address to an integer
ipnum = iptonum(ip)
@@ -308,12 +293,7 @@ def isp(hostname)
# http://geolite.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz
#
def asn(hostname)
- ip = hostname
- if ip.kind_of?(String) && ip !~ /^[0-9.]*$/
- # Lookup IP address, we were given a name
- ip = IPSocket.getaddress(hostname)
- ip = '0.0.0.0' if ip == '::1'
- end
+ ip = lookup_ip(hostname)
# Convert numeric IP address to an integer
ipnum = iptonum(ip)
@@ -442,6 +422,16 @@ def read_city(pos, hostname = '', ip = '') #:nodoc:
)
end
+ def lookup_ip(ip_or_hostname)
+ if ip.kind_of?(String) && ip !~ /^[0-9.]*$/
+ # Lookup IP address, we were given a name
+ ip = IPSocket.getaddress(hostname)
+ ip = '0.0.0.0' if ip == '::1'
+ end
+
+ return ip
+ end
+
def iptonum(ip) #:nodoc: Convert numeric IP address to integer
if ip.kind_of?(String) &&
ip =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/
Please sign in to comment.
Something went wrong with that request. Please try again.