Module: PlaceKit

Defined in:
lib/place_kit.rb,
lib/place_kit/version.rb

Constant Summary collapse

DB =
File.join(__dir__, 'db', 'GeoLite2-City-Locations-en.csv')
VERSION =
File.read(File.expand_path('../../VERSION', __dir__)).strip.freeze

Class Method Summary collapse

Class Method Details

.lookup(country, region, city) ⇒ Object

Look up time zone.

Parameters:

  • country (String|Symbol)

    Country ISO code.

  • region (String|Symbol)

    Region ISO (subdivison 1) code.

  • city (String|Symbol)

    City name.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/place_kit.rb', line 16

def self.lookup(country, region, city)
  country = country.to_s.humanize.downcase
  region = region.to_s.humanize.downcase
  city = city.to_s.humanize.downcase

  val = @cache.dig(country, region, city)
  return val if val

  @cache[country] ||= {}
  @cache[country][region] ||= {}

  CSV.foreach(DB) do |entry|
    next unless entry[4]&.downcase == country &&
                entry[6]&.downcase == region &&
                entry[10]&.downcase == city

    time_zone = ActiveSupport::TimeZone.new(entry[12])

    @cache[country][region][city] = time_zone

    return time_zone
  end

  @cache[country][region][city] = nil
end