Module: DecomposerHelper

Included in:
City, Distance, District, Elevation, NeighborhoodsParser, Population, Postcode
Defined in:
lib/turkish_cities/helpers/decomposer_helper.rb

Instance Method Summary collapse

Instance Method Details

#check_input_range(input, min, max) ⇒ Object

Raises:

  • (RangeError)


4
5
6
7
8
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 4

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

  raise RangeError, I18n.t('errors.outside_bounds', input: input, min: min, max: max)
end

#cities_not_found_error(first, second) ⇒ Object



18
19
20
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 18

def cities_not_found_error(first, second)
  I18n.t('errors.cities_not_found_error', first: first, second: second)
end

#city_not_found_error(input) ⇒ Object



10
11
12
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 10

def city_not_found_error(input)
  I18n.t('errors.city_not_found_error', input: input)
end

#city_population_not_found_errorObject



14
15
16
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 14

def city_population_not_found_error
  I18n.t('errors.city_population_not_found_error')
end

#convert_chars(string) ⇒ Object



22
23
24
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 22

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

#create_district_list(city_name) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 26

def create_district_list(city_name)
  file_name = create_file_path(city_name)
  file_path = File.join(File.dirname(__FILE__), "../data/districts/#{file_name}.yaml")
  begin
    YAML.load_file(file_path)
  rescue Errno::ENOENT => e
    "Caught the exception: #{e}"
  end
end

#create_file_path(city_name) ⇒ Object



36
37
38
39
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 36

def create_file_path(city_name)
  char_fallbacks = { 'ç' => 'c', 'ğ' => 'g', 'ı' => 'i', 'ö' => 'o', 'ş' => 's', 'ü' => 'u' }
  city_name.downcase(:turkic).encode('ASCII', 'UTF-8', fallback: char_fallbacks)
end

#district_not_found_error(district_input, city_input) ⇒ Object



41
42
43
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 41

def district_not_found_error(district_input, city_input)
  I18n.t('errors.district_not_found_error', district_input: district_input, city_input: city_input)
end

#find_by_between(search_type, city_list, input_one, input_two) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 45

def find_by_between(search_type, city_list, input_one, input_two)
  sorted_inputs = sort_input_numbers(input_one, input_two)

  city_list.map do |city|
    city['name'] if city[search_type] > sorted_inputs[0] && city[search_type] < sorted_inputs[1]
  end.compact
end

#postcode_not_found_error(postcode_input) ⇒ Object



53
54
55
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 53

def postcode_not_found_error(postcode_input)
  I18n.t('errors.postcode_not_found_error', postcode_input: postcode_input)
end

#prepare_city_list(city_list, options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 57

def prepare_city_list(city_list, options)
  if options[:with]
    city_list.map do |city|
      result = {}
      result[:name] = city['name']

      add_plate_number_if_requested(result, city, options)
      add_phone_code_if_requested(result, city, options)
      add_metropolitan_municipality_if_requested(result, city, options)

      result[:region] = city['region'] if options.dig(:with, :region) || options.dig(:with, :all)
      result
    end
  else
    city_list.map do |city|
      city['name']
    end
  end
end

#sort_alphabetically(list, options = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 77

def sort_alphabetically(list, options = nil)
  turkish_alphabet = ' -0123456789abcçdefgğhıijklmnoöprsştuüvyz'

  list.sort_by do |item|
    item_to_sort = if options.nil? || options[:with].nil?
                     item
                   else
                     item[:name]
                   end
    item_to_sort.downcase(:turkic).chars.map { |char| turkish_alphabet.index(char) }
  end
end

#sort_input_numbers(input_one, input_two) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 90

def sort_input_numbers(input_one, input_two)
  if input_one > input_two
    [input_two, input_one]
  else
    [input_one, input_two]
  end
end

#subdistrict_not_found_error(subdistrict_input, district_input, city_input) ⇒ Object



98
99
100
101
# File 'lib/turkish_cities/helpers/decomposer_helper.rb', line 98

def subdistrict_not_found_error(subdistrict_input, district_input, city_input)
  I18n.t('errors.subdistrict_not_found_error', subdistrict_input: subdistrict_input, district_input: district_input,
                                               city_input: city_input)
end