Class: Trackdown::Providers::MaxmindProvider

Inherits:
BaseProvider show all
Defined in:
lib/trackdown/providers/maxmind_provider.rb

Overview

Provider that uses MaxMind GeoLite2 database for IP geolocation Requires the maxmind-db gem and a downloaded database file

Defined Under Namespace

Classes: DatabaseError, TimeoutError

Constant Summary collapse

@@reader_pool =
nil
@@pool_mutex =
Mutex.new

Class Method Summary collapse

Methods inherited from BaseProvider

get_country_name, get_emoji_flag

Class Method Details

.available?(request: nil) ⇒ Boolean

Check if MaxMind database is available



28
29
30
31
32
33
# File 'lib/trackdown/providers/maxmind_provider.rb', line 28

def available?(request: nil)
  return false unless maxmind_available?
  return false unless Trackdown.database_exists?

  true
end

.locate(ip, request: nil) ⇒ LocationResult

Locate IP using MaxMind database

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/trackdown/providers/maxmind_provider.rb', line 39

def locate(ip, request: nil)
  raise Trackdown::Error, "MaxMind database not found" unless Trackdown.database_exists?
  raise Trackdown::Error, "maxmind-db gem not installed. Add it to your Gemfile: gem 'maxmind-db'" unless maxmind_available?

  record = fetch_record(ip)
  return LocationResult.new(nil, 'Unknown', 'Unknown', '🏳️') if record.nil?

  country_code = extract_country_code(record)
  country_name = extract_country_name(record)
  city = extract_city(record)
  flag_emoji = get_emoji_flag(country_code)

  LocationResult.new(country_code, country_name, city, flag_emoji)
end