Has anyone found a good Gem or maybe just a RegEx ...
# support
z
Has anyone found a good Gem or maybe just a RegEx for checking if an address is a PO Box or APO (US military) address?
e
I’ve seen projects using http://smartystreets.com, pulling and caching normalized data from it, then checking for all kinds of things, including po boxes
g
I needed to do something similar in a custom shipping calculator. Probably not the best way to go about it, but this is what we came up with that does what we wanted:
Copy code
# don't ship to military or po boxes
      if ['AE','AA','AP','AF','AC','AM'].include? order.ship_address.state.abbr
        return false
      end

      # don't ship to POBox
      if order.ship_address.address1.match(/(^|(?:post(al)? *(?:office *)?|p[. ]*o\.? *))box *#? *\w+/ui)
        return false
      end
      if order.ship_address.address2 && order.ship_address.address2.match(/(^|(?:post(al)? *(?:office *)?|p[. ]*o\.? *))box *#? *\w+/ui)
        return false
      end
z
Thanks both these are helpful answers! I think I might go with a regex from Stack Overflow to start, then something like Smarty if those still aren't working great.
b
We had something at Bevv for this. I think we took a download of zipcodes from somewhere and it was able to tell us if the zipcode was a military location or a PO Box.
👍 1
And if the zipcode was retired as well.