Class: GeoIPMulti

Inherits:
GeoIP
  • Object
show all
Defined in:
lib/geoip_multi.rb

Instance Method Summary collapse

Methods inherited from GeoIP

#filename, #netmask, #range_by_ip

Constructor Details

#initialize(*filenames) ⇒ GeoIPMulti

Returns a new instance of GeoIPMulti.



101
102
103
104
105
# File 'lib/geoip_multi.rb', line 101

def initialize (*filenames)
  @geoip_databases=[]
  filenames=Dir.glob(File.join(File.expand_path($0)[/(.*\/)/],'*.dat')) if filenames.length == 0
  add_database(filenames)
end

Instance Method Details

#add_database(*filenames) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/geoip_multi.rb', line 107

def add_database (*filenames)
  filenames.flatten!
  filenames.each do |filename|
    if File.directory?(filename)
      Dir.glob(File.join(filename,'*.dat')) {|filename| @geoip_databases << GeoIP.new(filename)}
    else
      GeoIP.new(filename)
      @geoip_databases << GeoIP.new(filename)
    end
  end
end

#lookup(ip) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/geoip_multi.rb', line 119

def lookup(ip)
  results ={}
  @geoip_databases.each do |current_database|
    case current_database.database_type
      #IPv4 addresses expected, IPv6 not currently supported, maybe in the future...
      when GEOIP_COUNTRY_EDITION
        results.merge!(current_database.country(ip).to_hash)
      when GEOIP_CITY_EDITION_REV1
        results.merge!(current_database.city(ip).to_hash)
        results.merge!(:netmask=>current_database.netmask(ip))
        iprange = current_database.range_by_ip(ip)
        results.merge!(:first_ip => iprange[0],:last_ip => iprange[1])
      when GEOIP_ISP_EDITION
        results.merge!(:isp=>current_database.isp(ip))
      when GEOIP_ORG_EDITION
        results.merge!(current_database.filename[/([^\/]+)$/][/(?:GeoIP)?([^.]+)/,1].downcase.to_sym => current_database.isp(ip))
      when GEOIP_ASNUM_EDITION
        results.merge!(current_database.asn(ip).to_hash)
      else
        puts "Unknown or Unsupported GeoIP database type:"
        puts "filename:#{current_database.filename} => database_type:#{current_database.database_type}"
    end
  end
  results
end