diff --git a/CHANGELOG.md b/CHANGELOG.md index 0732e8c..db68ca7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/turkish_cities/city.rb b/lib/turkish_cities/city.rb index 0c7d8ab..526f916 100644 --- a/lib/turkish_cities/city.rb +++ b/lib/turkish_cities/city.rb @@ -33,7 +33,7 @@ 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 @@ -41,16 +41,12 @@ def find_by_name(city_name, return_type) 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 @@ -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) diff --git a/lib/turkish_cities/data/cities.yaml b/lib/turkish_cities/data/cities.yaml index d4b4bca..d5e7b4d 100644 --- a/lib/turkish_cities/data/cities.yaml +++ b/lib/turkish_cities/data/cities.yaml @@ -3,403 +3,484 @@ name: 'Adana' phone_code: 322 metropolitan_municipality_since: 1986 + region: 'Akdeniz' - plate_number: 2 name: 'Adıyaman' phone_code: 416 metropolitan_municipality_since: null + region: 'Güneydoğu Anadolu' - plate_number: 3 name: 'Afyon' phone_code: 272 metropolitan_municipality_since: null + region: 'Ege' - plate_number: 4 name: 'Ağrı' phone_code: 472 metropolitan_municipality_since: null + region: 'Doğu Anadolu' - plate_number: 5 name: 'Amasya' phone_code: 358 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 6 name: 'Ankara' phone_code: 312 metropolitan_municipality_since: 1984 + region: 'İç Anadolu' - plate_number: 7 name: 'Antalya' phone_code: 242 metropolitan_municipality_since: 1993 + region: 'Akdeniz' - plate_number: 8 name: 'Artvin' phone_code: 466 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 9 name: 'Aydın' phone_code: 256 metropolitan_municipality_since: 2012 + region: 'Ege' - plate_number: 10 name: 'Balıkesir' phone_code: 266 metropolitan_municipality_since: 2012 + region: 'Marmara' - plate_number: 11 name: 'Bilecik' phone_code: 228 metropolitan_municipality_since: null + region: 'Marmara' - plate_number: 12 name: 'Bingöl' phone_code: 426 metropolitan_municipality_since: null + region: 'Doğu Anadolu' - plate_number: 13 name: 'Bitlis' phone_code: 434 metropolitan_municipality_since: null + region: 'Doğu Anadolu' - plate_number: 14 name: 'Bolu' phone_code: 374 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 15 name: 'Burdur' phone_code: 248 metropolitan_municipality_since: null + region: 'Akdeniz' - plate_number: 16 name: 'Bursa' phone_code: 224 metropolitan_municipality_since: 1987 + region: 'Marmara' - plate_number: 17 name: 'Çanakkale' phone_code: 286 metropolitan_municipality_since: null + region: 'Marmara' - plate_number: 18 name: 'Çankırı' phone_code: 376 metropolitan_municipality_since: null + region: 'İç Anadolu' - plate_number: 19 name: 'Çorum' phone_code: 364 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 20 name: 'Denizli' phone_code: 258 metropolitan_municipality_since: 2012 + region: 'Ege' - plate_number: 21 name: 'Diyarbakır' phone_code: 412 metropolitan_municipality_since: 1993 + region: 'Güneydoğu Anadolu' - plate_number: 22 name: 'Edirne' phone_code: 284 metropolitan_municipality_since: null + region: 'Marmara' - plate_number: 23 name: 'Elazığ' phone_code: 424 metropolitan_municipality_since: null + region: 'Doğu Anadolu' - plate_number: 24 name: 'Erzincan' phone_code: 446 metropolitan_municipality_since: null + region: 'Doğu Anadolu' - plate_number: 25 name: 'Erzurum' phone_code: 442 metropolitan_municipality_since: 1993 + region: 'Doğu Anadolu' - plate_number: 26 name: 'Eskişehir' phone_code: 222 metropolitan_municipality_since: 1993 + region: 'İç Anadolu' - plate_number: 27 name: 'Gaziantep' phone_code: 342 metropolitan_municipality_since: 1987 + region: 'Güneydoğu Anadolu' - plate_number: 28 name: 'Giresun' phone_code: 454 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 29 name: 'Gümüşhane' phone_code: 456 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 30 name: 'Hakkari' phone_code: 438 metropolitan_municipality_since: null + region: 'Doğu Anadolu' - plate_number: 31 name: 'Hatay' phone_code: 326 metropolitan_municipality_since: 2012 + region: 'Akdeniz' - plate_number: 32 name: 'Isparta' phone_code: 246 metropolitan_municipality_since: null + region: 'Akdeniz' - plate_number: 33 name: 'Mersin' phone_code: 324 metropolitan_municipality_since: 1993 + region: 'Akdeniz' - plate_number: 34 name: 'İstanbul' phone_code: [212,216] metropolitan_municipality_since: 1984 + region: 'Marmara' - plate_number: 35 name: 'İzmir' phone_code: 232 metropolitan_municipality_since: 1984 + region: 'Ege' - plate_number: 36 name: 'Kars' phone_code: 474 metropolitan_municipality_since: null + region: 'Doğu Anadolu' - plate_number: 37 name: 'Kastamonu' phone_code: 366 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 38 name: 'Kayseri' phone_code: 352 metropolitan_municipality_since: 1988 + region: 'İç Anadolu' - plate_number: 39 name: 'Kırklareli' phone_code: 288 metropolitan_municipality_since: null + region: 'Marmara' - plate_number: 40 name: 'Kırşehir' phone_code: 386 metropolitan_municipality_since: null + region: 'İç Anadolu' - plate_number: 41 name: 'Kocaeli' phone_code: 262 metropolitan_municipality_since: 1993 + region: 'Marmara' - plate_number: 42 name: 'Konya' phone_code: 332 metropolitan_municipality_since: 1987 + region: 'İç Anadolu' - plate_number: 43 name: 'Kütahya' phone_code: 274 metropolitan_municipality_since: null + region: 'Ege' - plate_number: 44 name: 'Malatya' phone_code: 422 metropolitan_municipality_since: 2012 + region: 'Doğu Anadolu' - plate_number: 45 name: 'Manisa' phone_code: 236 metropolitan_municipality_since: 2012 + region: 'Ege' - plate_number: 46 name: 'Kahramanmaraş' phone_code: 344 metropolitan_municipality_since: 2012 + region: 'Akdeniz' - plate_number: 47 name: 'Mardin' phone_code: 482 metropolitan_municipality_since: 2012 + region: 'Güneydoğu Anadolu' - plate_number: 48 name: 'Muğla' phone_code: 252 metropolitan_municipality_since: 2012 + region: 'Ege' - plate_number: 49 name: 'Muş' phone_code: 436 metropolitan_municipality_since: null + region: 'Doğu Anadolu' - plate_number: 50 name: 'Nevşehir' phone_code: 384 metropolitan_municipality_since: null + region: 'İç Anadolu' - plate_number: 51 name: 'Niğde' phone_code: 388 metropolitan_municipality_since: null + region: 'İç Anadolu' - plate_number: 52 name: 'Ordu' phone_code: 452 metropolitan_municipality_since: 2012 + region: 'Karadeniz' - plate_number: 53 name: 'Rize' phone_code: 464 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 54 name: 'Sakarya' phone_code: 264 metropolitan_municipality_since: 2000 + region: 'Marmara' - plate_number: 55 name: 'Samsun' phone_code: 362 metropolitan_municipality_since: 1993 + region: 'Karadeniz' - plate_number: 56 name: 'Siirt' phone_code: 484 metropolitan_municipality_since: null + region: 'Güneydoğu Anadolu' - plate_number: 57 name: 'Sinop' phone_code: 368 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 58 name: 'Sivas' phone_code: 346 metropolitan_municipality_since: null + region: 'İç Anadolu' - plate_number: 59 name: 'Tekirdağ' phone_code: 282 metropolitan_municipality_since: 2012 + region: 'Marmara' - plate_number: 60 name: 'Tokat' phone_code: 356 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 61 name: 'Trabzon' phone_code: 462 metropolitan_municipality_since: 2012 + region: 'Karadeniz' - plate_number: 62 name: 'Tunceli' phone_code: 428 metropolitan_municipality_since: null + region: 'Doğu Anadolu' - plate_number: 63 name: 'Şanlıurfa' phone_code: 414 metropolitan_municipality_since: 2012 + region: 'Güneydoğu Anadolu' - plate_number: 64 name: 'Uşak' phone_code: 276 metropolitan_municipality_since: null + region: 'Ege' - plate_number: 65 name: 'Van' phone_code: 432 metropolitan_municipality_since: 2012 + region: 'Doğu Anadolu' - plate_number: 66 name: 'Yozgat' phone_code: 354 metropolitan_municipality_since: null + region: 'İç Anadolu' - plate_number: 67 name: 'Zonguldak' phone_code: 372 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 68 name: 'Aksaray' phone_code: 382 metropolitan_municipality_since: null + region: 'İç Anadolu' - plate_number: 69 name: 'Bayburt' phone_code: 458 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 70 name: 'Karaman' phone_code: 338 metropolitan_municipality_since: null + region: 'İç Anadolu' - plate_number: 71 name: 'Kırıkkale' phone_code: 318 metropolitan_municipality_since: null + region: 'İç Anadolu' - plate_number: 72 name: 'Batman' phone_code: 488 metropolitan_municipality_since: null + region: 'Güneydoğu Anadolu' - plate_number: 73 name: 'Şırnak' phone_code: 486 metropolitan_municipality_since: null + region: 'Güneydoğu Anadolu' - plate_number: 74 name: 'Bartın' phone_code: 378 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 75 name: 'Ardahan' phone_code: 478 metropolitan_municipality_since: null + region: 'Doğu Anadolu' - plate_number: 76 name: 'Iğdır' phone_code: 476 metropolitan_municipality_since: null + region: 'Doğu Anadolu' - plate_number: 77 name: 'Yalova' phone_code: 226 metropolitan_municipality_since: null + region: 'Marmara' - plate_number: 78 name: 'Karabük' phone_code: 370 metropolitan_municipality_since: null + region: 'Karadeniz' - plate_number: 79 name: 'Kilis' phone_code: 348 metropolitan_municipality_since: null + region: 'Güneydoğu Anadolu' - plate_number: 80 name: 'Osmaniye' phone_code: 328 metropolitan_municipality_since: null + region: 'Akdeniz' - plate_number: 81 name: 'Düzce' phone_code: 380 - metropolitan_municipality_since: null \ No newline at end of file + metropolitan_municipality_since: null + region: 'Karadeniz' \ No newline at end of file diff --git a/spec/turkish_cities_spec.rb b/spec/turkish_cities_spec.rb index f65d60e..0efdd46 100644 --- a/spec/turkish_cities_spec.rb +++ b/spec/turkish_cities_spec.rb @@ -84,6 +84,19 @@ expect(city_array[14]).to eq 'İzmir' end + it 'lists cities by region' do + city_array = TurkishCities.list_cities({ region: 'Karadeniz' }) + expect(city_array.size).to eq 18 + expect(city_array[17]).to eq 'Düzce' + end + + it 'lists only metropolitan municipality cities by region' do + city_array = TurkishCities.list_cities({ region: 'Karadeniz', + metropolitan_municipality: true }) + expect(city_array.size).to eq 3 + expect(city_array).to eq %w[Ordu Samsun Trabzon] + end + it 'lists only metropolitan municipality cities by alphabetical order' do city_array = TurkishCities.list_cities({ alphabetically_sorted: true, metropolitan_municipality: true }) @@ -91,6 +104,14 @@ expect(city_array[14]).to eq 'Kahramanmaraş' end + it 'lists cities with all filters' do + city_array = TurkishCities.list_cities({ alphabetically_sorted: true, + region: 'Marmara', + metropolitan_municipality: true }) + expect(city_array.size).to eq 6 + expect(city_array).to eq %w[Balıkesir Bursa İstanbul Kocaeli Sakarya Tekirdağ] + end + it 'ignores wrong parameters' do city_array = TurkishCities.list_cities({ falanicly_sorted: true }) expect(city_array.size).to eq 81