Skip to content

Commit

Permalink
Add new search methods related with phone code
Browse files Browse the repository at this point in the history
  • Loading branch information
sarslanoglu authored and sarslanoglu committed Apr 17, 2020
1 parent 71b5f0d commit 51ea473
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## master (unreleased)

### New features

* [#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

* [#19](https://github.com/sarslanoglu/turkish_cities/pull/19): Fix yaml file read error while deploying apps to Heroku

## 0.1.2 (2020-04-13)

### New features
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ TurkishCities.list_cities({ alphabetically_sorted: true, metropolitan_municipali

1. Fork it ( https://github.com/sarslanoglu/turkish_cities/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
3. Commit your changes (`git commit -m 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

Expand Down
10 changes: 9 additions & 1 deletion lib/turkish_cities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ def self.find_name_by_plate_number(plate_number)
City.new.find_by_id(plate_number)
end

def self.find_name_by_phone_code(phone_code)
City.new.find_by_phone_code(phone_code)
end

def self.find_plate_number_by_name(city_name)
City.new.find_by_name(city_name)
City.new.find_by_name(city_name, 'plate_number')
end

def self.find_phone_code_by_name(city_name)
City.new.find_by_name(city_name, 'phone_code')
end

def self.list_cities(options = {})
Expand Down
34 changes: 29 additions & 5 deletions lib/turkish_cities/city.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,31 @@ class City
CITY_LIST = YAML.load_file(file_path)

def find_by_id(plate_number)
unless plate_number.to_i.between?(1, 81)
raise RangeError, "Given value [#{plate_number}] is outside bounds of 1 to 81."
end
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

def find_by_name(city_name)
def find_by_phone_code(phone_code)
check_input_range(phone_code, 212, 488)
check_phone_code(phone_code)

CITY_LIST.each do |city|
if city['phone_code'].is_a?(Array)
return city['name'] if city['phone_code'].include?(phone_code.to_i)
elsif city['phone_code'] == phone_code.to_i
return city['name']
end
end
"Couldn't find city name with phone code #{phone_code}"
end

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))
return city['plate_number']
return return_type == 'plate_number' ? city['plate_number'] : city['phone_code']
end
end
"Couldn't find city name with '#{city_name}'"
Expand All @@ -44,6 +56,18 @@ def list_cities(options)

private

def check_input_range(input, min, max)
return if input.to_i.between?(min, max)

raise RangeError, "Given value [#{input}] is outside bounds of #{min} to #{max}."
end

def check_phone_code(input)
return if input.to_i.even?

raise ArgumentError, "Given value [#{input}] must be an even number."
end

def convert_chars(string)
I18n.transliterate(string)
end
Expand Down
37 changes: 36 additions & 1 deletion spec/turkish_cities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
expect(TurkishCities.find_name_by_plate_number('78')).to eq 'Karabük'
end

it 'gives error when range is out of bounds' do
it 'gives error when plate number range is out of bounds' do
expect { TurkishCities.find_name_by_plate_number(nil) }
.to raise_error(RangeError, 'Given value [] is outside bounds of 1 to 81.')
expect { TurkishCities.find_name_by_plate_number(0) }
Expand All @@ -20,6 +20,30 @@
.to raise_error(RangeError, 'Given value [1000] is outside bounds of 1 to 81.')
end

it 'finds city by phone code' do
expect(TurkishCities.find_name_by_phone_code(312)).to eq 'Ankara'
expect(TurkishCities.find_name_by_phone_code(242)).to eq 'Antalya'
expect(TurkishCities.find_name_by_phone_code(222)).to eq 'Eskişehir'
expect(TurkishCities.find_name_by_phone_code(274)).to eq 'Kütahya'
expect(TurkishCities.find_name_by_phone_code(370)).to eq 'Karabük'
expect(TurkishCities.find_name_by_phone_code(360))
.to eq "Couldn't find city name with phone code 360"
end

it 'gives error when phone code range is out of bounds' do
expect { TurkishCities.find_name_by_phone_code(nil) }
.to raise_error(RangeError, 'Given value [] is outside bounds of 212 to 488.')
expect { TurkishCities.find_name_by_phone_code(0) }
.to raise_error(RangeError, 'Given value [0] is outside bounds of 212 to 488.')
expect { TurkishCities.find_name_by_phone_code('1000') }
.to raise_error(RangeError, 'Given value [1000] is outside bounds of 212 to 488.')
end

it 'gives error when phone code is not an even number' do
expect { TurkishCities.find_name_by_phone_code(213) }
.to raise_error(ArgumentError, 'Given value [213] must be an even number.')
end

it 'finds plate number by city' do
expect(TurkishCities.find_plate_number_by_name('Ankara')).to eq 6
expect(TurkishCities.find_plate_number_by_name('Canakkale')).to eq 17
Expand All @@ -31,6 +55,17 @@
.to eq "Couldn't find city name with 'falansehir'"
end

it 'finds phone code by city' do
expect(TurkishCities.find_phone_code_by_name('Ankara')).to eq 312
expect(TurkishCities.find_phone_code_by_name('Canakkale')).to eq 286
expect(TurkishCities.find_phone_code_by_name('Eskişehir')).to eq 222
expect(TurkishCities.find_phone_code_by_name('Isparta')).to eq 246
expect(TurkishCities.find_phone_code_by_name('Istanbul')).to eq [212, 216]
expect(TurkishCities.find_phone_code_by_name('kirsehir')).to eq 386
expect(TurkishCities.find_phone_code_by_name('filansehir'))
.to eq "Couldn't find city name with 'filansehir'"
end

it 'lists cities by plate number' do
city_array = TurkishCities.list_cities
expect(city_array.size).to eq 81
Expand Down

0 comments on commit 51ea473

Please sign in to comment.