Sha256: 17c4a72aa2fa5870f24ec9d80e2a73f9aefb6ccbf9463c5c56acb46b1d0191b8

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

require 'csv'

module Tuk
  
  class Number
    
    def initialize(with_number)
      @original_number = with_number.gsub(" ", "")
    end
    
    def normalised_number
      @_normalised_number ||= begin
        self.class.remove_country_code(@original_number)
      end
    end
    
    def area_code
      self.class.area_code_for(normalised_number)
    end
    
    class << self
      def remove_country_code(number)
        if number[0..2] == "+44"
          "0" + number[3..-1]
        elsif number[0..1] == "44"
          "0" + number[2..-1]
        elsif number[0..3] == "0044"
          "0" + number[4..-1]
        else
          number
        end
      end
      
      def area_code_for(number)
        if matched = number.match(area_code_regex)
          matched[1]
        else
          nil
        end
      end
      
      def area_code_regex
        @_area_code_regex || begin
          Regexp.new("^(#{area_codes.join('|')}).*")
        end
      end
      
      def area_codes
        @_area_codes || begin
          codes = CSV.read(File.dirname(__FILE__) + '/area_codes.csv').drop(1).map{|n| "0" + n[1]}
        end
      end      
      
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tuk-0.2.2 lib/tuk.rb
tuk-0.2.1 lib/tuk.rb
tuk-0.2.0 lib/tuk.rb