Sha256: 4a270721970e0e113082dba5b4ef6eb4866cdaecd8eb802524c554fe67805dc7

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

# Address objects are used to represent people, places, and organizations in a number of contexts.
class EasyPost::Address < EasyPost::Resource
  attr_accessor :message # Backwards compatibility

  # Create an Address.
  def self.create(params = {}, api_key = nil)
    url = self.url

    address = params.reject { |k, _| [:verify, :verify_strict].include?(k) }

    if params[:verify] || params[:verify_strict]
      verify = params[:verify] || []
      verify_strict = params[:verify_strict] || []

      url += '?'
      verify.each do |verification|
        url += "verify[]=#{verification}&"
      end
      verify_strict.each do |verification|
        url += "verify_strict[]=#{verification}&"
      end
    end

    response = EasyPost.make_request(:post, url, api_key, { address: address })
    EasyPost::Util.convert_to_easypost_object(response, api_key)
  end

  # Create and verify an Address in one call.
  def self.create_and_verify(params = {}, carrier = nil, api_key = nil)
    wrapped_params = {}
    wrapped_params[class_name.to_sym] = params
    wrapped_params[:carrier] = carrier
    response = EasyPost.make_request(:post, "#{url}/create_and_verify", api_key, wrapped_params)

    raise EasyPost::Error.new('Unable to verify address.') unless response.key?('address')

    if response.key?('message')
      response['address']['message'] = response['message']
    end

    EasyPost::Util.convert_to_easypost_object(response['address'], api_key)
  end

  # Verify an Address.
  def verify
    begin
      response = EasyPost.make_request(:get, "#{url}/verify", @api_key)
    rescue StandardError
      raise EasyPost::Error.new('Unable to verify address.')
    end

    raise EasyPost::Error.new('Unable to verify address.') unless response.key?('address')

    EasyPost::Util.convert_to_easypost_object(response['address'], api_key)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
easypost-4.1.2 lib/easypost/address.rb
easypost-4.1.1 lib/easypost/address.rb
easypost-4.1.0 lib/easypost/address.rb
easypost-4.0.0 lib/easypost/address.rb