Class: Tuk::Number
- Inherits:
-
Object
- Object
- Tuk::Number
- Defined in:
- lib/tuk.rb
Class Method Summary collapse
- .area_code_for(number) ⇒ Object
- .area_code_regex ⇒ Object
- .area_codes ⇒ Object
- .remove_country_code(number) ⇒ Object
Instance Method Summary collapse
- #area_code ⇒ Object
-
#initialize(with_number) ⇒ Number
constructor
A new instance of Number.
- #normalised_number ⇒ Object
Constructor Details
#initialize(with_number) ⇒ Number
Returns a new instance of Number.
7 8 9 |
# File 'lib/tuk.rb', line 7 def initialize(with_number) @original_number = with_number.gsub(" ", "") end |
Class Method Details
.area_code_for(number) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/tuk.rb', line 34 def area_code_for(number) if matched = number.match(area_code_regex) matched[1] else nil end end |
.area_code_regex ⇒ Object
42 43 44 45 46 |
# File 'lib/tuk.rb', line 42 def area_code_regex @_area_code_regex || begin Regexp.new("^(#{area_codes.join('|')}).*") end end |
.area_codes ⇒ Object
48 49 50 51 52 |
# File 'lib/tuk.rb', line 48 def area_codes @_area_codes || begin codes = CSV.read(File.dirname(__FILE__) + '/area_codes.csv').drop(1).map{|n| "0" + n[1]} end end |
.remove_country_code(number) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tuk.rb', line 22 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 |
Instance Method Details
#area_code ⇒ Object
17 18 19 |
# File 'lib/tuk.rb', line 17 def area_code self.class.area_code_for(normalised_number) end |
#normalised_number ⇒ Object
11 12 13 14 15 |
# File 'lib/tuk.rb', line 11 def normalised_number @_normalised_number ||= begin self.class.remove_country_code(@original_number) end end |