Class: EasyPost::Address

Inherits:
Resource show all
Defined in:
lib/easypost/address.rb

Overview

Address objects are used to represent people, places, and organizations in a number of contexts.

Instance Attribute Summary collapse

Attributes inherited from EasyPostObject

#api_key, #name, #parent, #unsaved_values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

all, class_name, #delete, each, #refresh, retrieve, #save, url, #url

Methods inherited from EasyPostObject

#[], #[]=, #as_json, construct_from, #deconstruct_keys, #each, #id, #id=, #initialize, #inspect, #keys, #refresh_from, #to_hash, #to_json, #to_s, #values

Constructor Details

This class inherits a constructor from EasyPost::EasyPostObject

Instance Attribute Details

#messageObject

Backwards compatibility



5
6
7
# File 'lib/easypost/address.rb', line 5

def message
  @message
end

Class Method Details

.create(params = {}, api_key = nil) ⇒ Object

Create an Address.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/easypost/address.rb', line 8

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

  wrapped_params = { address: address }

  if params[:verify]
    wrapped_params[:verify] = params[:verify]
  end

  if params[:verify_strict]
    wrapped_params[:verify_strict] = params[:verify_strict]
  end

  response = EasyPost.make_request(:post, url, api_key, wrapped_params)
  EasyPost::Util.convert_to_easypost_object(response, api_key)
end

.create_and_verify(params = {}, api_key = nil) ⇒ Object

Create and verify an Address in one call.

Raises:



26
27
28
29
30
31
32
33
34
# File 'lib/easypost/address.rb', line 26

def self.create_and_verify(params = {}, api_key = nil)
  wrapped_params = {}
  wrapped_params[class_name.to_sym] = params
  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')

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

Instance Method Details

#verifyObject

Verify an Address.

Raises:



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/easypost/address.rb', line 37

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