Class: Dap::Filter::FilterGeoIP2LegacyCompat

Inherits:
Object
  • Object
show all
Includes:
Base, GeoIP2Library
Defined in:
lib/dap/filter/geoip2.rb

Overview

Convert GeoIP2 data as closely as possible to the legacy GeoIP data as generated by geo_ip, geo_ip_asn and geo_ip_org

Constant Summary

Constants included from GeoIP2Library

GeoIP2Library::GEOIP2_ASN, GeoIP2Library::GEOIP2_CITY, GeoIP2Library::GEOIP2_DIRS, GeoIP2Library::GEOIP2_ISP

Instance Attribute Summary collapse

Attributes included from Base

#name, #opts

Instance Method Summary collapse

Methods included from GeoIP2Library

find_db, #get_maxmind_data, #remove_empties

Constructor Details

#initialize(args) ⇒ FilterGeoIP2LegacyCompat

Returns a new instance of FilterGeoIP2LegacyCompat.



228
229
230
231
232
# File 'lib/dap/filter/geoip2.rb', line 228

def initialize(args)
  super
  fail "Expected 1 arguments to '#{self.name}' but got #{args.size}" unless args.size == 1
  self.base_field = args.first
end

Instance Attribute Details

#base_fieldObject

Returns the value of attribute base_field.



226
227
228
# File 'lib/dap/filter/geoip2.rb', line 226

def base_field
  @base_field
end

Instance Method Details

#process(doc) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/dap/filter/geoip2.rb', line 234

def process(doc)
  # all of these values we just take directly and rename
  remap = {
    # geoip2 name -> geoip name
    "city.country.iso_code": "country_code",
    "city.country.name": "country_name",
    "city.postal.code": "postal_code",
    "city.location.latitude": "latitude",
    "city.location.longitude": "longitude",
    "city.city.name": "city",
    "city.subdivisions.0.iso_code": "region",
    "city.subdivisions.0.name": "region_name",
    "asn.asn": "asn",
    "isp.asn": "asn",
  }

  ret = {}
  remap.each_pair do |geoip2,geoip|
    geoip2_key = "#{self.base_field}.geoip2.#{geoip2}"
    if doc.include?(geoip2_key)
      ret["#{self.base_field}.#{geoip}"] = doc[geoip2_key]
    end
  end

  # these values all require special handling

  # https://dev.maxmind.com/geoip/geoip2/whats-new-in-geoip2/#Custom_Country_Codes
  # which basically says if traits.is_anonymous_proxy is true, previously the
  # country_code would have had a special value of A1.  Similarly, if
  # traits.is_satellite_provider is true, previously the country_code would
  # have a special value of A2.
  anon_key = "#{self.base_field}.geoip2.city.traits.is_anonymous_proxy"
  if doc.include?(anon_key)
    anon_value = doc[anon_key]
    if anon_value == "true"
      ret["#{self.base_field}.country_code"] = "A1"
    end
  end

  satellite_key = "#{self.base_field}.geoip2.city.traits.is_satellite_provider"
  if doc.include?(satellite_key)
    satellite_value = doc[satellite_key]
    if satellite_value == "true"
      ret["#{self.base_field}.country_code"] = "A1"
    end
  end

  # only set dma_code if location.metro_code was set and not empty or 0
  metro_key = "#{self.base_field}.geoip2.city.location.metro_code}"
  if doc.include?(metro_key)
    metro_value = doc[metro_key]
    if !metro_value.empty? && metro_value != "0"
      ret["#{self.base_field}.dma_code"] = metro_value
    end
  end

  # get the org key from 3 possible fields in decreasing order of preference
  asn_org_key = "#{self.base_field}.geoip2.asn.asn_org"
  isp_asn_org_key = "#{self.base_field}.geoip2.isp.asn_org"
  isp_org_key = "#{self.base_field}.geoip2.isp.asn_org"
  [ isp_org_key, isp_asn_org_key, asn_org_key ].each do |k|
    v = doc[k]
    if v && !v.empty?
      ret["#{self.base_field}.org"] = v
      break
    end
  end

  [ doc.merge(remove_empties(ret)) ]
end