Sha256: 1be17b8c1996e5cfa533964c9ce01531b8561abca4f750a1a3eaf690494b33a0

Contents?: true

Size: 1.73 KB

Versions: 15

Compression:

Stored size: 1.73 KB

Contents

require 'geocoder/lookups/base'
require 'geocoder/results/smarty_streets'

module Geocoder::Lookup
  class SmartyStreets < Base
    def name
      "SmartyStreets"
    end

    def required_api_key_parts
      %w(auth-id auth-token)
    end

    # required by API as of 26 March 2015
    def supported_protocols
      [:https]
    end

    private # ---------------------------------------------------------------

    def base_query_url(query)
      if international?(query)
        "#{protocol}://international-street.api.smartystreets.com/verify?"
      elsif zipcode_only?(query)
        "#{protocol}://us-zipcode.api.smartystreets.com/lookup?"
      else
        "#{protocol}://us-street.api.smartystreets.com/street-address?"
      end
    end

    def zipcode_only?(query)
      !query.text.is_a?(Array) and query.to_s.strip =~ /\A\d{5}(-\d{4})?\Z/
    end

    def international?(query)
      !query.options[:country].nil?
    end

    def query_url_params(query)
      params = {}
      if international?(query)
        params[:freeform] = query.sanitized_text
        params[:country] = query.options[:country]
        params[:geocode] = true
      elsif zipcode_only?(query)
        params[:zipcode] = query.sanitized_text
      else
        params[:street] = query.sanitized_text
      end
      if configuration.api_key.is_a?(Array)
        params[:"auth-id"] = configuration.api_key[0]
        params[:"auth-token"] = configuration.api_key[1]
      else
        params[:"auth-token"] = configuration.api_key
      end
      params.merge(super)
    end

    def results(query)
      doc = fetch_data(query) || []
      if doc.is_a?(Hash) and doc.key?('status') # implies there's an error
        return []
      else
        return doc
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
geocoder-1.8.3 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.8.2 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.8.1 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.8.0 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.7.5 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.7.4 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.7.3 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.7.2 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.7.1 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.7.0 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.6.7 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.6.6 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.6.5 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.6.4 lib/geocoder/lookups/smarty_streets.rb
geocoder-1.6.3 lib/geocoder/lookups/smarty_streets.rb