Module: NginxTail::RemoteAddr

Defined in:
lib/ntail/remote_addr.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ntail/remote_addr.rb', line 12

def self.included(base) # :nodoc:
  base.class_eval do

    def self.to_host_s(remote_addr)
      Socket::getaddrinfo(remote_addr, nil)[0][2]
    end

    def self.to_country_s(remote_addr)
      record = if defined? GeoIP # ie. if the optional GeoIP gem is installed
        if File.exists?('/usr/share/GeoIP/GeoIP.dat') # ie. if the GeoIP country database is installed
          GeoIP.new('/usr/share/GeoIP/GeoIP.dat').country(remote_addr)
        end
      end
      record ? record[5] : 'N/A'
    end
  
    def self.to_city_s(remote_addr)
      record = if defined? GeoIP # ie. if the optional GeoIP gem is installed
        if File.exists?('/usr/share/GeoIP/GeoIPCity.dat') # ie. if the GeoIP city database is installed
          GeoIP.new('/usr/share/GeoIP/GeoIPCity.dat').city(remote_addr)
        end
      end
      record ? record[7] : 'N/A'
    end
    
    # this ensures the below module methods actually make sense...
    raise "Class #{base.name} should implement instance method 'remote_addr'" unless base.instance_methods.map(&:to_s).include? 'remote_addr'

  end
end

Instance Method Details

#to_city_sObject



51
52
53
# File 'lib/ntail/remote_addr.rb', line 51

def to_city_s()
  self.class.to_city_s(self.remote_addr)
end

#to_country_sObject



47
48
49
# File 'lib/ntail/remote_addr.rb', line 47

def to_country_s()
  self.class.to_country_s(self.remote_addr)
end

#to_host_sObject



43
44
45
# File 'lib/ntail/remote_addr.rb', line 43

def to_host_s()
  self.class.to_host_s(self.remote_addr)
end