From b5ae16166ab0bfbb2d360c7987396fd127c2264e Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 23 Mar 2022 14:33:15 -0500 Subject: [PATCH] Refactor CustomerInfoForm to use find_by_format --- lib/customer_info_form.rb | 44 ++++++++++----------------------- lib/customer_repo.rb | 2 +- lib/proxied_jid.rb | 19 +++++++------- test/test_customer_info_form.rb | 28 ++++++++++----------- 4 files changed, 37 insertions(+), 56 deletions(-) diff --git a/lib/customer_info_form.rb b/lib/customer_info_form.rb index e89ef875ed9756d5a6e4d909ea59f76b584af96d..881aadb3d2f51f1ac0f676d2e702875a9b5e1097 100644 --- a/lib/customer_info_form.rb +++ b/lib/customer_info_form.rb @@ -24,42 +24,24 @@ class CustomerInfoForm end def parse_something(value) - parser = Parser.new(@customer_repo) - EMPromise.all([ - parser.as_customer_id(value), - parser.as_jid(value), - parser.as_phone(value), + find_customer_one(value), + find_customer_one(Blather::JID.new(value)), + find_customer_one(ProxiedJID.proxy(value)), + find_customer_by_phone(value), EMPromise.resolve(NoCustomer.new) ]).then { |approaches| approaches.compact.first } end - class Parser - def initialize(customer_repo) - @customer_repo = customer_repo - end - - def as_customer_id(value) - @customer_repo.find(value).catch { nil } - end - - def as_cheo(value) - ProxiedJID.proxy(Blather::JID.new(value)) - end - - def as_jid(value) - EMPromise.all([ - @customer_repo.find_by_jid(value).catch { nil }, - @customer_repo.find_by_jid(as_cheo(value)).catch { nil } - ]).then { |approaches| approaches.compact.first } - end - - def as_phone(value) - unless value.gsub(/[^0-9]/, "") =~ /^\+?1?(\d{10})$/ - return EMPromise.resolve(nil) - end + def find_customer_one(q) + @customer_repo.find_by_format(q).catch { nil } + end - @customer_repo.find_by_tel("+1#{$1}").catch { nil } - end + def find_customer_by_phone(value) + value + .gsub(/\D/, "") + .match(/\A1?(\d{10})\Z/) + &.[](1) + &.then { find_customer_one("+1#{_1}") } end end diff --git a/lib/customer_repo.rb b/lib/customer_repo.rb index db19a27690e303ad0402bed3f973ded744a8158b..e943810ca7e4ac640c2e1707f981788fbded6af3 100644 --- a/lib/customer_repo.rb +++ b/lib/customer_repo.rb @@ -22,7 +22,7 @@ class CustomerRepo module QueryKey def self.for(s) case s - when Blather::JID + when Blather::JID, ProxiedJID JID.for(s) when /\Axmpp:(.*)/ JID.for($1) diff --git a/lib/proxied_jid.rb b/lib/proxied_jid.rb index 055d96e18a82b2abda824fe59544d6562032578f..dfb309c0d7fabea33151c871bcceba7039b6fa41 100644 --- a/lib/proxied_jid.rb +++ b/lib/proxied_jid.rb @@ -15,15 +15,14 @@ class ProxiedJID < SimpleDelegator end def self.proxy(jid, suffix=CONFIG[:upstream_domain]) - ProxiedJID.new( - Blather::JID.new( - jid.stripped.to_s - .gsub(/([ "&'\/:<>@]|\\(?=#{ESCAPED}))/) { |s| - "\\#{s.ord.to_s(16)}" - }, - suffix, - jid.resource - ) - ) + jid = Blather::JID.new(jid) + ProxiedJID.new(Blather::JID.new( + jid.stripped.to_s + .gsub(/([ "&'\/:<>@]|\\(?=#{ESCAPED}))/) { |s| + "\\#{s.ord.to_s(16)}" + }, + suffix, + jid.resource + )) end end diff --git a/test/test_customer_info_form.rb b/test/test_customer_info_form.rb index dd10fded5eeb29735c79991c00c61a9d9ced1c1b..8d1d0cf2a4de3d2f686044a315ccbbdf2e6e419e 100644 --- a/test/test_customer_info_form.rb +++ b/test/test_customer_info_form.rb @@ -10,23 +10,23 @@ class FakeRepo @customers = customers end - def find(id) - EMPromise.resolve(nil).then do - @customers.find { |cust| cust.customer_id == id } || raise("No Customer") - end - end - - def find_by_jid(jid) - EMPromise.resolve(nil).then do - @customers.find { |cust| - cust.jid.to_s == jid.to_s - } || raise("No Customer") - end + def find_by(k, v) + @customers.find { |cust| cust.public_send(k).to_s == v.to_s } end - def find_by_tel(tel) + def find_by_format(s) EMPromise.resolve(nil).then do - @customers.find { |cust| cust.tel == tel } || raise("No Customer") + key = CustomerRepo::QueryKey.for(s) + case key + when CustomerRepo::QueryKey::ID + find_by(:customer_id, key.customer_id) + when CustomerRepo::QueryKey::JID + find_by(:jid, key.jid) + when CustomerRepo::QueryKey::Tel + find_by(:tel, key.tel) + else + raise "Un-faked format: #{s}" + end || raise("No Customer") end end end