Class: UsStreet

Inherits:
Object
  • Object
show all
Defined in:
lib/us_street.rb,
lib/us_street/version.rb

Constant Summary collapse

COMPONENTS =
[:unit, :street_number, :dir_prefix, :street_name, :street_suffix, :dir_suffix, :road_number].freeze
ROAD_SUFFIXES =
YAML.load(File.read(f))
DIRECTIONAL_MAPPINGS =
{
  "n" => "n",
  "e" => "e",
  "s" => "s",
  "w" => "w",
  "north" => "n",
  "east" => "e",
  "south" => "s",
  "west" => "w",
  "nth" => "n",
  "sth" => "s",
  "ne" => "ne",
  "nw" => "nw",
  "se" => "se",
  "sw" => "sw",
  "northeast" => "ne",
  "northwest" => "nw",
  "southeast" => "se",
  "southwest" => "sw",
}
VERSION =
"0.1.4"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(components = {}) ⇒ UsStreet

Returns a new instance of UsStreet.

Parameters:

  • street_number (String)
    • The street number

  • street_name (String)
    • The name of the street including all the ‘ixes

  • components (Hash) (defaults to: {})
    • The components of the street



63
64
65
# File 'lib/us_street.rb', line 63

def initialize(components = {})
  @components = components
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



58
59
60
# File 'lib/us_street.rb', line 58

def components
  @components
end

Class Method Details

.clean(str) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/us_street.rb', line 38

def self.clean(str)
  str.to_s
    .strip                       # 'cuz real data sux
    .gsub(/[\.,:;]/, '')         # remove non-meaningful characters
    .gsub(/[-]+$/, '')           # remove non-meaningful characters on the end
    .gsub(/\(.*?\)/, '')         # remove anything in parentheses
    .gsub(/.#[a-zA-Z0-9]+$/, '') # remove unit numbers on the end
    .gsub(/\s+/, ' ')            # collapse multiple spaces to one
    .strip.downcase.presence
end

.clean_hash(hash) ⇒ Object



49
50
51
# File 'lib/us_street.rb', line 49

def self.clean_hash(hash)
  hash.each_with_object({}.with_indifferent_access) { |(k,v),hsh| hsh[k] = clean(v) }
end

.componentsObject



36
# File 'lib/us_street.rb', line 36

def self.components; COMPONENTS; end

.direction_mapping(direction) ⇒ Object



170
171
172
# File 'lib/us_street.rb', line 170

def self.direction_mapping(direction)
  DIRECTIONAL_MAPPINGS[direction.to_s.downcase] && DIRECTIONAL_MAPPINGS[direction.to_s.downcase].upcase
end

.from_attrs(street_number, dir_prefix, street_name, street_suffix, dir_suffix) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/us_street.rb', line 160

def self.from_attrs(street_number, dir_prefix, street_name, street_suffix, dir_suffix)
  return parse(
    street_name,
    street_number: street_number,
    dir_prefix: dir_prefix,
    street_suffix: street_suffix,
    dir_suffix: dir_suffix
  )
end

.parse(full_street, overrides = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/us_street.rb', line 83

def self.parse(full_street, overrides = {})
  original_full_street = full_street.to_s
  full_street = clean(full_street.to_s).to_s # the cleaner removes unit numbers :(
  overrides = clean_hash(overrides)

  parts = full_street.split(' ')
  sidx, eidx = 0, parts.length - 1
  unit_number = dir_suffix = dir_prefix = street_suffix = street_number = road_number = nil

  # We could have a unit number last. Format '#[a-zA-Z0-9]+' but it's removed by the cleaners so match the original
  if match = original_full_street.match(/#([a-zA-Z0-9]+)$/)
    unit_number = match[1]
  end

  # we may be on a numbered road. Like a country road.
  if parts[eidx] =~ /^\d+$/
    road_number = parts[eidx]
    eidx -= 1
  end

  # strip suffixes starting at the end and working backwards
  # Check for a postdirectional (directional suffix)
  eidx -= 1 if eidx - sidx > 0 and dir_suffix = direction_mapping(parts[eidx])

  # Check for a street suffix. If there's already a street suffix override,
  # don't strip from string unless it's the same suffix.
  # Otherwise "Forest Trail Rd" gets stripped to "Forest Trl"
  if eidx - sidx > 0 and street_suffix_maybe = road_mapping(parts[eidx])
    if overrides[:street_suffix].blank? or street_suffix_maybe == road_mapping(overrides[:street_suffix])
      street_suffix = street_suffix_maybe
      eidx -= 1
    end
  end

  # remove components from the beginning

  # is there a street number at beginning
  if eidx - sidx > 0 and parts[sidx] =~ /^\d+$/
    street_number = parts[sidx]
    sidx += 1
  end

  # is there a predirectional?
  sidx += 1 if eidx - sidx > 0 and dir_prefix    = direction_mapping(parts[sidx])

  # fix weirdo street names and make them consistent
  parts[sidx] = "St" if parts[sidx] =~ /^((st\.?)|(saint))$/i  # catch "Saint John Street"
  parts[sidx] = "Dr" if parts[sidx] =~ /^((dr\.?)|(doctor))$/i  # catch "Dr. Oz Road"

  # Time to build up the output, prefer what was passed in
  # Note: We needed to decompose the street because the street may have had the components put into it
  # by some unsavoury operators :'(
  overrides_unit = overrides[:unit].to_s.try(:match, /([a-zA-Z0-9]+)/).try(:captures).try(:first)
  out = {
    unit: (overrides_unit || unit_number).try(:upcase),
    dir_prefix: direction_mapping(overrides[:dir_prefix]).presence || dir_prefix,
    street_name: overrides[:street_name].presence || parts[sidx..eidx].join(' '),
    street_suffix: road_mapping(overrides[:street_suffix]).presence || street_suffix,
    dir_suffix: direction_mapping(overrides[:dir_suffix]).presence || dir_suffix,
    street_number: overrides[:street_number].presence || street_number,
    road_number: overrides[:road_number].presence || road_number
  }

  # we may have country or co for a country road.
  out[:street_name] = 'co' if out[:street_name] == 'country'

  # perform one last mapping to be sure that we've got the correct 'ixes.
  out[:dir_prefix] = out[:dir_prefix].try(:upcase)
  out[:dir_suffix] = out[:dir_suffix].try(:upcase)
  out[:street_suffix] = road_mapping(out[:street_suffix]).try(:titleize)
  out[:street_name] = out[:street_name].try(:titleize)

  out[:street_name] = out[:street_name].to_i.ordinalize if out[:street_name].present? && out[:street_name] =~ /^\d+$/

  new out
end

.road_mapping(suffix) ⇒ Object



174
175
176
# File 'lib/us_street.rb', line 174

def self.road_mapping(suffix)
  ROAD_SUFFIXES[suffix.to_s.downcase] && ROAD_SUFFIXES[suffix.to_s.downcase].upcase
end

.trim(*address_components) ⇒ Object



178
179
180
# File 'lib/us_street.rb', line 178

def self.trim(*address_components)
  address_components.compact.join(' ').gsub(/\s+/, ' ').strip
end

Instance Method Details

#==(other) ⇒ Object



182
183
184
# File 'lib/us_street.rb', line 182

def ==(other)
  other.respond_to?(:full_street) && self.full_street == other.full_street
end

#attributesObject



79
80
81
# File 'lib/us_street.rb', line 79

def attributes
  @attributes ||= @components.merge(street: street, full_street: full_street, display: display)
end

#displayObject



75
76
77
# File 'lib/us_street.rb', line 75

def display
  @display ||= unit.present? ? UsStreet.trim(full_street, "##{unit}") : full_street
end

#full_streetObject



71
72
73
# File 'lib/us_street.rb', line 71

def full_street
  @full_street ||= UsStreet.trim(street_number, street)
end

#streetObject



67
68
69
# File 'lib/us_street.rb', line 67

def street
  @street ||= UsStreet.trim(dir_prefix, street_name, street_suffix, dir_suffix, road_number)
end