Skip to content

Commit

Permalink
Add distance class and distance_between_air method (#64)
Browse files Browse the repository at this point in the history
* Add distance class and distance_between_air method

* Apply PR review changes
  • Loading branch information
sarslanoglu authored Nov 21, 2020
1 parent 2b9b0ba commit 473768b
Show file tree
Hide file tree
Showing 17 changed files with 462 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ AllCops:
- 'turkish_cities.gemspec'
NewCops: enable
Layout/LineLength:
Max: 100
Max: 120
Metrics/AbcSize:
Max: 20
Metrics/BlockLength:
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

## master (unreleased)

### New features

* [#13](https://github.com/sarslanoglu/turkish_cities/issues/13): Add ```distance``` class and ```distance_between``` method for calculating air travel distance and time estimation between cities

### Changes

* [#45](https://github.com/sarslanoglu/turkish_cities/issues/45): Refactor `District` class
* [#50](https://github.com/sarslanoglu/turkish_cities/issues/50): Additional improvements on `District` class methods

* [#45](https://github.com/sarslanoglu/turkish_cities/issues/45): Refactor `District` class

## 0.2.1 (2020-06-29)

### Bug fixes
Expand Down
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
* [With subdistrict info](#with-subdistrict-info)
* [Without subdistrict info](#without-subdistrict-info)
* [Finding city, district and subdistrict name by postcode](#finding-city-district-and-subdistrict-name-by-postcode)
* [Finding travel distance and time estimation between two cities](#finding-travel-distance-and-time-estimation-between-two-cities)
* [By air](#by-air)
* [Data sources](#data-sources)
* [Compatibility](#compatibility)
* [Contributing](#contributing)
* [Changelog](#changelog)
Expand Down Expand Up @@ -242,6 +245,41 @@ TurkishCities.find_by_postcode(34382) # => "Couldn't find any subdistrict wit
TurkishCities.find_by_postcode('100000') # => Given value [100000] is outside bounds of 1010 to 81952.
```

### Finding travel distance and time estimation between two cities

### By air

City names can be given case and turkish character insensitive. Travel method should be lower case. Array with 3 elements will be return. First element is distance between cities in kilometers, second element is estimated travel time and last element is description created by first two element.

```rb
TurkishCities.distance_between('Eskişehir', 'Kastamonu', 'air')
# => [327.74, 62, "Air travel distance between Eskişehir and Kastamonu is 327.74 km. Estimated air travel would take 62 minutes."]
TurkishCities.distance_between('kirsehir', 'Ordu', 'air')
# => [376.89, 67, "Air travel distance between Kırşehir and Ordu is 376.89 km. Estimated air travel would take 67 minutes."]
TurkishCities.distance_between('İzmir', 'Antalya', 'air')
# => [357.18, 65, "Air travel distance between İzmir and Antalya is 357.18 km. Estimated air travel would take 65 minutes."]
TurkishCities.distance_between('istanbul', 'kars', 'air')
# => [1187.94, 120, "Air travel distance between İstanbul and Kars is 1187.94 km. Estimated air travel would take 120 minutes."]
TurkishCities.distance_between('Adana', 'Adıyaman', 'time')
# => "Travel method 'time' is unsupported"
TurkishCities.distance_between('filansa', 'falansa', 'air')
# => "Couldn't find cities combination with 'filansa/falansa'"
```

## Data sources

All information related with cities can be found at:

```
https://tr.wikipedia.org/wiki/{#city_name_here}
```

Districts, subdisctricts, neighborhoods and postcodes can be found at:

```
https://postakodu.ptt.gov.tr/
```

## Compatibility

| Ruby Version | Supported |
Expand All @@ -251,7 +289,7 @@ TurkishCities.find_by_postcode('100000') # => Given value [100000] is outside bo
| 2.5.8 | :white_check_mark: |
| < 2.5.1 | :x: |

- This gem heavily depends on ```:turkic``` case mapping support of Ruby string downcase method. Below Ruby version 2.5.1 some functions will run buggy/false or even won't run at all.
- TurkishCities heavily depends on ```:turkic``` case mapping support of Ruby string downcase method. Below Ruby version 2.5.1 some functions will run buggy/false or even won't run at all.

## Contributing

Expand All @@ -261,6 +299,10 @@ Contributing guidelines are available [here](CONTRIBUTING.md).

Changelog is available [here](CHANGELOG.md).

## Logo

TurkishCities's logo created by [Nebal Çolpan](https://www.behance.net/nebalcolpan)

## Copyright

Copyright (c) 2020 Semih Arslanoglu. See [LICENSE.txt](LICENSE.txt) for
Expand Down
5 changes: 5 additions & 0 deletions lib/turkish_cities.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative '../lib/turkish_cities/city'
require_relative '../lib/turkish_cities/distance'
require_relative '../lib/turkish_cities/district'
require_relative '../lib/turkish_cities/postcode'
require_relative '../lib/turkish_cities/version'
Expand Down Expand Up @@ -30,6 +31,10 @@ def self.list_districts(city_name)
City.new.list_districts(city_name)
end

def self.distance_between(from, to, travel_method)
Distance.new(from, to, travel_method).distance_between
end

def self.list_subdistricts(city_name, district_name)
District.new(city_name, district_name).subdistricts
end
Expand Down
5 changes: 3 additions & 2 deletions lib/turkish_cities/city.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ def find_by_phone_code(phone_code)
check_phone_code(phone_code)

CITY_LIST.each do |city|
if city['phone_code'].is_a?(Array)
case city['phone_code']
when Array
return city['name'] if city['phone_code'].include?(phone_code.to_i)
elsif city['phone_code'] == phone_code.to_i
when phone_code.to_i
return city['name']
end
end
Expand Down
Loading

0 comments on commit 473768b

Please sign in to comment.