Skip to content

Commit

Permalink
Add region data and update list_cities method
Browse files Browse the repository at this point in the history
  • Loading branch information
sarslanoglu authored and sarslanoglu committed Apr 18, 2020
1 parent 51ea473 commit fd310d1
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New features

* [#23](https://github.com/sarslanoglu/turkish_cities/pull/23): Add region data to cities.yaml and update ```list_cities``` method to support regions
* [#21](https://github.com/sarslanoglu/turkish_cities/pull/21): Add city finding with phone code and vice versa. ```find_name_by_phone_code``` and ```find_phone_code_by_name``` methods are added

### Bug fixes
Expand Down
27 changes: 16 additions & 11 deletions lib/turkish_cities/city.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,20 @@ def find_by_phone_code(phone_code)

def find_by_name(city_name, return_type)
CITY_LIST.each do |city|
if convert_chars(city['name'].downcase(:turkic)) == convert_chars(city_name.downcase(:turkic))
if convert_chars(city['name']) == convert_chars(city_name)
return return_type == 'plate_number' ? city['plate_number'] : city['phone_code']
end
end
"Couldn't find city name with '#{city_name}'"
end

def list_cities(options)
if options[:metropolitan_municipality]
city_list = CITY_LIST.map do |city|
city unless city['metropolitan_municipality_since'].nil?
end
city_list
else
city_list = CITY_LIST
end
city_list = CITY_LIST

final_city_list = prepare_city_list(city_list.compact)
city_list = filter_metropolitan_municipalities(city_list) if options[:metropolitan_municipality]
city_list = filter_regions(city_list, options[:region]) if options[:region]

final_city_list = prepare_city_list(city_list)
options[:alphabetically_sorted] ? sort_cities(final_city_list) : final_city_list
end

Expand All @@ -69,7 +65,16 @@ def check_phone_code(input)
end

def convert_chars(string)
I18n.transliterate(string)
I18n.transliterate(string.downcase(:turkic))
end

def filter_metropolitan_municipalities(city_list)
city_list.map { |city| city unless city['metropolitan_municipality_since'].nil? }.compact
end

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

def prepare_city_list(city_list)
Expand Down
Loading

0 comments on commit fd310d1

Please sign in to comment.