class City

Constants

CITY_LIST

Public Instance Methods

find_by_id(plate_number) click to toggle source
# File lib/turkish_cities/city.rb, line 14
def find_by_id(plate_number)
  check_input_range(plate_number, 1, 81)

  CITY_LIST.each do |city|
    return city['name'] if city['plate_number'] == plate_number.to_i
  end
end
find_by_name(city_name, return_type) click to toggle source
# File lib/turkish_cities/city.rb, line 37
def find_by_name(city_name, return_type)
  CITY_LIST.each do |city|
    if convert_chars(city['name']) == convert_chars(city_name)
      return return_type == 'plate_number' ? city['plate_number'] : city['phone_code']
    end
  end
  city_not_found_error(city_name)
end
find_by_phone_code(phone_code) click to toggle source
# File lib/turkish_cities/city.rb, line 22
def find_by_phone_code(phone_code)
  check_input_range(phone_code, 212, 488)
  check_phone_code(phone_code)

  CITY_LIST.each do |city|
    case city['phone_code']
    when Array
      return city['name'] if city['phone_code'].include?(phone_code.to_i)
    when phone_code.to_i
      return city['name']
    end
  end
  city_not_found_error(phone_code)
end
list_cities(options) click to toggle source
# File lib/turkish_cities/city.rb, line 46
def list_cities(options)
  city_list = CITY_LIST

  city_list = filter_metropolitan_municipalities(city_list) if options[:metropolitan_municipality]
  city_list = filter_regions(city_list, options[:region]) if options[:region]

  final_list = prepare_city_list(city_list, options)
  options[:alphabetically_sorted] ? sort_alphabetically(final_list, options) : final_list
end
list_districts(city_name) click to toggle source
# File lib/turkish_cities/city.rb, line 56
def list_districts(city_name)
  CITY_LIST.each do |city|
    return city['districts'] if convert_chars(city['name']) == convert_chars(city_name)
  end
  city_not_found_error(city_name)
end

Private Instance Methods

check_phone_code(input) click to toggle source
# File lib/turkish_cities/city.rb, line 65
def check_phone_code(input)
  return if input.to_i.even?

  raise ArgumentError, I18n.t('errors.not_even_input', input: input)
end
filter_metropolitan_municipalities(city_list) click to toggle source
# File lib/turkish_cities/city.rb, line 71
def filter_metropolitan_municipalities(city_list)
  city_list.map { |city| city unless city['metropolitan_municipality_since'].nil? }.compact
end
filter_regions(city_list, region) click to toggle source
# File lib/turkish_cities/city.rb, line 75
def filter_regions(city_list, region)
  region = convert_chars(region.to_s)
  city_list.map { |city| city if convert_chars(city['region']) == region }.compact
end