Sha256: fb15fd590cfa450541fa48bdbf56e1b3976cc24141de1d0de9ceaf06f07e04d2

Contents?: true

Size: 1.58 KB

Versions: 25

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require "active_support/number_helper/number_converter"

module ActiveSupport
  module NumberHelper
    class NumberToPhoneConverter < NumberConverter #:nodoc:
      def convert
        str = country_code(opts[:country_code]).dup
        str << convert_to_phone_number(number.to_s.strip)
        str << phone_ext(opts[:extension])
      end

      private

        def convert_to_phone_number(number)
          if opts[:area_code]
            convert_with_area_code(number)
          else
            convert_without_area_code(number)
          end
        end

        def convert_with_area_code(number)
          default_pattern = /(\d{1,3})(\d{3})(\d{4}$)/
          number.gsub!(regexp_pattern(default_pattern),
                       "(\\1) \\2#{delimiter}\\3")
          number
        end

        def convert_without_area_code(number)
          default_pattern = /(\d{0,3})(\d{3})(\d{4})$/
          number.gsub!(regexp_pattern(default_pattern),
                       "\\1#{delimiter}\\2#{delimiter}\\3")
          number.slice!(0, 1) if start_with_delimiter?(number)
          number
        end

        def start_with_delimiter?(number)
          delimiter.present? && number.start_with?(delimiter)
        end

        def delimiter
          opts[:delimiter] || "-"
        end

        def country_code(code)
          code.blank? ? "" : "+#{code}#{delimiter}"
        end

        def phone_ext(ext)
          ext.blank? ? "" : " x #{ext}"
        end

        def regexp_pattern(default_pattern)
          opts.fetch :pattern, default_pattern
        end
    end
  end
end

Version data entries

25 entries across 23 versions & 5 rubygems

Version Path
activesupport-6.0.2.2 lib/active_support/number_helper/number_to_phone_converter.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/number_helper/number_to_phone_converter.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-6.0.2.1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-6.0.2 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-6.0.2.rc2 lib/active_support/number_helper/number_to_phone_converter.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_phone_converter.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/activesupport-6.0.1/lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-6.0.2.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_phone_converter.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/activesupport-6.0.1/lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-6.0.1 lib/active_support/number_helper/number_to_phone_converter.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-6.0.1.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_phone_converter.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_phone_converter.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_phone_converter.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_phone_converter.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-6.0.0 lib/active_support/number_helper/number_to_phone_converter.rb