Class: Freegeoip::Lookup

Inherits:
Object
  • Object
show all
Defined in:
app/models/freegeoip/lookup.rb

Class Method Summary collapse

Class Method Details

.hostname_or_ip(string, locale = "en") ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/freegeoip/lookup.rb', line 9

def self.hostname_or_ip(string, locale = "en")
  ip = resolve string.to_s

  return nil unless ip

  result = lookup(ip)

  return nil if result.empty?

  {
    ip: ip,
    country_code: result.dig("country", "iso_code"),
    country_name: result.dig("country", "names", locale),
    region_code: result.dig("subdivisions", 0, "iso_code"),
    region_name: result.dig("subdivisions", 0, "names", locale),
    city: result.dig("city", "names", locale),
    zip_code: result.dig("postal", "code"),
    time_zone: result.dig("location", "time_zone"),
    latitude: result.dig("location", "latitude"),
    longitude: result.dig("location", "longitude"),
    metro_code: (result.dig("location", "metro_code") || 0),
  }
end