Class: StreetAddress::US::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/street_address.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Address

Returns a new instance of Address.



834
835
836
837
838
# File 'lib/street_address.rb', line 834

def initialize(args)
  args.each do |attr, val|
    public_send("#{attr}=", val)
  end
end

Instance Attribute Details

#city ⇒ Object

Returns the value of attribute city.



815
816
817
# File 'lib/street_address.rb', line 815

def city
  @city
end

#number ⇒ Object

Returns the value of attribute number.



815
816
817
# File 'lib/street_address.rb', line 815

def number
  @number
end

#postal_code ⇒ Object

Returns the value of attribute postal_code.



815
816
817
# File 'lib/street_address.rb', line 815

def postal_code
  @postal_code
end

#postal_code_ext ⇒ Object

Returns the value of attribute postal_code_ext.



815
816
817
# File 'lib/street_address.rb', line 815

def postal_code_ext
  @postal_code_ext
end

#prefix ⇒ Object

Returns the value of attribute prefix.



815
816
817
# File 'lib/street_address.rb', line 815

def prefix
  @prefix
end

#prefix2 ⇒ Object

Returns the value of attribute prefix2.



815
816
817
# File 'lib/street_address.rb', line 815

def prefix2
  @prefix2
end

#redundant_street_type ⇒ Object

Returns the value of attribute redundant_street_type.



815
816
817
# File 'lib/street_address.rb', line 815

def redundant_street_type
  @redundant_street_type
end

#state ⇒ Object

Returns the value of attribute state.



815
816
817
# File 'lib/street_address.rb', line 815

def state
  @state
end

#street ⇒ Object

Returns the value of attribute street.



815
816
817
# File 'lib/street_address.rb', line 815

def street
  @street
end

#street2 ⇒ Object

Returns the value of attribute street2.



815
816
817
# File 'lib/street_address.rb', line 815

def street2
  @street2
end

#street_type ⇒ Object

Returns the value of attribute street_type.



815
816
817
# File 'lib/street_address.rb', line 815

def street_type
  @street_type
end

#street_type2 ⇒ Object

Returns the value of attribute street_type2.



815
816
817
# File 'lib/street_address.rb', line 815

def street_type2
  @street_type2
end

#suffix ⇒ Object

Returns the value of attribute suffix.



815
816
817
# File 'lib/street_address.rb', line 815

def suffix
  @suffix
end

#suffix2 ⇒ Object

Returns the value of attribute suffix2.



815
816
817
# File 'lib/street_address.rb', line 815

def suffix2
  @suffix2
end

#unit ⇒ Object

Returns the value of attribute unit.



815
816
817
# File 'lib/street_address.rb', line 815

def unit
  @unit
end

#unit_prefix ⇒ Object

Returns the value of attribute unit_prefix.



815
816
817
# File 'lib/street_address.rb', line 815

def unit_prefix
  @unit_prefix
end

Instance Method Details

#==(other) ⇒ Object



922
923
924
925
# File 'lib/street_address.rb', line 922

def ==(other)
  return false if other.nil?
  to_s == other.to_s
end

#full_postal_code ⇒ Object



841
842
843
844
# File 'lib/street_address.rb', line 841

def full_postal_code
  return nil unless self.postal_code
  self.postal_code_ext ? "#{postal_code}-#{postal_code_ext}" : self.postal_code
end

#intersection? ⇒ Boolean

Returns:

  • (Boolean)


858
859
860
# File 'lib/street_address.rb', line 858

def intersection?
  !street2.nil?
end

#line1 ⇒ Object



863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
# File 'lib/street_address.rb', line 863

def line1
  parts = []
  if intersection?
    parts << prefix       if prefix
    parts << street
    parts << street_type  if street_type
    parts << suffix       if suffix
    parts << "and"
    parts << prefix2      if prefix2
    parts << street2
    parts << street_type2 if street_type2
    parts << suffix2      if suffix2
  else
    parts << number
    parts << prefix if prefix
    parts << street if street
    parts << street_type if street_type && !redundant_street_type
    parts << suffix if suffix
    parts << unit_prefix if unit_prefix
    # follow USPS guidelines: http://pe.usps.gov/cpim/ftp/pubs/Pub28/pub28.pdf pg28
    parts << (unit_prefix ? unit : "# #{unit}") if unit
  end
  parts.join(" ").strip
end

#line2 ⇒ Object



889
890
891
892
893
894
895
896
897
898
899
# File 'lib/street_address.rb', line 889

def line2
  parts = []
  parts << city  if city
  parts << state if state
  s = parts.join(", ")
  if postal_code
    s = "#{s} #{postal_code}"
    s = "#{s}-#{postal_code_ext}" if postal_code_ext
  end
  s.strip
end

#state_fips ⇒ Object



847
848
849
# File 'lib/street_address.rb', line 847

def state_fips
  StreetAddress::US::FIPS_STATES[state]
end

#state_name ⇒ Object



852
853
854
855
# File 'lib/street_address.rb', line 852

def state_name
  name = StreetAddress::US::STATE_NAMES[state]
  name&.split&.map(&:capitalize)&.join(" ")
end

#to_h ⇒ Object



914
915
916
917
918
919
920
# File 'lib/street_address.rb', line 914

def to_h
  self.instance_variables.each_with_object({}) do |var_name, hash|
    var_value = self.instance_variable_get(var_name)
    hash_name = var_name[1..-1].to_sym
    hash[hash_name] = var_value
  end
end

#to_s(format = :default) ⇒ Object



902
903
904
905
906
907
908
909
910
911
# File 'lib/street_address.rb', line 902

def to_s(format = :default)
  case format
  when :line1
    line1
  when :line2
    line2
  else
    [line1, line2].reject(&:empty?).join(", ")
  end
end