Class: Trackdown::IpLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/trackdown/ip_locator.rb

Defined Under Namespace

Classes: DatabaseError, TimeoutError

Class Method Summary collapse

Class Method Details

.locate(ip) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/trackdown/ip_locator.rb', line 14

def locate(ip)
  IpValidator.validate!(ip)

  if Trackdown.configuration.reject_private_ips? && IpValidator.private_ip?(ip)
    raise IpValidator::InvalidIpError, "Private IP addresses are not allowed"
  end

  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