Sha256: 69aaa234a2ae892ea38abd5c5dcc68a9e81cc24b0a602e1f5a139091126a5a18

Contents?: true

Size: 913 Bytes

Versions: 1

Compression:

Stored size: 913 Bytes

Contents

# frozen_string_literal: true

require_relative 'world_voyager/version'

module WorldVoyager

  class Error < StandardError; end

  DATABASE_PATH = 'lib/db/worldcities.csv'

  @data = []

  class << self

    def install
      require 'csv'

      CSV.foreach(DATABASE_PATH, headers: true, header_converters: :symbol) do |row|
        @data << row.to_h
      end
    end

    def countries
      install if @data.empty?

      @data.map { |d| d[:country] }.uniq.sort
    end

    def districts(country)
      install if @data.empty?

      country_downcased = country.downcase
      @data.select { |d| d[:country].downcase == country_downcased }.map { |d| d[:district] }.uniq.sort
    end

    def cities(district)
      install if @data.empty?

      district_downcased = district.downcase
      @data.select { |d| d[:district].downcase == district_downcased }.map { |d| d[:city] }.uniq.sort
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
world_voyager-0.1.0 lib/world_voyager.rb