Module: Phoney::Formatter

Included in:
Parser
Defined in:
lib/phoney/formatter.rb

Instance Method Summary collapse

Instance Method Details

#extract_country_code(input, options = {}) ⇒ Object

TODO: handle case where international call prefix implicitly specifies country (e.g. tz: “005->254”)



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/phoney/formatter.rb', line 104

def extract_country_code(input, options={})
  options[:region] ||= Phoney.region
  intl_prefix = international_call_prefix_for(input, region: options[:region])
  
  # only try to extract a country code if we're dialing internationally
  if intl_prefix
    rest   = input[intl_prefix.count(NUMPAD_CHARS)..-1]
    region = Phoney::Region.all.find { |r| rest.start_with? r.country_code.to_s }
    
    region.country_code.to_s if region
  end
end

#extract_trunk_prefix(input, options = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/phoney/formatter.rb', line 117

def extract_trunk_prefix(input, options={})
  options[:region] ||= Phoney.region
  
  intl_prefix  = international_call_prefix_for input
  country_code = extract_country_code(input, region: options[:region])
  region_scope = country_code.nil? ? options[:region] : Phoney::Region[country_code]
  
  if intl_prefix
    # Strip international prefix from number
    input = input[intl_prefix.count(NUMPAD_CHARS)..-1]
  end
  
  if country_code
    # Strip country code from number
    input = input[country_code.count(DIGITS)..-1]
  end
  
  region_scope.trunk_prefixes.each do |prefix|
    stripped_prefix = Regexp.escape prefix.delete(' ')
    regexp = Regexp.new "^#{stripped_prefix.gsub('\\#', '[0-9]')}"
  
    return format(input, prefix.gsub(/[0-9]/, '#'), fill: '') if input =~ regexp
  end
  
  return nil
end

#format(input, pattern, options = {}) ⇒ Object

Returns the string formatted according to a pattern.

Examples: format(‘123456789’, ‘XXX-XX-XXXX’)

=> "123-45-6789"

format(‘12345’, ‘XXX-XX-XXXX’)

=> "123-45"

Parameters: string – The string to be formatted. pattern – The format string, see above examples. fill – A string for padding. If the empty string, then the pattern is

filled as much as possible, and the rest of the pattern is
truncated. If nil, and the string is too long for the pattern,
the string is returned unchanged.  Otherwise, the string is
padded to fill the pattern, which is not truncated.


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
74
75
76
# File 'lib/phoney/formatter.rb', line 19

def format(input, pattern, options={})
  fill         = options[:fill]
  intl_prefix  = options[:intl_prefix]||''
  trunk_prefix = options[:trunk_prefix]||''
  slots        = pattern.count(PLACEHOLDER_CHAR)
  
  # Return original input if it is too long
  return input if (fill.nil? && input.length > slots)
  
  # Pad and clone the string if necessary.
  source = (fill.nil? || fill.empty?) ? input : input.ljust(slots, fill)
  
  result   = ''
  slot     = 0
  has_open = had_c = had_n = false
  
  pattern.split('').each_with_index do |chr, index|
    case chr
    when 'c'
      had_c = true
      result << intl_prefix
    when 'n'
      had_n = true
      result << trunk_prefix
    when '#'
      if slot < source.length
        result << source[slot]
        slot += 1
      else
        result << ' ' if has_open
      end
    when '('
      if slot < source.length
        has_open = true
        result << chr
      end
    when ')'
      if (slot < source.length || has_open)
        has_open = false
        result << chr
      end
    else
      # Don't show space after n if no trunk prefix or after c if no intl prefix
      next if (chr == ' ' && pattern[index-1] == 'n' && trunk_prefix.empty?)
      next if (chr == ' ' && pattern[index-1] == 'c' && intl_prefix.empty?)
      
      result << chr if (slot < source.length)
    end
  end

  # Not all format strings have a 'c' or 'n' in them.
  # If we have an international prefix or a trunk prefix but the format string
  # doesn't explictly say where to put it then simply add it to the beginning.
  result.prepend trunk_prefix if (!had_n && !trunk_prefix.empty?)
  result.prepend "#{intl_prefix} " if (!had_c && !intl_prefix.empty?)
  
  result.strip
end

#international_call_prefix_for(input, options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/phoney/formatter.rb', line 84

def international_call_prefix_for(input, options={})
  options[:region] ||= Phoney.region
  
  return nil if input.length == 0
  
  options[:region].dialout_prefixes.each do |prefix|
    stripped_prefix = Regexp.escape prefix.delete(' ').split('->').first[0, input.length]
    regexp = Regexp.new "^#{stripped_prefix.gsub('\\#', '[0-9]')}"
      
    return format(input, prefix.gsub(/[\\+0-9]/, '#'), fill: '') if input =~ regexp
    
    if (input.start_with?('+') && (stripped_prefix.start_with?(input[1..-1]) || input[1..-1] =~ regexp))
      return format(input, '#'+prefix.gsub(/[\\+0-9]/, '#'), fill: '')
    end
  end
  
  input.start_with?('+') ? '+' : nil
end

#normalize(str) ⇒ Object

Strips all non-numberpad characters from a string

> For example: “+45 (123) 023 1.1.1” -> “+45123023111”



80
81
82
# File 'lib/phoney/formatter.rb', line 80

def normalize(str)
  str.gsub(/[^0-9+*#]/,'') unless str.nil?
end