Sha256: 77e815f6560c81857549aba1f6ec4fc9996110353280703e033bb872dd997064

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

module EasyPost
  class Address < Resource

    def self.create_and_verify(params={}, carrier=nil, api_key=nil)
      wrapped_params = {}
      wrapped_params[self.class_name().to_sym] = params
      wrapped_params[:carrier] = carrier
      response, api_key = EasyPost.request(:post, url + '/create_and_verify', api_key, wrapped_params)

      if response.has_key?(:address)
        if response.has_key?(:message)
          response[:address][:message] = response[:message]
        end
        verified_address = EasyPost::Util::convert_to_easypost_object(response[:address], api_key)
        return verified_address
      else
        raise_verification_failure
      end
    end

    def verify(params={}, carrier=nil)
      begin
        response, api_key = EasyPost.request(:get, url + '/verify?carrier=' + String(carrier), @api_key, params)
      rescue
        raise_verification_failure
      end

      if response.has_key?(:address)
        if response.has_key?(:message)
          response[:address][:message] = response[:message]
        end
        verified_address = EasyPost::Util::convert_to_easypost_object(response[:address], api_key)
        return verified_address
      else
        raise_verification_failure
      end

      return self
    end

    def raise_verification_failure
      raise Error.new("Unable to verify address.")
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
easypost-2.1.4 lib/easypost/address.rb
easypost-2.1.3 lib/easypost/address.rb
easypost-2.1.2 lib/easypost/address.rb
easypost-2.1.1 lib/easypost/address.rb
easypost-2.0.15 lib/easypost/address.rb