class StreetAddress::US
Constants
- DIRECTIONAL
- DIRECTION_CODES
- FIPS_STATES
- STATE_CODES
- STATE_FIPS
- STATE_NAMES
- STREET_TYPES
- STREET_TYPES_LIST
- VERSION
Attributes
address_regexp[RW]
city_and_state_regexp[RW]
corner_regexp[RW]
direct_regexp[RW]
fraction_regexp[RW]
informal_address_regexp[RW]
number_regexp[RW]
place_regexp[RW]
state_regexp[RW]
street_regexp[RW]
street_type_regexp[RW]
unit_regexp[RW]
zip_regexp[RW]
Public Class Methods
parse(location, args = {})
click to toggle source
# File lib/street_address.rb, line 670 def parse(location, args = {}) if Regexp.new(corner_regexp, Regexp::IGNORECASE).match(location) parse_intersection(location) elsif args[:informal] parse_address(location) || parse_informal_address(location) else parse_address(location); end end
parse_address(addr)
click to toggle source
parses only an address and returns an instance of StreetAddress::US::Address or nil if the address cannot be parsed
example¶ ↑
address = StreetAddress::US.parse('1600 Pennsylvania Ave Washington, DC 20006') assert !address.intersection?
# File lib/street_address.rb, line 726 def parse_address(addr) regex = Regexp.new(address_regexp, Regexp::IGNORECASE + Regexp::EXTENDED) return unless match = regex.match(addr) normalize_address( StreetAddress::US::Address.new( :number => match[1], :street => match[5] || match[10] || match[2], :street_type => match[6] || match[3], :unit => match[15], :unit_prefix => match[13] || match[14], :suffix => match[7] || match[12], :prefix => match[4], :city => match[16], :state => match[17], :postal_code => match[18], :postal_code_ext => match[19] ) ) end
parse_informal_address(addr)
click to toggle source
# File lib/street_address.rb, line 748 def parse_informal_address(addr) regex = Regexp.new(informal_address_regexp, Regexp::IGNORECASE + Regexp::EXTENDED) return unless match = regex.match(addr) normalize_address( StreetAddress::US::Address.new( :number => match[4], :street => match[8] || match[13] || match[5], :street_type => match[9] || match[6], :unit => match[18] || match[3], :unit_prefix => match[16] || match[17] || match[1] || match[2], :suffix => match[10] || match[15], :prefix => match[7], :city => match[19], :state => match[20], :postal_code => match[21], :postal_code_ext => match[22] ) ) end
parse_intersection(inter)
click to toggle source
parses only an intersection and returns an instance of StreetAddress::US::Address or nil if the intersection cannot be parsed
example¶ ↑
address = StreetAddress::US.parse('Hollywood & Vine, Los Angeles, CA') assert address.intersection?
# File lib/street_address.rb, line 689 def parse_intersection(inter) regex = Regexp.new( '\A\W*' + street_regexp + '\W*? \s+' + corner_regexp + '\s+' + street_regexp + '\W+' + place_regexp + '\W*\Z', Regexp::IGNORECASE + Regexp::EXTENDED ) return unless match = regex.match(inter) normalize_address( StreetAddress::US::Address.new( :street => match[4] || match[9], :street_type => match[5], :suffix => match[6], :prefix => match[3], :street2 => match[15] || match[20], :street_type2 => match[16], :suffix2 => match[17], :prefix2 => match[14], :city => match[23], :state => match[24], :postal_code => match[25] ) ) end
Private Class Methods
normalize_address(addr)
click to toggle source
# File lib/street_address.rb, line 771 def normalize_address(addr) addr.state = normalize_state(addr.state) unless addr.state.nil? addr.street_type = normalize_street_type(addr.street_type) unless addr.street_type.nil? addr.prefix = normalize_directional(addr.prefix) unless addr.prefix.nil? addr.suffix = normalize_directional(addr.suffix) unless addr.suffix.nil? addr.street.gsub!(/\b([a-z])/) {|wd| wd.capitalize} unless addr.street.nil? addr.street_type2 = normalize_street_type(addr.street_type2) unless addr.street_type2.nil? addr.prefix2 = normalize_directional(addr.prefix2) unless addr.prefix2.nil? addr.suffix2 = normalize_directional(addr.suffix2) unless addr.suffix2.nil? addr.street2.gsub!(/\b([a-z])/) {|wd| wd.capitalize} unless addr.street2.nil? addr.city.gsub!(/\b([a-z])/) {|wd| wd.capitalize} unless addr.city.nil? addr.unit_prefix.capitalize! unless addr.unit_prefix.nil? return addr end
normalize_directional(dir)
click to toggle source
# File lib/street_address.rb, line 800 def normalize_directional(dir) if dir.length < 3 dir.upcase else DIRECTIONAL[dir.downcase] end end
normalize_state(state)
click to toggle source
# File lib/street_address.rb, line 786 def normalize_state(state) if state.length < 3 state.upcase else STATE_CODES[state.downcase] end end
normalize_street_type(s_type)
click to toggle source
# File lib/street_address.rb, line 794 def normalize_street_type(s_type) s_type.downcase! s_type = STREET_TYPES[s_type] || s_type if STREET_TYPES_LIST[s_type] s_type.capitalize end