Refactor CustomerInfoForm to use find_by_format

Stephen Paul Weber created

Change summary

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(-)

Detailed changes

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

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)

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

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