Class: Thm::DataServices::Geolocation

Inherits:
Thm::DataServices show all
Defined in:
lib/thm/dataservices/geolocation/geolocation.rb

Instance Attribute Summary collapse

Attributes inherited from Thm::DataServices

#autocommit, #datastore, #dbhost, #dbname, #dbpass, #dbuser, #debug, #mqhost, #mqpass, #mquser, #mqvhost, #queueprefix, #tblname_ippacket, #tblname_tcppacket, #tblname_udppacket

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Thm::DataServices

#dbconnect, #mqclose, #mqconnect, #query

Constructor Details

#initializeGeolocation

Returns a new instance of Geolocation.



19
20
21
22
# File 'lib/thm/dataservices/geolocation/geolocation.rb', line 19

def initialize
  @geodebug = false
  @continent_name, @country_name, @city_name = Array.new, Array.new, Array.new
end

Instance Attribute Details

#geodebug=(value) ⇒ Object (writeonly)

Sets the attribute geodebug

Parameters:

  • value

    the value to set the attribute geodebug to.



17
18
19
# File 'lib/thm/dataservices/geolocation/geolocation.rb', line 17

def geodebug=(value)
  @geodebug = value
end

Class Method Details

.define_component(name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/thm/dataservices/geolocation/geolocation.rb', line 32

def self.define_component(name)
  name_func = name.to_sym
  define_method(name_func) do |ip|
    octets = formatinet(ip, 2)
    geoquery = "SELECT count(*) as num FROM geoipdata_ipv4blocks_#{name_func} a "
    geoquery << "JOIN geoipdata_locations_#{name_func} b ON (a.geoname_id = b.geoname_id) "
    geoquery << "WHERE network LIKE '#{octets}.%';"
    begin
      res = @conn.query("#{geoquery}")
      rowgeocount = res.fetch_hash
      geocount = rowgeocount["num"].to_i
    rescue => e
      pp e
    end
    puts "Geo SELECT COUNT: #{geoquery}: Number: #{geocount}" if @geodebug == true
    if geocount > 0;
      geoquery = "SELECT continent_name, #{name_func}_name FROM geoipdata_ipv4blocks_#{name_func} a "
      geoquery << "JOIN geoipdata_locations_#{name_func} b ON (a.geoname_id = b.geoname_id) "
      geoquery << "WHERE network LIKE '#{octets}.%' GROUP BY b.continent_name, b.#{name_func}_name LIMIT 1;"
      puts "Geo SELECT: #{geoquery}" if @geodebug == true
      begin
        resgeo = @conn.query("#{geoquery}")
        while row = resgeo.fetch_hash do
          populategeo = instance_variable_get("@#{name_func}_name")
          populategeo << row["#{name_func}_name"].to_s
          instance_variable_set("@#{name_func}_name", populategeo)
          @continent_name = row["continent_name"].to_s
        end
      rescue => e
        pp e
      end
    else
      return false
    end
  end
end

Instance Method Details

#formatinet(ip, octets = 2) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/thm/dataservices/geolocation/geolocation.rb', line 24

def formatinet(ip, octets=2)
  if octets == 2
    "#{ip.split(".")[0]}.#{ip.split(".")[1]}"
  elsif octets == 3
    "#{ip.split(".")[0]}.#{ip.split(".")[1]}.#{ip.split(".")[2]}"
  end
end

#geoiplookup(ip) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/thm/dataservices/geolocation/geolocation.rb', line 72

def geoiplookup(ip)
  t = country(ip)
  city(ip)
  unless t == false
    res = "(#{@continent_name}) - \n"
    @country_name.each {|n|
      res << "[ #{n} ]"
    }
    @city_name.each {|n|
      res << "[ #{n} ] "
    }
    initialize
  else
    res = "Not Available"
  end
  return res
end