Class: USPS::Address

Inherits:
Struct
  • Object
show all
Defined in:
lib/usps/address.rb

Overview

TODO: Documentation

The USPS API uses a standard where Address2 is the street adress and Address1 is the apartment, suite, etc… I have switched them to match how I see them on an envelope. Additionally they are refered to address and extra_address though both address1 and address2 work. Just remember they are flip flopped based on the USPS documentation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Address

Returns a new instance of Address.



21
22
23
24
25
26
27
# File 'lib/usps/address.rb', line 21

def initialize(options = {}, &block)
  options.each_pair do |k, v|
    self.send("#{k}=", v)
  end

  block.call(self) if block
end

Instance Attribute Details

#address1Object Also known as: address

Returns the value of attribute address1

Returns:

  • (Object)

    the current value of address1



7
8
9
# File 'lib/usps/address.rb', line 7

def address1
  @address1
end

#address2Object Also known as: extra_address

Returns the value of attribute address2

Returns:

  • (Object)

    the current value of address2



7
8
9
# File 'lib/usps/address.rb', line 7

def address2
  @address2
end

#cityObject

Returns the value of attribute city

Returns:

  • (Object)

    the current value of city



7
8
9
# File 'lib/usps/address.rb', line 7

def city
  @city
end

#companyObject Also known as: firm

Returns the value of attribute company

Returns:

  • (Object)

    the current value of company



7
8
9
# File 'lib/usps/address.rb', line 7

def company
  @company
end

#errorObject (readonly)

Returns the value of attribute error.



19
20
21
# File 'lib/usps/address.rb', line 19

def error
  @error
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



7
8
9
# File 'lib/usps/address.rb', line 7

def name
  @name
end

#stateObject

Returns the value of attribute state

Returns:

  • (Object)

    the current value of state



7
8
9
# File 'lib/usps/address.rb', line 7

def state
  @state
end

#zip4Object

Returns the value of attribute zip4

Returns:

  • (Object)

    the current value of zip4



7
8
9
# File 'lib/usps/address.rb', line 7

def zip4
  @zip4
end

#zip5Object

Returns the value of attribute zip5

Returns:

  • (Object)

    the current value of zip5



7
8
9
# File 'lib/usps/address.rb', line 7

def zip5
  @zip5
end

Instance Method Details

#replace(other) ⇒ Object

Similar to Hash#replace, overwrite the values of this object with the other. It will not replace a provided key on the original object that does not exist on the replacing object (such as name with verification requests).

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/usps/address.rb', line 61

def replace(other)
  raise ArgumentError unless other.is_a?(USPS::Address)

  other.each_pair do |key, val|
    # Do not overwrite values that may exist on the original but not on
    # the replacement.
    self[key] = val unless val.nil?
  end

  self
end

#standardizeObject



49
50
51
52
# File 'lib/usps/address.rb', line 49

def standardize
  response = USPS::Request::AddressStandardization.new(self).send!
  response[self]
end

#standardize!Object



54
55
56
# File 'lib/usps/address.rb', line 54

def standardize!
  replace(self.standardize)
end

#valid?Boolean

Check with the USPS if this address can be verified and will in missing fields (such as zip code) if they are available.

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'lib/usps/address.rb', line 40

def valid?
  @error = nil
  standardize
  true
rescue USPS::Error => e
  @error = e
  false
end

#zipObject



29
30
31
# File 'lib/usps/address.rb', line 29

def zip
  zip4 ? "#{zip5}-#{zip4}" : zip5.to_s
end

#zip=(val) ⇒ Object

Sets zip5 and zip4 if given a zip code in the format “99881” or “99881-1234”



34
35
36
# File 'lib/usps/address.rb', line 34

def zip=(val)
  self.zip5, self.zip4 = val.to_s.split('-')
end