jkeen / tracking_number_data

Shared data for cross platform tracking number detection
MIT License
114 stars 39 forks source link

Model partnerships between shipping couriers #84

Closed jkeen closed 1 year ago

jkeen commented 1 year ago

Certain shipments (e.g. FedEx SmartPost) are shared between carriers, and until this PR this dataset had no way of modeling that relationship. Previously, consuming libraries would successfully match a FedExSmartPost number like 420112139261290983497923666238 against the FedExSmartPost criteria, and the USPS91 criteria and return both to the consumer. This wasn't wrong, but it wasn't the complete answer either. #12, #23

This change now models both sides of the relationship so that consuming libraries can detect if the tracking number returned is a partnership, and which part of the relationship that tracking number fulfills. One side of the partnership is a shipper (e.g FedEx), and the other side is the [last mile] carrier (e.g. USPS).

This is how it's implemented in the upcoming version of tracking_number

# Search defaults to only showing numbers that fulfill the carrier side of the relationship 
# (if a partnership exists at all), as this is the end a consumer would most likely be interested in.

results = TrackingNumber.search('420112139261290983497923666238') 
=> [#<TrackingNumber::USPS91:0x26ac0 420112139261290983497923666238>]

tn = results.first
tn.shipper? #=> false
tn.carrier? #=> true
tn.partnership? #=> true
tn.partners
 #=> <struct TrackingNumber::Base::PartnerStruct
 #       shipper=#<TrackingNumber::FedExSmartPost:0x30624 420112139261290983497923666238>,
 #       carrier=#<TrackingNumber::USPS91:0x2f1fc 420112139261290983497923666238>>

tn.partners.shipper #=> #<TrackingNumber::FedExSmartPost:0x30624 420112139261290983497923666238>
tn.partners.carrier == tn #=> true

To return both the shipper and carrier when searching, you can pass a second argument into search:

results = TrackingNumber.search('420112139261290983497923666238', match: :all) 
#=> [<TrackingNumber::FedExSmartPost:0x30624 420112139261290983497923666238>, <TrackingNumber::USPS91:0x2f1fc 420112139261290983497923666238>]

TrackingNumbers that are not a partnership fulfill both sides of the relationship and indicate as such:

tn = TrackingNumber.new('1Z879E930346834440')

tn.shipper? #=> true
tn.carrier? #=> true
tn.partnership? #=> false
tn.partners #=> nil
github-actions[bot] commented 1 year ago

:tada: This PR is included in version 1.5.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: