Sha256: 770ccd213eab5fa54ea13b4f8c3672cb74c815f4b0a89c5a5db586ae226b2a1a

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module MainStreet
  class AddressVerifier
    def initialize(address, country: nil)
      @address = address
      @country = country
    end

    def success?
      failure_message.nil?
    end

    def failure_message
      if !result
        "Address can't be confirmed"
      elsif result.respond_to?(:analysis)
        analysis = result.analysis

        if analysis["verification_status"]
          case analysis["verification_status"]
          when "Verified"
            nil # success!!
          when "Ambiguous", "Partial", "None"
            "Address can't be confirmed"
          else
            raise "Unknown verification_status"
          end
        elsif analysis["dpv_match_code"]
          case analysis["dpv_match_code"]
          when "Y"
            nil # success!!
          when "N"
            "Address can't be confirmed"
          when "S"
            "Apartment or suite can't be confirmed"
          when "D"
            "Apartment or suite is missing"
          else
            raise "Unknown dpv_match_code"
          end
        end
      end
    end

    def result
      @result ||= begin
        options = {lookup: lookup}
        options[:country] = @country if @country && !usa?
        Geocoder.search(@address, options).first
      end
    end

    def latitude
      result && result.latitude
    end

    def longitude
      result && result.longitude
    end

    private

    def usa?
      ["United States", "USA", "US", "840"].include?(@country.to_s)
    end

    def lookup
      ENV["SMARTY_STREETS_AUTH_ID"] ? :smarty_streets : nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mainstreet-0.2.0 lib/mainstreet/address_verifier.rb