Class: MoveToGo::PhoneHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/move-to-go/phone_helper.rb

Overview

The PhoneHelper helps you parse and format phone number strings into pretty looking numbers.

Class Method Summary collapse

Class Method Details

.parse_numbers(number_string, delimiters = ',', strict_mode = false) ⇒ Object

Parses the specified number_string into one or more phone numbers using the specified delimiters. If strict_mode is true only valid numbers are returned, otherwise are invalid numbers returned as found in the number_string.

Examples:

Parse a number

number = MoveToGo::PhoneHelper.parse_numbers("046 - 270 48 00")

Parses a string with two numbers and a custom delimiter

source = "046 - 270 48 00/ 031-712 44 00"
number1, number2 = MoveToGo::PhoneHelper.parse_numbers(source, '/')


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/move-to-go/phone_helper.rb', line 35

def self.parse_numbers(number_string, delimiters = ',', strict_mode = false)
    return nil if number_string.nil?
    numbers = []

    if delimiters.is_a?(Array)
        # we have several delimiters, replace all delimiters
        # in the number_string with the first delimiter
        delimiters.each do |del|
            number_string = number_string.sub(del, delimiters[0])
        end
        delimiter = delimiters[0]
    elsif delimiters.is_a?(String)
        delimiter = delimiters
    else
        raise "delimiters should be either a string or and array of strings"
    end

    number_string.split(delimiter).each do |possible_number|
        if !possible_number.empty?
            number = GlobalPhone.parse([possible_number])

            if !number.nil? && number.valid?
                numbers.push number.to_s
            else
                if !strict_mode
                    numbers.push possible_number
                end
            end
        end
    end

    if numbers.length == 0
        return ""
    elsif numbers.length == 1
        return numbers[0]
    else
        return numbers
    end
end

.parse_numbers_strict(number_string, delimiters = ',') ⇒ Object

Parses the specifed number_string and returns only valid numbers.

See Also:



20
21
22
# File 'lib/move-to-go/phone_helper.rb', line 20

def self.parse_numbers_strict(number_string, delimiters = ',')
    parse_numbers number_string, delimiters, true
end

.set_country_code(country_code) ⇒ Object

Sets the country code used during parsning. The default is swedish (:se) and if you are parsing swedish numbers you dont need to set the country code.



13
14
15
# File 'lib/move-to-go/phone_helper.rb', line 13

def self.set_country_code(country_code)
    GlobalPhone.default_territory_name = country_code
end