Module: GetCountryByPhone

Includes:
CountriesPriorities, Numer
Defined in:
lib/ptc/get_country_by_phone.rb

Constant Summary

Constants included from Numer

Numer::DIGIT

Instance Method Summary collapse

Methods included from CountriesPriorities

#cps, #top_priority_country_by_country_code

Methods included from Numer

#_numeric

Instance Method Details

#get_country_geo_by_phone(phone_number) ⇒ Object

Получить геометрический (географический) центр страны по номеру телефона Если коду страны из номера телефона соответствуют более 1 страны (например, +7 для RU и KZ), то взята будет одна первая страна из списка согласно приоритету countries_priorities.yml



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ptc/get_country_by_phone.rb', line 8

def get_country_geo_by_phone(phone_number)
  return nil if phone_number.nil?

  phone_number = Phony.split(_numeric phone_number)
  return nil unless phone_number.all?

  country_code = phone_number[0]
  countries    = ISO3166::Country.find_all_by(:country_code, country_code)
  top_country  = top_priority_country_by_country_code country_code
  country      = top_country.nil? ? countries.values.first : countries[top_country]

  #gem "countries", '3.0.1'
  #country_geo  = country.values.first['geo']
  # OpenStruct.new(
  #   latitude:  country_geo['latitude'],
  #   longitude: country_geo['longitude']
  # )

  #puts [country['latitude_dec'], country['longitude_dec']].join ','
  OpenStruct.new(
              latitude:  country['latitude_dec'],
              longitude: country['longitude_dec']
  )
rescue ::Phony::SplittingError
  nil
end