class DcAddressParser::Address
Constants
- NUMBER_REGEX
- NUMBER_SUFFIX_REGEX
- PARTS
- QUADRANT_REGEX
- REQUIRED_PARTS
- STREET_NAME_REGEX
- STREET_TYPE_ABV_REGEX
- STREET_TYPE_REGEX
Attributes
raw_address[R]
Public Class Methods
new(address_or_hash)
click to toggle source
# File lib/dc_address_parser/address.rb, line 17 def initialize(address_or_hash) if address_or_hash.class == Hash address_or_hash = PARTS.clone.map { |part| address_or_hash[part] }.join(" ") end @raw_address = @address = address_or_hash normalize! REQUIRED_PARTS.each do |part| raise InvalidAddress, "#{part.to_s.sub("_", " ")} is missing" if send(part).nil? end end
Public Instance Methods
lookup()
click to toggle source
# File lib/dc_address_parser/address.rb, line 92 def lookup DcAddressLookup.lookup to_s end
number()
click to toggle source
# File lib/dc_address_parser/address.rb, line 29 def number @number ||= match(NUMBER_REGEX).to_i end
number_suffix()
click to toggle source
# File lib/dc_address_parser/address.rb, line 33 def number_suffix @number_suffix ||= match(/#{number}\s#{NUMBER_SUFFIX_REGEX}/) end
Also aliased as: suffix
quadrant()
click to toggle source
# File lib/dc_address_parser/address.rb, line 57 def quadrant @quadrant ||= match QUADRANT_REGEX end
Also aliased as: quad
street_name()
click to toggle source
# File lib/dc_address_parser/address.rb, line 38 def street_name @street_name ||= begin street_name = match( /#{number}(-?#{unit_number}|\s#{Regexp.escape number_suffix.to_s})? \s#{STREET_NAME_REGEX}\s(?=#{STREET_TYPE_REGEX})/x, 2) if street_name =~ /\A[0-9]+\z/ street_name = ActiveSupport::Inflector.ordinalize(street_name).upcase end street_name end end
Also aliased as: street
street_type()
click to toggle source
# File lib/dc_address_parser/address.rb, line 53 def street_type @street_type ||= match(STREET_TYPE_REGEX) || "STREET" end
to_h()
click to toggle source
# File lib/dc_address_parser/address.rb, line 76 def to_h hash = {} PARTS.each { |part| hash[part] = send(part) } hash.merge({city: DcAddressParser::CITY}) end
to_s(include_city=false)
click to toggle source
# File lib/dc_address_parser/address.rb, line 82 def to_s(include_city=false) parts = to_h if include_city parts[:quadrant] << "," else parts.delete(:city) end parts.values.compact.join(" ") end
unit_number()
click to toggle source
# File lib/dc_address_parser/address.rb, line 62 def unit_number @unit_number ||= begin unit_number = match(/\A(\d+)(-|–)?([A-Z])\b/) || match(/\s(UNIT\s|APT\s|#)([A-Z0-9]+)(\s|\z)/, 2) || match(/#{quadrant}\s([A-Z0-9]+)\z/) if unit_number =~ /\A\d+\z/ unit_number.to_i else unit_number end end end
Also aliased as: unit
Private Instance Methods
match(regex, number=nil)
click to toggle source
# File lib/dc_address_parser/address.rb, line 165 def match(regex, number=nil) matches = @address.match(regex) return unless matches return matches[number] if number matches.to_a.last end
normalize!()
click to toggle source
# File lib/dc_address_parser/address.rb, line 98 def normalize! normalize_whitespace normalize_case normalize_ranges normalize_quadrant normalize_street_type normalize_rear normalize_space normalize_mlk normalize_directions normalize_mt split end
normalize_case()
click to toggle source
# File lib/dc_address_parser/address.rb, line 116 def normalize_case @address = @address.upcase end
normalize_directions()
click to toggle source
# File lib/dc_address_parser/address.rb, line 150 def normalize_directions regex = /\b(#{Regexp.union DcAddressParser::DIRECTIONS.values})(?=\s+|\.)/ @address.gsub!(regex, DcAddressParser::DIRECTIONS.invert) end
normalize_mlk()
click to toggle source
# File lib/dc_address_parser/address.rb, line 145 def normalize_mlk @address.gsub!(/\bM\.?L\.? KING\b/, "MARTIN LUTHER KING") @address.gsub!(/\bJR\./, "JR") end
normalize_mt()
click to toggle source
# File lib/dc_address_parser/address.rb, line 155 def normalize_mt @address.gsub!(/\bMT\b/, "MOUNT") end
normalize_quadrant()
click to toggle source
# File lib/dc_address_parser/address.rb, line 125 def normalize_quadrant @address.gsub!(/([NS])\.([EW])\.?/, '\1\2') @address.gsub!(/, ([NS][EW])/, ' \1') end
normalize_ranges()
click to toggle source
# File lib/dc_address_parser/address.rb, line 120 def normalize_ranges @address.gsub!(/\A(\d+)\s?(-|–|&)\s?\d+/, '\1') @address.gsub!(/(\d+), \d+,? and \d+/i, '\1') end
normalize_rear()
click to toggle source
# File lib/dc_address_parser/address.rb, line 134 def normalize_rear regex = /\AREAR OF (\d+)/ return unless @address =~ regex @address.gsub!(/\AREAR OF (\d+)/, '\1') @address << " REAR" end
normalize_space()
click to toggle source
# File lib/dc_address_parser/address.rb, line 141 def normalize_space @address.gsub!(/\bSPACE\b/, "UNIT") end
normalize_street_type()
click to toggle source
# File lib/dc_address_parser/address.rb, line 130 def normalize_street_type @address.gsub!(STREET_TYPE_ABV_REGEX, DcAddressParser::STREET_TYPES.invert) end
normalize_whitespace()
click to toggle source
# File lib/dc_address_parser/address.rb, line 112 def normalize_whitespace @address = @address.strip.squeeze("\s").squeeze("'") end
split()
click to toggle source
# File lib/dc_address_parser/address.rb, line 159 def split @address = @address.split(";").reject { |s| s.empty? }.first.to_s @address = @address.split(/\bAND\b/).first.to_s.strip @address = @address.split(/\b([NS][EW]),/)[0..1].join end