Sha256: f45e5094c006e00bc19cf8750fbae3f139e7dce3fecacaea0d2ae803b0e08742

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

module TelephoneNumber
  module ClassMethods
    attr_accessor :override_file, :default_format_string
    attr_reader :default_format_pattern

    def default_format_pattern=(format_string)
      @default_format_pattern = Regexp.new(format_string)
    end

    def parse(number, country = detect_country(number))
      TelephoneNumber::Number.new(sanitize(number), country)
    end

    def valid?(number, country = detect_country(number), keys = [])
      parse(number, country).valid?(keys)
    end

    def invalid?(*args)
      !valid?(*args)
    end

    def sanitize(input_number)
      input_number.to_s.gsub(/\D/, '')
    end

    def detect_country(number)
      sanitized_number = sanitize(number)
      detected_country = Country.all_countries.detect do |country|
        sanitized_number.start_with?(country.country_code) && valid?(sanitized_number, country.country_id)
      end

      detected_country.country_id.to_sym if detected_country
    end

    # generates binary file from xml that user gives us
    def generate_override_file(file)
      DataImporter.new(file, override: true).import!
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
telephone_number-1.2.0 lib/telephone_number/class_methods.rb
telephone_number-1.2.0.rc1 lib/telephone_number/class_methods.rb