From 950e0683b159f154c9ccfacde0c2ff590244b4c3 Mon Sep 17 00:00:00 2001 From: Chris Gunther Date: Tue, 20 Aug 2024 14:57:16 -0400 Subject: [PATCH] Upcase body before searching to find lowercase tracking numbers The regexps define the search pattern in terms of uppercase characters, so when the source data you're searching contains lowercase tracking numbers, they don't match. By upcasing the body before searching, it's more likely to match the search patterns. This also matches that initializing a new `TrackingNumber` object upcases the number for its internal value. Inspired by: https://github.com/jkeen/tracking_number_data/pull/102 Meant as an interim solution until it's decided whether the source regexps should be case-sensitive or not. --- lib/tracking_number/base.rb | 2 +- test/tracking_number_test.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/tracking_number/base.rb b/lib/tracking_number/base.rb index 4a905b3..f1eaa09 100644 --- a/lib/tracking_number/base.rb +++ b/lib/tracking_number/base.rb @@ -24,7 +24,7 @@ def self.scan(body) # matches with match groups within the match data matches = [] - body.scan(const_get(:SEARCH_PATTERN)) do + body.upcase.scan(const_get(:SEARCH_PATTERN)) do # get the match data instead, which is needed with these types of regexes matches << $~ end diff --git a/test/tracking_number_test.rb b/test/tracking_number_test.rb index 3baccad..55e2d1e 100644 --- a/test/tracking_number_test.rb +++ b/test/tracking_number_test.rb @@ -40,6 +40,12 @@ class TrackingNumberTest < Minitest::Test assert_equal 1, s.size assert_equal "1Z879E930346834440", s.first.tracking_number end + + should "return tracking numbers entered as lowercase" do + s = TrackingNumber.search("hello 1z879E930346834440 bye") + assert_equal 1, s.size + assert_equal "1Z879E930346834440", s.first.tracking_number + end end context "tracking number additional data for ups" do @@ -201,7 +207,7 @@ class TrackingNumberTest < Minitest::Test context "searching numbers that have partners" do partnership_number = "420 11213 92 6129098349792366623 8" single_number = "92001903060085300042901077" - + search_string = ["number that matches two services", partnership_number, " number that matches only one: ", single_number, "let's see if that does it"].join(' ') should "match only carriers by default" do