turkish_cities 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'i18n'
4
+ require 'yaml'
5
+
6
+ require_relative '../turkish_cities/helpers/decomposer_helper'
7
+
8
+ class Distance
9
+ include DecomposerHelper
10
+
11
+ file_path = File.join(File.dirname(__FILE__), 'data/cities.yaml')
12
+ CITY_LIST = YAML.load_file(file_path)
13
+ RADIUS_OF_EARTH = 6371 # Earth radius in km is 6371
14
+
15
+ def initialize(from, to, travel_method)
16
+ @from = from
17
+ @to = to
18
+ @travel_method = travel_method
19
+ end
20
+
21
+ def distance_between
22
+ case @travel_method
23
+ when 'land'
24
+ distance_between_land
25
+ when 'sea'
26
+ distance_between_sea
27
+ when 'air'
28
+ distance_between_air
29
+ else
30
+ unsupported_travel_method(@travel_method)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def distance_between_land
37
+ # TODO
38
+ end
39
+
40
+ def distance_between_sea
41
+ # TODO
42
+ end
43
+
44
+ def distance_between_air
45
+ city_array = find_city_attributes
46
+ case city_array
47
+ when String
48
+ city_array
49
+ when Array
50
+ results = []
51
+ results[0] = calculate_flight_distance(city_array).round(2)
52
+ results[1] = distance_time_estimation(results[0]).to_i
53
+ results[2] = description_text('Air', city_array, results)
54
+ results
55
+ end
56
+ end
57
+
58
+ def find_city_attributes
59
+ from_city = to_city = nil
60
+
61
+ CITY_LIST.each do |city|
62
+ from_city = city if convert_chars(@from) == convert_chars(city['name'])
63
+ to_city = city if convert_chars(@to) == convert_chars(city['name'])
64
+ end
65
+
66
+ from_city.nil? || to_city.nil? ? cities_not_found_error(@from, @to) : [from_city, to_city]
67
+ end
68
+
69
+ def degree_to_radian(degree)
70
+ degree * Math::PI / 180
71
+ end
72
+
73
+ # rubocop:disable Metrics/AbcSize
74
+ def calculate_flight_distance(city_array)
75
+ lat_diff = degree_to_radian(city_array[1]['latitude'] - city_array[0]['latitude'])
76
+ lng_diff = degree_to_radian(city_array[1]['longitude'] - city_array[0]['longitude'])
77
+
78
+ from_lat = degree_to_radian(city_array[0]['latitude'])
79
+ to_lat = degree_to_radian(city_array[1]['latitude'])
80
+
81
+ cosines_var = (Math.sin(lat_diff / 2)**2) + ((Math.sin(lng_diff / 2)**2) * (Math.cos(from_lat) * Math.cos(to_lat)))
82
+ result = 2 * Math.atan2(Math.sqrt(cosines_var), Math.sqrt(1 - cosines_var))
83
+ RADIUS_OF_EARTH * result
84
+ end
85
+ # rubocop:enable Metrics/AbcSize
86
+
87
+ # Time estimation for air travel. For planes take-off and landing took nearly same amount.
88
+ # Longer the distance shorter the time. Flying out at high altitude makes less air resistance / more speed
89
+ def distance_time_estimation(air_distance)
90
+ case air_distance
91
+ when 0..400
92
+ (air_distance / 10) + 30
93
+ when 400..1000
94
+ (air_distance / 11.25) + 30
95
+ else
96
+ (air_distance / 12.5) + 25
97
+ end
98
+ end
99
+
100
+ def description_text(travel_method, city_array, result_set)
101
+ "#{travel_method} travel distance between #{city_array[0]['name']} and #{city_array[1]['name']} is " \
102
+ "#{result_set[0]} km. Estimated air travel would take #{result_set[1]} minutes."
103
+ end
104
+ end
@@ -8,62 +8,46 @@ require_relative '../turkish_cities/helpers/decomposer_helper'
8
8
  class District
9
9
  include DecomposerHelper
10
10
 
11
- def list_subdistricts(city_name, district_name)
12
- district_list = create_district_list(city_name)
13
-
14
- if !district_list[district_name].nil?
15
- subdistricts = []
16
- district_list[district_name].each do |subdistrict|
17
- subdistricts << subdistrict[0]
18
- end
19
- sort_alphabetically(subdistricts)
20
- else
21
- district_not_found_error(district_name, city_name)
22
- end
11
+ def initialize(city_name, district_name)
12
+ @city_name = city_name
13
+ @district_name = district_name
14
+ @district_list = create_district_list(city_name)
23
15
  end
24
16
 
25
- def list_neighborhoods(city_name, district_name, subdistrict_name)
26
- district_list = create_district_list(city_name)
17
+ def subdistricts
18
+ return district_not_found_error(@district_name, @city_name) if district_item.nil?
27
19
 
28
- if !district_list[district_name].nil?
29
- neighborhoods = create_neighborhoods(district_list, city_name,
30
- district_name, subdistrict_name)
20
+ sort_alphabetically(district_item.keys)
21
+ end
31
22
 
32
- neighborhoods.is_a?(Array) ? sort_alphabetically(neighborhoods) : neighborhoods
33
- else
34
- district_not_found_error(district_name, city_name)
35
- end
23
+ def neighborhoods(subdistrict_name)
24
+ return district_not_found_error(@district_name, @city_name) if district_item.nil?
25
+
26
+ neighborhoods = create_neighborhoods(subdistrict_name)
27
+ neighborhoods.is_a?(Array) ? sort_alphabetically(neighborhoods) : neighborhoods
36
28
  end
37
29
 
38
30
  private
39
31
 
40
- def create_neighborhoods(district_list, city_name, district_name, subdistrict_name)
41
- if subdistrict_name.nil?
42
- create_neighborhoods_without_subdistrict_name(district_list, district_name)
43
- else
44
- if district_list[district_name][subdistrict_name].nil?
45
- return subdistrict_not_found_error(subdistrict_name, district_name, city_name)
46
- end
32
+ def create_neighborhoods(subdistrict_name)
33
+ return create_neighborhoods_without_subdistrict_name if subdistrict_name.nil?
47
34
 
48
- create_neighborhoods_with_subdistrict_name(district_list, district_name, subdistrict_name)
35
+ if district_item[subdistrict_name].nil?
36
+ return subdistrict_not_found_error(subdistrict_name, @district_name, @city_name)
49
37
  end
38
+
39
+ create_neighborhoods_with_subdistrict_name(subdistrict_name)
50
40
  end
51
41
 
52
- def create_neighborhoods_without_subdistrict_name(district_list, district_name)
53
- neighborhoods = []
54
- district_list[district_name].each do |subdistrict|
55
- subdistrict[1]['neighborhoods'].each do |neighborhood|
56
- neighborhoods << neighborhood
57
- end
58
- end
59
- neighborhoods
42
+ def create_neighborhoods_without_subdistrict_name
43
+ district_item.values.map { |subdistrict| subdistrict['neighborhoods'] }.flatten
60
44
  end
61
45
 
62
- def create_neighborhoods_with_subdistrict_name(district_list, district_name, subdistrict_name)
63
- neighborhoods = []
64
- district_list[district_name][subdistrict_name]['neighborhoods'].each do |neighborhood|
65
- neighborhoods << neighborhood
66
- end
67
- neighborhoods
46
+ def create_neighborhoods_with_subdistrict_name(subdistrict_name)
47
+ district_item[subdistrict_name]['neighborhoods']
48
+ end
49
+
50
+ def district_item
51
+ @district_list[@district_name]
68
52
  end
69
53
  end
@@ -11,6 +11,10 @@ module DecomposerHelper
11
11
  "Couldn't find city name with '#{input}'"
12
12
  end
13
13
 
14
+ def cities_not_found_error(first, second)
15
+ "Couldn't find cities combination with '#{first}/#{second}'"
16
+ end
17
+
14
18
  def convert_chars(string)
15
19
  I18n.transliterate(string.downcase(:turkic))
16
20
  end
@@ -39,7 +43,7 @@ module DecomposerHelper
39
43
  end
40
44
 
41
45
  def prepare_city_list(city_list, options)
42
- if options.dig(:with)
46
+ if options[:with]
43
47
  city_list.map do |city|
44
48
  result = {}
45
49
  result[:name] = city['name']
@@ -61,7 +65,7 @@ module DecomposerHelper
61
65
  def sort_alphabetically(list, options = nil)
62
66
  turkish_alphabet = ' -0123456789abcçdefgğhıijklmnoöprsştuüvyz'
63
67
  list.sort_by do |item|
64
- item_to_sort = if options.nil? || options.dig(:with).nil?
68
+ item_to_sort = if options.nil? || options[:with].nil?
65
69
  item
66
70
  else
67
71
  item[:name]
@@ -93,4 +97,8 @@ module DecomposerHelper
93
97
 
94
98
  result[:metropolitan_municipality_since] = city['metropolitan_municipality_since']
95
99
  end
100
+
101
+ def unsupported_travel_method(input)
102
+ "Travel method '#{input}' is unsupported"
103
+ end
96
104
  end
@@ -17,9 +17,7 @@ class Postcode
17
17
  city_file.each do |district|
18
18
  district_name = district[0]
19
19
  district[1].each do |subdistrict|
20
- if subdistrict[1]['postcode'] == postcode.to_i
21
- return [city_name, district_name, subdistrict[0]]
22
- end
20
+ return [city_name, district_name, subdistrict[0]] if subdistrict[1]['postcode'] == postcode.to_i
23
21
  end
24
22
  end
25
23
  postcode_not_found_error(postcode)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TurkishCities
4
- VERSION = '0.2.1'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
 
12
12
  s.date = '2020-04-13'
13
13
  s.summary = 'List and find Turkish cities'
14
- s.description = 'Simple ruby gem for listing and finding Turkish cities via name, plate number or size'
14
+ s.description = 'List and find Turkish cities via name, district name, post code, plate number. Calculate air travel distance between cities and get realistic estimates of travel time'
15
15
  s.homepage = 'https://github.com/sarslanoglu/turkish_cities'
16
16
  s.license = 'MIT'
17
17
 
@@ -23,8 +23,8 @@ Gem::Specification.new do |s|
23
23
 
24
24
  s.add_development_dependency 'bundler', '~> 2.1.4'
25
25
  s.add_development_dependency 'coveralls', '~> 0.8.23'
26
- s.add_development_dependency 'rspec', '~> 3.9.0'
27
- s.add_development_dependency 'rubocop', '~> 0.86.0'
26
+ s.add_development_dependency 'rspec', '~> 3.10.0'
27
+ s.add_development_dependency 'rubocop', '~> 1.3.1'
28
28
 
29
29
  if s.respond_to?(:metadata)
30
30
  s.metadata['changelog_uri'] = 'https://github.com/sarslanoglu/turkish_cities/blob/master/CHANGELOG.md'
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turkish_cities
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Semih Arslanoglu
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2020-04-13 00:00:00.000000000 Z
@@ -64,30 +64,31 @@ dependencies:
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: 3.9.0
67
+ version: 3.10.0
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: 3.9.0
74
+ version: 3.10.0
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: rubocop
77
77
  requirement: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: 0.86.0
81
+ version: 1.3.1
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
- version: 0.86.0
89
- description: Simple ruby gem for listing and finding Turkish cities via name, plate
90
- number or size
88
+ version: 1.3.1
89
+ description: List and find Turkish cities via name, district name, post code, plate
90
+ number. Calculate air travel distance between cities and get realistic estimates
91
+ of travel time
91
92
  email: arslanoglusemih93@gmail.com
92
93
  executables: []
93
94
  extensions: []
@@ -194,10 +195,17 @@ files:
194
195
  - lib/turkish_cities/data/districts/yozgat.yaml
195
196
  - lib/turkish_cities/data/districts/zonguldak.yaml
196
197
  - lib/turkish_cities/data/neighborhoods_parser.rb
198
+ - lib/turkish_cities/distance.rb
197
199
  - lib/turkish_cities/district.rb
198
200
  - lib/turkish_cities/helpers/decomposer_helper.rb
199
201
  - lib/turkish_cities/postcode.rb
200
202
  - lib/turkish_cities/version.rb
203
+ - public/assets/img/TurkishCities_darkblue-on-yellow_800X800@3x-8.png
204
+ - public/assets/img/TurkishCities_darkblue-on-yellow_914X343@3x-8.png
205
+ - public/assets/img/TurkishCities_darkblue-on-yellow_horizontal_914X343@3x-8.png
206
+ - public/assets/img/TurkishCities_yellow-on-darkblue_800X800@3x-8.png
207
+ - public/assets/img/TurkishCities_yellow-on-darkblue_914X343@3x-8.png
208
+ - public/assets/img/TurkishCities_yellow-on-darkblue_horizontal_914X343@3x-8.png
201
209
  - turkish_cities.gemspec
202
210
  homepage: https://github.com/sarslanoglu/turkish_cities
203
211
  licenses:
@@ -206,7 +214,7 @@ metadata:
206
214
  changelog_uri: https://github.com/sarslanoglu/turkish_cities/blob/master/CHANGELOG.md
207
215
  source_code_uri: https://github.com/sarslanoglu/turkish_cities
208
216
  bug_tracker_uri: https://github.com/sarslanoglu/turkish_cities/issues
209
- post_install_message:
217
+ post_install_message:
210
218
  rdoc_options: []
211
219
  require_paths:
212
220
  - lib
@@ -222,7 +230,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
230
  version: '0'
223
231
  requirements: []
224
232
  rubygems_version: 3.1.2
225
- signing_key:
233
+ signing_key:
226
234
  specification_version: 4
227
235
  summary: List and find Turkish cities
228
236
  test_files: []