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

#commit, #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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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, geo|
    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 "
      if geo == false
        geoquery << "continent_name, #{name_func}_name "
      else
        geoquery << "latitude, longitude "
      end
      geoquery << "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 "
      if geo == false
        geoquery << "b.continent_name, b.#{name_func}_name "
      else
        geoquery << "a.latitude, a.longitude "
      end
      geoquery << "LIMIT 1;"
      puts "Geo SELECT: #{geoquery}" if @geodebug == true
      begin
        resgeo = @conn.query("#{geoquery}")
        while row = resgeo.fetch_hash do
          if geo == false
            populategeo = instance_variable_get("@#{name_func}_name")
            populategeo << row["#{name_func}_name"].to_s
            instance_variable_set("@#{name_func}_name", populategeo) # Only returns 1 row
            @continent_name = row["continent_name"].to_s
          else
            instance_variable_set("@latitude", row["latitude"].to_s)
            instance_variable_set("@longitude", row["longitude"].to_s)
          end
        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, geo = false) ⇒ Object

Geo set to false by default for normal operation



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/thm/dataservices/geolocation/geolocation.rb', line 90

def geoiplookup(ip, geo=false)
  t = country(ip, geo)
  city(ip, geo)
  unless t == false
    # Check if @longitude / @latitude exists for Reverse Geocoding options
    if instance_variable_defined?("@latitude") and instance_variable_defined?("@longitude")
      res = [@latitude, @longitude]
    else
      res = "(#{@continent_name}) - \n"
      @country_name.each {|n|
        res << "[ #{n} ]" unless n == ""
      }
      @city_name.each {|n|
        res << "[ #{n} ] " unless n == ""
      }
      initialize
    end
  else
    res = "Not Available"
  end
  return res
end