Class: Dap::Filter::FilterGeoIP2City
- Inherits:
-
Object
- Object
- Dap::Filter::FilterGeoIP2City
- Includes:
- BaseDecoder, GeoIP2Library
- Defined in:
- lib/dap/filter/geoip2.rb
Overview
Add GeoIP2 tags using the MaxMind GeoIP2::City
Constant Summary collapse
- GEOIP2_LANGUAGE =
ENV["GEOIP2_LANGUAGE"] || "en"
- LOCALE_SPECIFIC_NAMES =
%w(city.names continent.names country.names registered_country.names represented_country.names)
- DESIRED_GEOIP2_KEYS =
%w( city.geoname_id continent.code continent.geoname_id country.geoname_id country.iso_code country.is_in_european_union location.accuracy_radius location.latitude location.longitude location.metro_code location.time_zone postal.code registered_country.geoname_id registered_country.iso_code registered_country.is_in_european_union represented_country.geoname_id represented_country.iso_code represented_country.is_in_european_union represented_country.type traits.is_anonymous_proxy traits.is_satellite_provider )
Constants included from GeoIP2Library
GeoIP2Library::GEOIP2_ASN, GeoIP2Library::GEOIP2_CITY, GeoIP2Library::GEOIP2_DIRS, GeoIP2Library::GEOIP2_ISP
Instance Attribute Summary collapse
-
#locale_specific_names ⇒ Object
readonly
Returns the value of attribute locale_specific_names.
Attributes included from Base
Instance Method Summary collapse
- #decode(ip) ⇒ Object
- #defaults ⇒ Object
-
#initialize(args = {}) ⇒ FilterGeoIP2City
constructor
A new instance of FilterGeoIP2City.
Methods included from GeoIP2Library
find_db, #get_maxmind_data, #remove_empties
Methods included from BaseDecoder
Methods included from Base
Constructor Details
#initialize(args = {}) ⇒ FilterGeoIP2City
Returns a new instance of FilterGeoIP2City.
79 80 81 82 |
# File 'lib/dap/filter/geoip2.rb', line 79 def initialize(args={}) @locale_specific_names = LOCALE_SPECIFIC_NAMES.map { |lsn| "#{lsn}.#{GEOIP2_LANGUAGE}" } super end |
Instance Attribute Details
#locale_specific_names ⇒ Object (readonly)
Returns the value of attribute locale_specific_names.
78 79 80 |
# File 'lib/dap/filter/geoip2.rb', line 78 def locale_specific_names @locale_specific_names end |
Instance Method Details
#decode(ip) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/dap/filter/geoip2.rb', line 84 def decode(ip) unless @@geo_city raise "No MaxMind GeoIP2::City data found" end ret = defaults geo_hash = get_maxmind_data(@@geo_city, ip) return unless geo_hash if geo_hash.include?("subdivisions") # handle countries that are divided into various subdivisions. generally 1, sometimes 2 subdivisions = geo_hash["subdivisions"] geo_hash.delete("subdivisions") ret["geoip2.city.subdivisions.length"] = subdivisions.size.to_s subdivisions.each_index do |i| subdivision = subdivisions[i] subdivision.each_pair do |k,v| if %w(geoname_id iso_code).include?(k) ret["geoip2.city.subdivisions.#{i}.#{k}"] = v.to_s elsif k == "names" if v.include?(GEOIP2_LANGUAGE) ret["geoip2.city.subdivisions.#{i}.name"] = subdivision["names"][GEOIP2_LANGUAGE] end end end end end Dap::Utils::Misc.flatten_hash(geo_hash).each_pair do |k,v| if DESIRED_GEOIP2_KEYS.include?(k) # these keys we can just copy directly over ret["geoip2.city.#{k}"] = v elsif @locale_specific_names.include?(k) # these keys we need to pick the locale-specific name and set the key accordingly lsn_renamed = k.gsub(/\.names.#{GEOIP2_LANGUAGE}/, ".name") ret["geoip2.city.#{lsn_renamed}"] = v end end remove_empties(ret) end |
#defaults ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/dap/filter/geoip2.rb', line 126 def defaults() ret = {} default_int_suffixes = %w(geoname_id metro_code) default_bool_suffixes = %w(is_in_european_union is_anonymous_proxy is_satellite_provider) DESIRED_GEOIP2_KEYS.each do |k| suffix = k.split(/\./)[-1] if default_int_suffixes.include?(suffix) ret["geoip2.city.#{k}"] = "0" elsif default_bool_suffixes.include?(suffix) ret["geoip2.city.#{k}"] = "false" else ret["geoip2.city.#{k}"] = "" end end ret end |