diff --git a/Gemfile b/Gemfile index d66fb0b38..52624c5c0 100644 --- a/Gemfile +++ b/Gemfile @@ -68,7 +68,6 @@ gem 'net-ldap' gem 'writeexcel' gem 'icalendar' gem 'browser' -gem 'phony' # integrations gem 'slack-notifier' diff --git a/Gemfile.lock b/Gemfile.lock index 66c4239ac..d2ffa73ec 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -228,7 +228,6 @@ GEM parser (2.3.1.2) ast (~> 2.2) pg (0.18.4) - phony (2.15.29) pkg-config (1.1.7) pluginator (1.3.0) power_assert (0.3.1) @@ -426,7 +425,6 @@ DEPENDENCIES omniauth-oauth2 omniauth-twitter pg - phony pre-commit puma rack-livereload diff --git a/app/models/cti/caller_id.rb b/app/models/cti/caller_id.rb index 4118a58df..09f1345dd 100644 --- a/app/models/cti/caller_id.rb +++ b/app/models/cti/caller_id.rb @@ -2,6 +2,8 @@ module Cti class CallerId < ApplicationModel self.table_name = 'cti_caller_ids' + DEFAULT_COUNTRY_ID = '49'.freeze + =begin Cti::CallerId.maybe_add( @@ -117,7 +119,7 @@ returns attributes.each { |_attribute, value| next if value.class != String next if value.empty? - local_caller_ids = Cti::CallerId.parse_text(value) + local_caller_ids = Cti::CallerId.extract_numbers(value) next if local_caller_ids.empty? caller_ids = caller_ids.concat(local_caller_ids) } @@ -188,7 +190,7 @@ returns =begin - caller_ids = Cti::CallerId.parse_text('...') + caller_ids = Cti::CallerId.extract_numbers('...') returns @@ -196,41 +198,25 @@ returns =end - def self.parse_text(text) - caller_ids = [] + def self.extract_numbers(text) + # see specs for example + text.scan(/([\d|\s|\-|\(|\)]{6,26})/).map do |match| + normalize_number(match[0]) + end + end - # 022 1234567 - # 021 123 2345 - # 0271233211 - # 021-233-9123 - # 09 123 32112 - # 021 2331231 or 021 321123123 - # 622 32281 - # 5754321 - # 092213212 - # (09)1234321 - # +41 30 53 00 00 000 - # +42 160 0000000 - # +43 (0) 30 60 00 00 00-0 - # 0043 (0) 30 60 00 00 00-0 - - default_country_id = '49' - text.gsub!(/([\d|\s|\-|\(|\)]{6,26})/) { - number = $1.strip - number.sub!(/^00/, '') - number.sub!(/\(0\)/, '') - number.gsub!(/(\s|\-|\(|\))/, '') - if !Phony.plausible?(number) - if number =~ /^0/ - number.gsub!(/^0/, default_country_id) - else - number = "#{default_country_id}#{number}" - end - next if !Phony.plausible?(number) - end - caller_ids.push number - } - caller_ids + def self.normalize_number(number) + number = number.gsub(/[\s-]/, '') + number.gsub!(/^(00)?(\+?\d\d)\(0?(\d*)\)/, '\\1\\2\\3') + number.gsub!(/\D/, '') + case number + when /^00/ + number[2..-1] + when /^0/ + DEFAULT_COUNTRY_ID + number[1..-1] + else + number + end end def self.get_comment_preferences(caller_id, direction) @@ -241,7 +227,7 @@ returns preferences_maybe = {} preferences_maybe[direction] = [] - lookup(caller_id).each { |record| + lookup(extract_numbers(caller_id)).each { |record| if record.level == 'known' preferences_known[direction].push record else diff --git a/spec/models/cti/caller_id_spec.rb b/spec/models/cti/caller_id_spec.rb new file mode 100644 index 000000000..f7c7a5bd1 --- /dev/null +++ b/spec/models/cti/caller_id_spec.rb @@ -0,0 +1,34 @@ +require 'rails_helper' + +RSpec.describe Cti::CallerId do + + describe 'extract_numbers' do + it { expect(described_class.extract_numbers("some text\ntest 123")).to eq [] } + it { expect(described_class.extract_numbers('Lorem ipsum dolor sit amet, consectetuer +49 (0) 30 60 00 00 00-0 adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel.')).to eq ['4930600000000'] } + it { expect(described_class.extract_numbers("GS Oberalteich\nTelefon 09422 1000 Telefax 09422 805000\nE-Mail: ")).to eq %w(4994221000 499422805000) } + it { expect(described_class.extract_numbers('Tel +41 81 288 63 93 / +41 76 346 72 14 ...')).to eq %w(41812886393 41763467214) } + it { expect(described_class.extract_numbers("P: +1 (949) 431 0000\nF: +1 (949) 431 0001\nW: http://znuny")).to eq %w(19494310000 19494310001) } + end + + describe 'normalize_number' do + # can be anything + it { expect(described_class.normalize_number('5754321')).to eq '5754321' } + it { expect(described_class.normalize_number('622 32281')).to eq '62232281' } + it { expect(described_class.normalize_number('0049 1234 123456789')).to eq '491234123456789' } + it { expect(described_class.normalize_number('022 1234567')).to eq '49221234567' } + it { expect(described_class.normalize_number('0271233211')).to eq '49271233211' } + it { expect(described_class.normalize_number('021-233-9123')).to eq '49212339123' } + it { expect(described_class.normalize_number('09 123 32112')).to eq '49912332112' } + it { expect(described_class.normalize_number('021 2331231')).to eq '49212331231' } + it { expect(described_class.normalize_number('021 321123123')).to eq '4921321123123' } + it { expect(described_class.normalize_number('0150 12345678')).to eq '4915012345678' } + it { expect(described_class.normalize_number('092213212')).to eq '4992213212' } + it { expect(described_class.normalize_number('(09)1234321')).to eq '4991234321' } + it { expect(described_class.normalize_number('+49 30 53 00 00 000')).to eq '4930530000000' } + it { expect(described_class.normalize_number('+49 160 0000000')).to eq '491600000000' } + it { expect(described_class.normalize_number('+49 (0) 30 60 00 00 00-0')).to eq '4930600000000' } + it { expect(described_class.normalize_number('0043 (0) 30 60 00 00 00-0')).to eq '4330600000000' } + it { expect(described_class.normalize_number('0043 30 60 00 00 00-0')).to eq '4330600000000' } + it { expect(described_class.normalize_number('1-888-407-4747')).to eq '18884074747' } + end +end diff --git a/test/unit/cti_caller_id_test.rb b/test/unit/cti_caller_id_test.rb index b5a367730..1f49599bf 100644 --- a/test/unit/cti_caller_id_test.rb +++ b/test/unit/cti_caller_id_test.rb @@ -3,97 +3,6 @@ require 'test_helper' class CtiCallerIdTest < ActiveSupport::TestCase - test '1 parse possible phone numbers in text' do - text = "some text\ntest 123" - result = [] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '0049 1234 123456789' - result = ['491234123456789'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '022 1234567' - result = ['49221234567'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '0271233211' - result = ['49271233211'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '021-233-9123' - result = ['49212339123'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '09 123 32112' - result = ['49912332112'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '021 2331231' - result = ['49212331231'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '021 321123123' - result = ['4921321123123'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '622 32281' - result = ['4962232281'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '5754321' - result = ['495754321'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '092213212' - result = ['4992213212'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '(09)1234321' - result = ['4991234321'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '+49 30 53 00 00 000' - result = ['4930530000000'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '+49 160 0000000' - result = ['491600000000'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '+49 (0) 30 60 00 00 00-0' - result = ['4930600000000'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '0043 (0) 30 60 00 00 00-0' - result = ['4330600000000'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '0043 30 60 00 00 00-0' - result = ['4330600000000'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = '1-888-407-4747' - result = ['18884074747'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = 'Lorem ipsum dolor sit amet, consectetuer +49 (0) 30 60 00 00 00-0 adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel.' - result = ['4930600000000'] - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = "GS Oberalteich\nTelefon 09422 1000 Telefax 09422 805000\nE-Mail: " - result = %w(4994221000 499422805000) - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = 'Tel +41 81 288 63 93 / +41 76 346 72 14 ...' - result = %w(41812886393 41763467214) - assert_equal(result, Cti::CallerId.parse_text(text)) - - text = "P: +1 (949) 431 0000\nF: +1 (949) 431 0001\nW: http://znuny" - result = %w(19494310000 19494310001) - assert_equal(result, Cti::CallerId.parse_text(text)) - - end - test '2 lookups' do Ticket.destroy_all