class TelephoneNumber::TimeZoneDetector
Attributes
phone_number[R]
timezone[R]
Public Class Methods
new(phone_number)
click to toggle source
# File lib/telephone_number/time_zone_detector.rb, line 5 def initialize(phone_number) @phone_number = phone_number end
Public Instance Methods
data()
click to toggle source
# File lib/telephone_number/time_zone_detector.rb, line 16 def data @data ||= Marshal.load(File.binread(File.expand_path('../../../data/timezones/map_data.dat', __FILE__))) end
detect_timezone()
click to toggle source
# File lib/telephone_number/time_zone_detector.rb, line 9 def detect_timezone normalized_number = build_normalized_number.dup timezone = nil (normalized_number.length - 2).times { break if timezone = data[normalized_number.chop!] } timezone.to_s.split('&').join(', ') end
Private Instance Methods
build_normalized_number()
click to toggle source
Google's geocoding data is odd in that it uses a non-standard format for lookups on countries that have a mobile token. While I don't believe that this is used right now it will be if/when Google adds more specific data for Argentina. This method safe guards against that.
# File lib/telephone_number/time_zone_detector.rb, line 26 def build_normalized_number return phone_number.e164_number(formatted: false) unless mobile_token = phone_number.country.mobile_token if phone_number.parser.normalized_number.start_with?(mobile_token) phone_number.e164_number(formatted: false).sub(mobile_token, '') else phone_number.e164_number(formatted: false) end end