Class: USPS::Address
- Inherits:
-
Struct
- Object
- Struct
- USPS::Address
- 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
-
#address1 ⇒ Object
(also: #address)
Returns the value of attribute address1.
-
#address2 ⇒ Object
(also: #extra_address)
Returns the value of attribute address2.
-
#city ⇒ Object
Returns the value of attribute city.
-
#company ⇒ Object
(also: #firm)
Returns the value of attribute company.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#name ⇒ Object
Returns the value of attribute name.
-
#return_text ⇒ Object
Returns the value of attribute return_text.
-
#state ⇒ Object
Returns the value of attribute state.
-
#zip4 ⇒ Object
Returns the value of attribute zip4.
-
#zip5 ⇒ Object
Returns the value of attribute zip5.
Instance Method Summary collapse
-
#initialize(options = {}, &block) ⇒ Address
constructor
A new instance of Address.
-
#replace(other) ⇒ Object
Similar to Hash#replace, overwrite the values of this object with the other.
- #standardize ⇒ Object
- #standardize! ⇒ Object
-
#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.
- #zip ⇒ Object
-
#zip=(val) ⇒ Object
Sets zip5 and zip4 if given a zip code in the format “99881” or “99881-1234”.
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( = {}, &block) .each_pair do |k, v| self.send("#{k}=", v) end block.call(self) if block end |
Instance Attribute Details
#address1 ⇒ Object Also known as: address
Returns the value of attribute address1
7 8 9 |
# File 'lib/usps/address.rb', line 7 def address1 @address1 end |
#address2 ⇒ Object Also known as: extra_address
Returns the value of attribute address2
7 8 9 |
# File 'lib/usps/address.rb', line 7 def address2 @address2 end |
#city ⇒ Object
Returns the value of attribute city
7 8 9 |
# File 'lib/usps/address.rb', line 7 def city @city end |
#company ⇒ Object Also known as: firm
Returns the value of attribute company
7 8 9 |
# File 'lib/usps/address.rb', line 7 def company @company end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
19 20 21 |
# File 'lib/usps/address.rb', line 19 def error @error end |
#name ⇒ Object
Returns the value of attribute name
7 8 9 |
# File 'lib/usps/address.rb', line 7 def name @name end |
#return_text ⇒ Object
Returns the value of attribute return_text
7 8 9 |
# File 'lib/usps/address.rb', line 7 def return_text @return_text end |
#state ⇒ Object
Returns the value of attribute state
7 8 9 |
# File 'lib/usps/address.rb', line 7 def state @state end |
#zip4 ⇒ Object
Returns the value of attribute zip4
7 8 9 |
# File 'lib/usps/address.rb', line 7 def zip4 @zip4 end |
#zip5 ⇒ Object
Returns the value of attribute 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).
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 |
#standardize ⇒ Object
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.
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 |
#zip ⇒ Object
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 |