Sha256: 8b9a15e0f96315be191ddebabdbdc9d46c659894f2ee1874e9f218ff32084e3c

Contents?: true

Size: 1.53 KB

Versions: 13

Compression:

Stored size: 1.53 KB

Contents

require 'socket'

begin
  require 'geoip'
rescue LoadError
  # NOOP (optional dependency)
end

module NginxTail
  module RemoteAddr

    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

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

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

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ntail-1.3.1 lib/ntail/remote_addr.rb
ntail-1.3.0 lib/ntail/remote_addr.rb
ntail-1.2.0 lib/ntail/remote_addr.rb
ntail-1.1.4 lib/ntail/remote_addr.rb
ntail-1.1.3 lib/ntail/remote_addr.rb
ntail-1.1.2 lib/ntail/remote_addr.rb
ntail-1.1.1 lib/ntail/remote_addr.rb
ntail-1.1.0 lib/ntail/remote_addr.rb
ntail-1.0.2 lib/ntail/remote_addr.rb
ntail-1.0.0 lib/ntail/remote_addr.rb
ntail-0.2.0 lib/ntail/remote_addr.rb
ntail-0.1.0 lib/ntail/remote_addr.rb
ntail-0.0.12 lib/ntail/remote_addr.rb