class Phonejack::Formatter
Attributes
country[R]
normalized_number[R]
original_number[R]
valid[R]
valid?[R]
Public Class Methods
new(number_obj)
click to toggle source
# File lib/phonejack/formatter.rb, line 6 def initialize(number_obj) @normalized_number = number_obj.normalized_number @country = number_obj.country @valid = number_obj.valid? @original_number = number_obj.original_number end
Public Instance Methods
e164_number(formatted: true)
click to toggle source
# File lib/phonejack/formatter.rb, line 18 def e164_number(formatted: true) return original_or_default if !valid? build_e164_number(formatted: formatted) end
international_number(formatted: true)
click to toggle source
# File lib/phonejack/formatter.rb, line 23 def international_number(formatted: true) return original_or_default if !valid? || !number_format build_international_number(formatted: formatted) end
national_number(formatted: true)
click to toggle source
# File lib/phonejack/formatter.rb, line 13 def national_number(formatted: true) return original_or_default if !valid? || !number_format build_national_number(formatted: formatted) end
Private Instance Methods
build_e164_number(formatted: true)
click to toggle source
# File lib/phonejack/formatter.rb, line 53 def build_e164_number(formatted: true) formatted_string = "+#{country.country_code}#{normalized_number}" formatted ? formatted_string : Phonejack.sanitize(formatted_string) end
build_international_number(formatted: true)
click to toggle source
# File lib/phonejack/formatter.rb, line 58 def build_international_number(formatted: true) return original_or_default if !valid? || number_format.nil? captures = normalized_number.match(number_format.pattern).captures key = number_format.intl_format || number_format.format formatted_string = "+#{country.country_code} #{format(ruby_format_string(key), *captures)}" formatted ? formatted_string : Phonejack.sanitize(formatted_string) end
build_national_number(formatted: true)
click to toggle source
# File lib/phonejack/formatter.rb, line 36 def build_national_number(formatted: true) captures = normalized_number.match(number_format.pattern).captures national_prefix_formatting_rule = number_format.national_prefix_formatting_rule || country.national_prefix_formatting_rule formatted_string = format(ruby_format_string(number_format.format), *captures) captures.delete(country.mobile_token) if national_prefix_formatting_rule national_prefix_string = national_prefix_formatting_rule.dup national_prefix_string.gsub!(/\$NP/, country.national_prefix) national_prefix_string.gsub!(/\$FG/, captures[0]) formatted_string.sub!(captures[0], national_prefix_string) end formatted ? formatted_string : Phonejack.sanitize(formatted_string) end
number_format()
click to toggle source
# File lib/phonejack/formatter.rb, line 32 def number_format @number_format ||= country.detect_format(normalized_number) end
original_or_default()
click to toggle source
# File lib/phonejack/formatter.rb, line 70 def original_or_default return original_number unless Phonejack.default_format_string && Phonejack.default_format_pattern captures = original_number.match(Phonejack.default_format_pattern).captures format(ruby_format_string(Phonejack.default_format_string), *captures) end
ruby_format_string(format_string)
click to toggle source
# File lib/phonejack/formatter.rb, line 66 def ruby_format_string(format_string) format_string.gsub(/(\$\d)/) { |cap| "%#{cap.reverse}s" } end