theseus/app/lib/snail_mail/imb.rb
2025-12-29 10:39:37 -05:00

34 lines
963 B
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module SnailMail
class IMb
attr_reader :letter
def initialize(letter)
@letter = letter
end
def generate
barcode_id = "00" # no OEL
stid = "310" # no address corrections no printed endorsements, but! IV-MTR!
mailer_id = letter.usps_mailer_id&.mid
return "" unless mailer_id
serial_number = letter.imb_serial_number
routing_code = letter.address.us? ? letter.address.postal_code.gsub(/[^0-9]/, "") : nil # zip(+dpc?) but no dash
routing_code = nil unless [5, 9, 11].include?(routing_code&.length)
begin
Imb::Barcode.new(
barcode_id,
stid,
mailer_id,
serial_number,
routing_code
).barcode_letters
rescue ArgumentError => e
Rails.logger.warn("Bad IMb input: #{e.message} @ MID #{mailer_id} SN #{serial_number} RC #{routing_code}")
Sentry.capture_exception(e)
""
end
end
end
end