Sha256: ef52ba936bdf01943d4f86f228293f6191df39160b3a78b22cba9f8664a33ade

Contents?: true

Size: 1.21 KB

Versions: 29

Compression:

Stored size: 1.21 KB

Contents

module TelephoneNumber
  class TimeZoneDetector
    attr_reader :phone_number, :timezone

    def initialize(phone_number)
      @phone_number = phone_number
    end

    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

    def data
      @data ||= Marshal.load(File.binread(File.expand_path('../../../data/timezones/map_data.dat', __FILE__)))
    end

    private

    # 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.
    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
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
telephone_number-1.4.22 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.21 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.20 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.19 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.18 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.16 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.15 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.14 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.13 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.12 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.11 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.10 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.9 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.8 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.7 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.6 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.5 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.4 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.3 lib/telephone_number/time_zone_detector.rb
telephone_number-1.4.2 lib/telephone_number/time_zone_detector.rb