Sha256: 39fe464d23ac403a6404dfc2e2cc46a99312404f5a7d02c0b2f5e251ea96288b

Contents?: true

Size: 922 Bytes

Versions: 1

Compression:

Stored size: 922 Bytes

Contents

require 'maxminddb'

module Fluent
  module Geoip
    def country_code_of_ip(ip)
      iso_code = global_geoip.lookup(ip).country.iso_code
      iso_code && iso_code.downcase
    end

    def city_of_ip(ip)
      city = global_geoip.lookup(ip)
      record = {}
      record['dma_code'] = city.location.metro_code
      record['city_name'] = city.city && city.city.name
      record['latitude'] = city.location.latitude.to_s
      record['longitude'] = city.location.longitude.to_s
      record['postal_code'] = city.postal.code
      record['region_name'] = city.subdivisions.first && city.subdivisions.first.iso_code
      record
    end

    def global_geoip
      if Time.now.strftime('%M:%S') == '01:00'
        @@global_geoip = ::MaxMindDB.new(geoip_db_path)
      else
        @@global_geoip ||= ::MaxMindDB.new(geoip_db_path)
      end
    end

    def geoip_db_path
      '/etc/GeoIP2-City.mmdb'
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-viki-0.1.5 lib/fluent/plugin/geoip.rb