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.



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

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] ||= {}

  File.open(DB) do |f|
    FastCSV.raw_parse(f) 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
  end

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