Sha256: 0b23ac4854b8911a199f04da1edf8a74db7fd2da59009b15d8dd3d28b2397a9e

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

module Torasup
  class PhoneNumber
    attr_reader :number,
                :country_code, :country_id,
                :area_code, :prefix, :local_number, :location,
                :operator

    def initialize(phone_number)
      parse_phone_number(phone_number)
    end

    def type
      operator && operator.type
    end

    private

    def parse_phone_number(number)
      raise "Not a vaild number" unless Phony.plausible?(number)

      @number = Phony.normalize(number)
      number_parts = split_number
      @country_code = number_parts.shift
      @country_id = Torasup.country_id(@country_code)
      area_code_or_prefix = number_parts.shift
      local_number = number_parts.join
      @location = Location.new(@country_id, area_code_or_prefix)
      @area_code = @location.area_code
      @operator = Operator.new(@country_code, area_code_or_prefix, local_number)
      @prefix = @operator.prefix
      @local_number = operator.local_number
    end

    def split_number
      Phony.split(@number).reject { |part| part == false }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
torasup-0.3.3 lib/torasup/phone_number.rb
torasup-0.3.2 lib/torasup/phone_number.rb
torasup-0.3.1 lib/torasup/phone_number.rb
torasup-0.3.0 lib/torasup/phone_number.rb