ankane / mainstreet

Address verification for Ruby and Rails
MIT License
165 stars 14 forks source link

NoMethodError (undefined method `[]' for nil:NilClass) #13

Closed cionescu closed 4 years ago

cionescu commented 4 years ago

Hello and thanks for this awesome gem!

I'm running into the following problem: I'm trying to validate a zip-only address using an invalid zip:

verifier = MainStreet::AddressVerifier.new('49372')
verifier.success?
# => true
verifier.latitude
# Traceback (most recent call last):
# NoMethodError (undefined method `[]' for nil:NilClass)

# Doing a bit of debugging on my own, I can see the smartystreets responds that the zipcode is invalid:
verifier.result
# => #<Geocoder::Result::SmartyStreets:0x000055880a34cca8 @data={"input_index"=>0, "status"=>"invalid_zipcode", "reason"=>"Invalid ZIP Code."}, @cache_hit=nil>

Do you have any pointers as how to handle this?

ankane commented 4 years ago

Hey @cionescu, thanks for reporting 👍 Mainstreet isn't really designed for ZIP code only queries. I pushed a fix on master so that success? returns false and there's no longer an error. You'll probably want to use the Geocoder gem directly for ZIP code queries.