TempStreetAddress 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Y2IzZTdiODMxNjhkZjQ4MjkyYTE4YmNhOGYyOGFhZjRkNjJiM2FiMQ==
5
+ data.tar.gz: !binary |-
6
+ NGQ3OTAzNTRhNzNjNjU2NjQwZjllNjE1MGNmN2IwNjAxNTRhMDYwNg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ Yzc3ZTE5NmJmZjU1NmRiZjNlOThkZjJiMzMyMzAwNWM4MTk2YmI4YTdlNWU3
10
+ ZWQ0MTk0YWNiNDY2Y2VlYWU1MjBhYzkzZmY0MjJkNzA5NmE1M2E1ZWRiMGYw
11
+ YjhjOGMzZjRhZDA4YjdkMTIzMWRmM2MxYjkzMDVmMGQ4Y2JmMTI=
12
+ data.tar.gz: !binary |-
13
+ N2I4ZDRiNTEyNWUwMTMyZDMzYTQ5OTk5M2ViMmEyNDc3MDM5N2E1NGYyMDQx
14
+ ZTczOTU0NDNmNzY4YzQzZGUxYWRiYWFlZTQ3NDNlNzYyNGYyM2ZhYWFkNTQw
15
+ ZjU2ZWE1ZDQ1MGIzNDU4NmQ5ZmE2YTBlM2RmMTI0NzAwNGZjNDI=
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2011 Derrek Long
2
+
3
+ The MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ == DESCRIPTION:
2
+
3
+ Parses one line street addresses and returns a normalized address object.
4
+
5
+ This is a near direct port of the of the perl module
6
+ Geo::StreetAddress::US originally written by Schuyler D. Erle.
7
+ For more information see
8
+ http://search.cpan.org/~sderle/Geo-StreetAddress-US-0.99/
9
+
10
+ == SYNOPSIS:
11
+
12
+ Parse United States addresses.
13
+
14
+ === Basic Usage:
15
+ require 'street_address'
16
+ address = StreetAddress::US.parse("1600 Pennsylvania Ave, Washington, DC, 20500")
17
+ address.street # Pennsylvania
18
+ address.number # 1600
19
+ address.postal_code # 20500
20
+ address.city # Washington
21
+ address.state # DC
22
+ address.state_name # District of columbia
23
+ address.street_type # Ave
24
+ address.intersection? # false
25
+ address.to_s # "1600 Pennsylvania Ave, Washington, DC 20500"
26
+ address.to_s(:line1) # 1600 Pennsylvania Ave
27
+ StreetAddress::US.parse("1600 Pennsylvania Ave") # nil not a full address
28
+
29
+ === Informal/Loose Parsing
30
+ address = StreetAddress::US.parse("1600 Pennsylvania Avenue", :informal => true)
31
+ address.number # 1600
32
+ address.street # Pennsylvania
33
+ address.street_type # Ave
34
+ address.to_s # 1600 Pennsylvania Ave
35
+
36
+ === Zip+4
37
+ address = StreetAddress::US.parse("5904 Richmond Hwy Ste 340 Alexandria VA 22303-1864")
38
+ address.postal_code_ext # 1846
39
+
40
+ address = StreetAddress::US.parse("5904 Richmond Hwy Ste 340 Alexandria VA 223031864")
41
+ address.postal_code_ext # 1846
42
+
43
+ === Gemfile:
44
+ To use from your Gemfile:
45
+ gem 'StreetAddress', '1.0.3', :require => "street_address"
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,898 @@
1
+ =begin rdoc
2
+
3
+ === Usage:
4
+ StreetAddress::US.parse("1600 Pennsylvania Ave, washington, dc")
5
+
6
+ === Valid Address Formats
7
+
8
+ 1600 Pennsylvania Ave Washington DC 20006
9
+ 1600 Pennsylvania Ave #400, Washington, DC, 20006
10
+ 1600 Pennsylvania Ave Washington, DC
11
+ 1600 Pennsylvania Ave #400 Washington DC
12
+ 1600 Pennsylvania Ave, 20006
13
+ 1600 Pennsylvania Ave #400, 20006
14
+ 1600 Pennsylvania Ave 20006
15
+ 1600 Pennsylvania Ave #400 20006
16
+
17
+ === Valid Intersection Formats
18
+
19
+ Hollywood & Vine, Los Angeles, CA
20
+ Hollywood Blvd and Vine St, Los Angeles, CA
21
+ Mission Street at Valencia Street, San Francisco, CA
22
+ Hollywood & Vine, Los Angeles, CA, 90028
23
+ Hollywood Blvd and Vine St, Los Angeles, CA, 90028
24
+ Mission Street at Valencia Street, San Francisco, CA, 90028
25
+
26
+ ==== License
27
+
28
+ Copyright (c) 2007 Riderway (Derrek Long, Nicholas Schlueter)
29
+
30
+ Permission is hereby granted, free of charge, to any person obtaining
31
+ a copy of this software and associated documentation files (the
32
+ "Software"), to deal in the Software without restriction, including
33
+ without limitation the rights to use, copy, modify, merge, publish,
34
+ distribute, sublicense, and/or sell copies of the Software, and to
35
+ permit persons to whom the Software is furnished to do so, subject to
36
+ the following conditions:
37
+
38
+ The above copyright notice and this permission notice shall be
39
+ included in all copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
42
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
44
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
45
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
46
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
47
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48
+
49
+ ==== Notes
50
+ If parts of the address are omitted from the original string
51
+ the accessor will be nil in StreetAddress::US::Address.
52
+
53
+ Example:
54
+ address = StreetAddress::US.parse("1600 Pennsylvania Ave, washington, dc")
55
+ assert address.postal_code.nil?
56
+
57
+ ==== Acknowledgements
58
+
59
+ This gem is a near direct port of the perl module Geo::StreetAddress::US
60
+ originally written by Schuyler D. Erle. For more information see
61
+ http://search.cpan.org/~sderle/Geo-StreetAddress-US-0.99/
62
+
63
+ =end
64
+
65
+ module StreetAddress
66
+ class US
67
+ VERSION = '1.0.5'
68
+ DIRECTIONAL = {
69
+ "north" => "N",
70
+ "northeast" => "NE",
71
+ "east" => "E",
72
+ "southeast" => "SE",
73
+ "south" => "S",
74
+ "southwest" => "SW",
75
+ "west" => "W",
76
+ "northwest" => "NW"
77
+ }
78
+ DIRECTION_CODES = DIRECTIONAL.invert
79
+
80
+ STREET_TYPES = {
81
+ "allee" => "aly",
82
+ "alley" => "aly",
83
+ "ally" => "aly",
84
+ "anex" => "anx",
85
+ "annex" => "anx",
86
+ "annx" => "anx",
87
+ "arcade" => "arc",
88
+ "av" => "ave",
89
+ "aven" => "ave",
90
+ "avenu" => "ave",
91
+ "avenue" => "ave",
92
+ "avn" => "ave",
93
+ "avnue" => "ave",
94
+ "bayoo" => "byu",
95
+ "bayou" => "byu",
96
+ "beach" => "bch",
97
+ "bend" => "bnd",
98
+ "bluf" => "blf",
99
+ "bluff" => "blf",
100
+ "bluffs" => "blfs",
101
+ "bot" => "btm",
102
+ "bottm" => "btm",
103
+ "bottom" => "btm",
104
+ "boul" => "blvd",
105
+ "boulevard" => "blvd",
106
+ "boulv" => "blvd",
107
+ "branch" => "br",
108
+ "brdge" => "brg",
109
+ "bridge" => "brg",
110
+ "brnch" => "br",
111
+ "brook" => "brk",
112
+ "brooks" => "brks",
113
+ "burg" => "bg",
114
+ "burgs" => "bgs",
115
+ "bypa" => "byp",
116
+ "bypas" => "byp",
117
+ "bypass" => "byp",
118
+ "byps" => "byp",
119
+ "camp" => "cp",
120
+ "canyn" => "cyn",
121
+ "canyon" => "cyn",
122
+ "cape" => "cpe",
123
+ "causeway" => "cswy",
124
+ "causway" => "cswy",
125
+ "cen" => "ctr",
126
+ "cent" => "ctr",
127
+ "center" => "ctr",
128
+ "centers" => "ctrs",
129
+ "centr" => "ctr",
130
+ "centre" => "ctr",
131
+ "circ" => "cir",
132
+ "circl" => "cir",
133
+ "circle" => "cir",
134
+ "circles" => "cirs",
135
+ "ck" => "crk",
136
+ "cliff" => "clf",
137
+ "cliffs" => "clfs",
138
+ "club" => "clb",
139
+ "cmp" => "cp",
140
+ "cnter" => "ctr",
141
+ "cntr" => "ctr",
142
+ "cnyn" => "cyn",
143
+ "common" => "cmn",
144
+ "corner" => "cor",
145
+ "corners" => "cors",
146
+ "course" => "crse",
147
+ "court" => "ct",
148
+ "courts" => "cts",
149
+ "cove" => "cv",
150
+ "coves" => "cvs",
151
+ "cr" => "crk",
152
+ "crcl" => "cir",
153
+ "crcle" => "cir",
154
+ "crecent" => "cres",
155
+ "creek" => "crk",
156
+ "crescent" => "cres",
157
+ "cresent" => "cres",
158
+ "crest" => "crst",
159
+ "crossing" => "xing",
160
+ "crossroad" => "xrd",
161
+ "crscnt" => "cres",
162
+ "crsent" => "cres",
163
+ "crsnt" => "cres",
164
+ "crssing" => "xing",
165
+ "crssng" => "xing",
166
+ "crt" => "ct",
167
+ "curve" => "curv",
168
+ "dale" => "dl",
169
+ "dam" => "dm",
170
+ "div" => "dv",
171
+ "divide" => "dv",
172
+ "driv" => "dr",
173
+ "drive" => "dr",
174
+ "drives" => "drs",
175
+ "drv" => "dr",
176
+ "dvd" => "dv",
177
+ "estate" => "est",
178
+ "estates" => "ests",
179
+ "exp" => "expy",
180
+ "expr" => "expy",
181
+ "express" => "expy",
182
+ "expressway" => "expy",
183
+ "expw" => "expy",
184
+ "extension" => "ext",
185
+ "extensions" => "exts",
186
+ "extn" => "ext",
187
+ "extnsn" => "ext",
188
+ "falls" => "fls",
189
+ "ferry" => "fry",
190
+ "field" => "fld",
191
+ "fields" => "flds",
192
+ "flat" => "flt",
193
+ "flats" => "flts",
194
+ "ford" => "frd",
195
+ "fords" => "frds",
196
+ "forest" => "frst",
197
+ "forests" => "frst",
198
+ "forg" => "frg",
199
+ "forge" => "frg",
200
+ "forges" => "frgs",
201
+ "fork" => "frk",
202
+ "forks" => "frks",
203
+ "fort" => "ft",
204
+ "freeway" => "fwy",
205
+ "freewy" => "fwy",
206
+ "frry" => "fry",
207
+ "frt" => "ft",
208
+ "frway" => "fwy",
209
+ "frwy" => "fwy",
210
+ "garden" => "gdn",
211
+ "gardens" => "gdns",
212
+ "gardn" => "gdn",
213
+ "gateway" => "gtwy",
214
+ "gatewy" => "gtwy",
215
+ "gatway" => "gtwy",
216
+ "glen" => "gln",
217
+ "glens" => "glns",
218
+ "grden" => "gdn",
219
+ "grdn" => "gdn",
220
+ "grdns" => "gdns",
221
+ "green" => "grn",
222
+ "greens" => "grns",
223
+ "grov" => "grv",
224
+ "grove" => "grv",
225
+ "groves" => "grvs",
226
+ "gtway" => "gtwy",
227
+ "harb" => "hbr",
228
+ "harbor" => "hbr",
229
+ "harbors" => "hbrs",
230
+ "harbr" => "hbr",
231
+ "haven" => "hvn",
232
+ "havn" => "hvn",
233
+ "height" => "hts",
234
+ "heights" => "hts",
235
+ "hgts" => "hts",
236
+ "highway" => "hwy",
237
+ "highwy" => "hwy",
238
+ "hill" => "hl",
239
+ "hills" => "hls",
240
+ "hiway" => "hwy",
241
+ "hiwy" => "hwy",
242
+ "hllw" => "holw",
243
+ "hollow" => "holw",
244
+ "hollows" => "holw",
245
+ "holws" => "holw",
246
+ "hrbor" => "hbr",
247
+ "ht" => "hts",
248
+ "hway" => "hwy",
249
+ "inlet" => "inlt",
250
+ "island" => "is",
251
+ "islands" => "iss",
252
+ "isles" => "isle",
253
+ "islnd" => "is",
254
+ "islnds" => "iss",
255
+ "jction" => "jct",
256
+ "jctn" => "jct",
257
+ "jctns" => "jcts",
258
+ "junction" => "jct",
259
+ "junctions" => "jcts",
260
+ "junctn" => "jct",
261
+ "juncton" => "jct",
262
+ "key" => "ky",
263
+ "keys" => "kys",
264
+ "knol" => "knl",
265
+ "knoll" => "knl",
266
+ "knolls" => "knls",
267
+ "la" => "ln",
268
+ "lake" => "lk",
269
+ "lakes" => "lks",
270
+ "landing" => "lndg",
271
+ "lane" => "ln",
272
+ "lanes" => "ln",
273
+ "ldge" => "ldg",
274
+ "light" => "lgt",
275
+ "lights" => "lgts",
276
+ "lndng" => "lndg",
277
+ "loaf" => "lf",
278
+ "lock" => "lck",
279
+ "locks" => "lcks",
280
+ "lodg" => "ldg",
281
+ "lodge" => "ldg",
282
+ "loops" => "loop",
283
+ "manor" => "mnr",
284
+ "manors" => "mnrs",
285
+ "meadow" => "mdw",
286
+ "meadows" => "mdws",
287
+ "medows" => "mdws",
288
+ "mill" => "ml",
289
+ "mills" => "mls",
290
+ "mission" => "msn",
291
+ "missn" => "msn",
292
+ "mnt" => "mt",
293
+ "mntain" => "mtn",
294
+ "mntn" => "mtn",
295
+ "mntns" => "mtns",
296
+ "motorway" => "mtwy",
297
+ "mount" => "mt",
298
+ "mountain" => "mtn",
299
+ "mountains" => "mtns",
300
+ "mountin" => "mtn",
301
+ "mssn" => "msn",
302
+ "mtin" => "mtn",
303
+ "neck" => "nck",
304
+ "orchard" => "orch",
305
+ "orchrd" => "orch",
306
+ "overpass" => "opas",
307
+ "ovl" => "oval",
308
+ "parks" => "park",
309
+ "parkway" => "pkwy",
310
+ "parkways" => "pkwy",
311
+ "parkwy" => "pkwy",
312
+ "passage" => "psge",
313
+ "paths" => "path",
314
+ "pikes" => "pike",
315
+ "pine" => "pne",
316
+ "pines" => "pnes",
317
+ "pk" => "park",
318
+ "pkway" => "pkwy",
319
+ "pkwys" => "pkwy",
320
+ "pky" => "pkwy",
321
+ "place" => "pl",
322
+ "plain" => "pln",
323
+ "plaines" => "plns",
324
+ "plains" => "plns",
325
+ "plaza" => "plz",
326
+ "plza" => "plz",
327
+ "point" => "pt",
328
+ "points" => "pts",
329
+ "port" => "prt",
330
+ "ports" => "prts",
331
+ "prairie" => "pr",
332
+ "prarie" => "pr",
333
+ "prk" => "park",
334
+ "prr" => "pr",
335
+ "rad" => "radl",
336
+ "radial" => "radl",
337
+ "radiel" => "radl",
338
+ "ranch" => "rnch",
339
+ "ranches" => "rnch",
340
+ "rapid" => "rpd",
341
+ "rapids" => "rpds",
342
+ "rdge" => "rdg",
343
+ "rest" => "rst",
344
+ "ridge" => "rdg",
345
+ "ridges" => "rdgs",
346
+ "river" => "riv",
347
+ "rivr" => "riv",
348
+ "rnchs" => "rnch",
349
+ "road" => "rd",
350
+ "roads" => "rds",
351
+ "route" => "rte",
352
+ "rvr" => "riv",
353
+ "shoal" => "shl",
354
+ "shoals" => "shls",
355
+ "shoar" => "shr",
356
+ "shoars" => "shrs",
357
+ "shore" => "shr",
358
+ "shores" => "shrs",
359
+ "skyway" => "skwy",
360
+ "spng" => "spg",
361
+ "spngs" => "spgs",
362
+ "spring" => "spg",
363
+ "springs" => "spgs",
364
+ "sprng" => "spg",
365
+ "sprngs" => "spgs",
366
+ "spurs" => "spur",
367
+ "sqr" => "sq",
368
+ "sqre" => "sq",
369
+ "sqrs" => "sqs",
370
+ "squ" => "sq",
371
+ "square" => "sq",
372
+ "squares" => "sqs",
373
+ "station" => "sta",
374
+ "statn" => "sta",
375
+ "stn" => "sta",
376
+ "str" => "st",
377
+ "strav" => "stra",
378
+ "strave" => "stra",
379
+ "straven" => "stra",
380
+ "stravenue" => "stra",
381
+ "stravn" => "stra",
382
+ "stream" => "strm",
383
+ "street" => "st",
384
+ "streets" => "sts",
385
+ "streme" => "strm",
386
+ "strt" => "st",
387
+ "strvn" => "stra",
388
+ "strvnue" => "stra",
389
+ "sumit" => "smt",
390
+ "sumitt" => "smt",
391
+ "summit" => "smt",
392
+ "terr" => "ter",
393
+ "terrace" => "ter",
394
+ "throughway" => "trwy",
395
+ "tpk" => "tpke",
396
+ "tr" => "trl",
397
+ "trace" => "trce",
398
+ "traces" => "trce",
399
+ "track" => "trak",
400
+ "tracks" => "trak",
401
+ "trafficway" => "trfy",
402
+ "trail" => "trl",
403
+ "trails" => "trl",
404
+ "trk" => "trak",
405
+ "trks" => "trak",
406
+ "trls" => "trl",
407
+ "trnpk" => "tpke",
408
+ "trpk" => "tpke",
409
+ "tunel" => "tunl",
410
+ "tunls" => "tunl",
411
+ "tunnel" => "tunl",
412
+ "tunnels" => "tunl",
413
+ "tunnl" => "tunl",
414
+ "turnpike" => "tpke",
415
+ "turnpk" => "tpke",
416
+ "underpass" => "upas",
417
+ "union" => "un",
418
+ "unions" => "uns",
419
+ "valley" => "vly",
420
+ "valleys" => "vlys",
421
+ "vally" => "vly",
422
+ "vdct" => "via",
423
+ "viadct" => "via",
424
+ "viaduct" => "via",
425
+ "view" => "vw",
426
+ "views" => "vws",
427
+ "vill" => "vlg",
428
+ "villag" => "vlg",
429
+ "village" => "vlg",
430
+ "villages" => "vlgs",
431
+ "ville" => "vl",
432
+ "villg" => "vlg",
433
+ "villiage" => "vlg",
434
+ "vist" => "vis",
435
+ "vista" => "vis",
436
+ "vlly" => "vly",
437
+ "vst" => "vis",
438
+ "vsta" => "vis",
439
+ "walks" => "walk",
440
+ "well" => "wl",
441
+ "wells" => "wls",
442
+ "wy" => "way"
443
+ }
444
+
445
+ STREET_TYPES_LIST = {}
446
+ STREET_TYPES.to_a.each do |item|
447
+ STREET_TYPES_LIST[item[0]] = true
448
+ STREET_TYPES_LIST[item[1]] = true
449
+ end
450
+
451
+ STATE_CODES = {
452
+ "alabama" => "AL",
453
+ "alaska" => "AK",
454
+ "american samoa" => "AS",
455
+ "arizona" => "AZ",
456
+ "arkansas" => "AR",
457
+ "california" => "CA",
458
+ "colorado" => "CO",
459
+ "connecticut" => "CT",
460
+ "delaware" => "DE",
461
+ "district of columbia" => "DC",
462
+ "federated states of micronesia" => "FM",
463
+ "florida" => "FL",
464
+ "georgia" => "GA",
465
+ "guam" => "GU",
466
+ "hawaii" => "HI",
467
+ "idaho" => "ID",
468
+ "illinois" => "IL",
469
+ "indiana" => "IN",
470
+ "iowa" => "IA",
471
+ "kansas" => "KS",
472
+ "kentucky" => "KY",
473
+ "louisiana" => "LA",
474
+ "maine" => "ME",
475
+ "marshall islands" => "MH",
476
+ "maryland" => "MD",
477
+ "massachusetts" => "MA",
478
+ "michigan" => "MI",
479
+ "minnesota" => "MN",
480
+ "mississippi" => "MS",
481
+ "missouri" => "MO",
482
+ "montana" => "MT",
483
+ "nebraska" => "NE",
484
+ "nevada" => "NV",
485
+ "new hampshire" => "NH",
486
+ "new jersey" => "NJ",
487
+ "new mexico" => "NM",
488
+ "new york" => "NY",
489
+ "north carolina" => "NC",
490
+ "north dakota" => "ND",
491
+ "northern mariana islands" => "MP",
492
+ "ohio" => "OH",
493
+ "oklahoma" => "OK",
494
+ "oregon" => "OR",
495
+ "palau" => "PW",
496
+ "pennsylvania" => "PA",
497
+ "puerto rico" => "PR",
498
+ "rhode island" => "RI",
499
+ "south carolina" => "SC",
500
+ "south dakota" => "SD",
501
+ "tennessee" => "TN",
502
+ "texas" => "TX",
503
+ "utah" => "UT",
504
+ "vermont" => "VT",
505
+ "virgin islands" => "VI",
506
+ "virginia" => "VA",
507
+ "washington" => "WA",
508
+ "west virginia" => "WV",
509
+ "wisconsin" => "WI",
510
+ "wyoming" => "WY"
511
+ }
512
+
513
+ STATE_NAMES = STATE_CODES.invert
514
+
515
+ STATE_FIPS = {
516
+ "01" => "AL",
517
+ "02" => "AK",
518
+ "04" => "AZ",
519
+ "05" => "AR",
520
+ "06" => "CA",
521
+ "08" => "CO",
522
+ "09" => "CT",
523
+ "10" => "DE",
524
+ "11" => "DC",
525
+ "12" => "FL",
526
+ "13" => "GA",
527
+ "15" => "HI",
528
+ "16" => "ID",
529
+ "17" => "IL",
530
+ "18" => "IN",
531
+ "19" => "IA",
532
+ "20" => "KS",
533
+ "21" => "KY",
534
+ "22" => "LA",
535
+ "23" => "ME",
536
+ "24" => "MD",
537
+ "25" => "MA",
538
+ "26" => "MI",
539
+ "27" => "MN",
540
+ "28" => "MS",
541
+ "29" => "MO",
542
+ "30" => "MT",
543
+ "31" => "NE",
544
+ "32" => "NV",
545
+ "33" => "NH",
546
+ "34" => "NJ",
547
+ "35" => "NM",
548
+ "36" => "NY",
549
+ "37" => "NC",
550
+ "38" => "ND",
551
+ "39" => "OH",
552
+ "40" => "OK",
553
+ "41" => "OR",
554
+ "42" => "PA",
555
+ "44" => "RI",
556
+ "45" => "SC",
557
+ "46" => "SD",
558
+ "47" => "TN",
559
+ "48" => "TX",
560
+ "49" => "UT",
561
+ "50" => "VT",
562
+ "51" => "VA",
563
+ "53" => "WA",
564
+ "54" => "WV",
565
+ "55" => "WI",
566
+ "56" => "WY",
567
+ "72" => "PR",
568
+ "78" => "VI"
569
+ }
570
+
571
+ FIPS_STATES = STATE_FIPS.invert
572
+
573
+ class << self
574
+ attr_accessor(
575
+ :street_type_regexp,
576
+ :number_regexp,
577
+ :fraction_regexp,
578
+ :state_regexp,
579
+ :city_and_state_regexp,
580
+ :direct_regexp,
581
+ :zip_regexp,
582
+ :corner_regexp,
583
+ :unit_regexp,
584
+ :street_regexp,
585
+ :place_regexp,
586
+ :address_regexp,
587
+ :informal_address_regexp
588
+ )
589
+ end
590
+
591
+ self.street_type_regexp = STREET_TYPES_LIST.keys.join("|")
592
+ self.number_regexp = '\d+-?\d*'
593
+ self.fraction_regexp = '\d+\/\d+'
594
+ self.state_regexp = STATE_CODES.to_a.join("|").gsub(/ /, "\\s")
595
+ self.city_and_state_regexp = '
596
+ (?:
597
+ ([^\d,]+?)\W+
598
+ (' + state_regexp + ')
599
+ )'
600
+
601
+ self.direct_regexp = DIRECTIONAL.keys.join("|") +
602
+ "|" +
603
+ DIRECTIONAL.values.sort{ |a,b|
604
+ b.length <=> a.length
605
+ }.map{ |x|
606
+ f = x.gsub(/(\w)/, '\1.')
607
+ [Regexp::quote(f), Regexp::quote(x)]
608
+ }.join("|")
609
+ self.zip_regexp = '(\d{5})(?:-?(\d{4})?)'
610
+ self.corner_regexp = '(?:\band\b|\bat\b|&|\@)'
611
+ self.unit_regexp = '(?:(su?i?te|p\W*[om]\W*b(?:ox)?|dept|apt|apartment|ro*m|fl|unit|box)\W+|\#\W*)([\w-]+)'
612
+ self.street_regexp =
613
+ '(?:
614
+ (?:(' + direct_regexp + ')\W+
615
+ (' + street_type_regexp + ')\b)
616
+ |
617
+ (?:(' + direct_regexp + ')\W+)?
618
+ (?:
619
+ ([^,]+)
620
+ (?:[^\w,]+(' + street_type_regexp + ')\b)
621
+ (?:[^\w,]+(' + direct_regexp + ')\b)?
622
+ |
623
+ ([^,]*\d)
624
+ (' + direct_regexp + ')\b
625
+ |
626
+ ([^,]+?)
627
+ (?:[^\w,]+(' + street_type_regexp + ')\b)?
628
+ (?:[^\w,]+(' + direct_regexp + ')\b)?
629
+ )
630
+ )'
631
+ self.place_regexp =
632
+ '(?:' + city_and_state_regexp + '\W*)?
633
+ (?:' + zip_regexp + ')?'
634
+
635
+ self.address_regexp =
636
+ '\A\W*
637
+ (' + number_regexp + ')\W*
638
+ (?:' + fraction_regexp + '\W*)?' +
639
+ street_regexp + '\W+
640
+ (?:' + unit_regexp + '\W+)?' +
641
+ place_regexp +
642
+ '\W*\Z'
643
+
644
+ self.informal_address_regexp =
645
+ '\A\s*
646
+ (' + number_regexp + ')\W*
647
+ (?:' + fraction_regexp + '\W*)?' +
648
+ street_regexp + '(?:\W+|\Z)
649
+ (?:' + unit_regexp + '(?:\W+|\Z))?' +
650
+ '(?:' + place_regexp + ')?'
651
+
652
+ =begin rdoc
653
+
654
+ parses either an address or intersection and returns an instance of
655
+ StreetAddress::US::Address or nil if the location cannot be parsed
656
+
657
+ pass the arguement, :informal => true, to make parsing more lenient
658
+
659
+ ====example
660
+ StreetAddress::US.parse('1600 Pennsylvania Ave Washington, DC 20006')
661
+ or:
662
+ StreetAddress::US.parse('Hollywood & Vine, Los Angeles, CA')
663
+ or
664
+ StreetAddress::US.parse("1600 Pennsylvania Ave", :informal => true)
665
+
666
+ =end
667
+ class << self
668
+ def parse(location, args = {})
669
+ if Regexp.new(corner_regexp, Regexp::IGNORECASE).match(location)
670
+ parse_intersection(location)
671
+ elsif args[:informal]
672
+ parse_address(location) || parse_informal_address(location)
673
+ else
674
+ parse_address(location);
675
+ end
676
+ end
677
+ =begin rdoc
678
+
679
+ parses only an intersection and returnsan instance of
680
+ StreetAddress::US::Address or nil if the intersection cannot be parsed
681
+
682
+ ====example
683
+ address = StreetAddress::US.parse('Hollywood & Vine, Los Angeles, CA')
684
+ assert address.intersection?
685
+
686
+ =end
687
+ def parse_intersection(inter)
688
+ regex = Regexp.new(
689
+ '\A\W*' + street_regexp + '\W*?
690
+ \s+' + corner_regexp + '\s+' +
691
+ street_regexp + '\W+' +
692
+ place_regexp + '\W*\Z', Regexp::IGNORECASE + Regexp::EXTENDED
693
+ )
694
+
695
+ return unless match = regex.match(inter)
696
+
697
+ normalize_address(
698
+ StreetAddress::US::Address.new(
699
+ :street => match[4] || match[9],
700
+ :street_type => match[5],
701
+ :suffix => match[6],
702
+ :prefix => match[3],
703
+ :street2 => match[15] || match[20],
704
+ :street_type2 => match[16],
705
+ :suffix2 => match[17],
706
+ :prefix2 => match[14],
707
+ :city => match[23],
708
+ :state => match[24],
709
+ :postal_code => match[25]
710
+ )
711
+ )
712
+ end
713
+
714
+ =begin rdoc
715
+
716
+ parses only an address and returnsan instance of
717
+ StreetAddress::US::Address or nil if the address cannot be parsed
718
+
719
+ ====example
720
+ address = StreetAddress::US.parse('1600 Pennsylvania Ave Washington, DC 20006')
721
+ assert !address.intersection?
722
+
723
+ =end
724
+ def parse_address(addr)
725
+ regex = Regexp.new(address_regexp, Regexp::IGNORECASE + Regexp::EXTENDED)
726
+
727
+ return unless match = regex.match(addr)
728
+
729
+ normalize_address(
730
+ StreetAddress::US::Address.new(
731
+ :number => match[1],
732
+ :street => match[5] || match[10] || match[2],
733
+ :street_type => match[6] || match[3],
734
+ :unit => match[14],
735
+ :unit_prefix => match[13],
736
+ :suffix => match[7] || match[12],
737
+ :prefix => match[4],
738
+ :city => match[15],
739
+ :state => match[16],
740
+ :postal_code => match[17],
741
+ :postal_code_ext => match[18]
742
+ )
743
+ )
744
+ end
745
+
746
+ def parse_informal_address(addr)
747
+ regex = Regexp.new(informal_address_regexp, Regexp::IGNORECASE + Regexp::EXTENDED)
748
+
749
+ return unless match = regex.match(addr)
750
+
751
+ normalize_address(
752
+ StreetAddress::US::Address.new(
753
+ :number => match[1],
754
+ :street => match[5] || match[10] || match[2],
755
+ :street_type => match[6] || match[3],
756
+ :unit => match[14],
757
+ :unit_prefix => match[13],
758
+ :suffix => match[7] || match[12],
759
+ :prefix => match[4],
760
+ :city => match[15],
761
+ :state => match[16],
762
+ :postal_code => match[17],
763
+ :postal_code_ext => match[18]
764
+ )
765
+ )
766
+ end
767
+
768
+ private
769
+ def normalize_address(addr)
770
+ addr.state = normalize_state(addr.state) unless addr.state.nil?
771
+ addr.street_type = normalize_street_type(addr.street_type) unless addr.street_type.nil?
772
+ addr.prefix = normalize_directional(addr.prefix) unless addr.prefix.nil?
773
+ addr.suffix = normalize_directional(addr.suffix) unless addr.suffix.nil?
774
+ addr.street.gsub!(/\b([a-z])/) {|wd| wd.capitalize} unless addr.street.nil?
775
+ addr.street_type2 = normalize_street_type(addr.street_type2) unless addr.street_type2.nil?
776
+ addr.prefix2 = normalize_directional(addr.prefix2) unless addr.prefix2.nil?
777
+ addr.suffix2 = normalize_directional(addr.suffix2) unless addr.suffix2.nil?
778
+ addr.street2.gsub!(/\b([a-z])/) {|wd| wd.capitalize} unless addr.street2.nil?
779
+ addr.city.gsub!(/\b([a-z])/) {|wd| wd.capitalize} unless addr.city.nil?
780
+ addr.unit_prefix.capitalize! unless addr.unit_prefix.nil?
781
+ return addr
782
+ end
783
+
784
+ def normalize_state(state)
785
+ if state.length < 3
786
+ state.upcase
787
+ else
788
+ STATE_CODES[state.downcase]
789
+ end
790
+ end
791
+
792
+ def normalize_street_type(s_type)
793
+ s_type.downcase!
794
+ s_type = STREET_TYPES[s_type] || s_type if STREET_TYPES_LIST[s_type]
795
+ s_type.capitalize
796
+ end
797
+
798
+ def normalize_directional(dir)
799
+ if dir.length < 3
800
+ dir.upcase
801
+ else
802
+ DIRECTIONAL[dir.downcase]
803
+ end
804
+ end
805
+ end
806
+
807
+ =begin rdoc
808
+
809
+ This is class returned by StreetAddress::US::parse, StreetAddress::US::parse_address
810
+ and StreetAddress::US::parse_intersection. If an instance represents an intersection
811
+ the attribute street2 will be populated.
812
+
813
+ =end
814
+ class Address
815
+ attr_accessor(
816
+ :number,
817
+ :street,
818
+ :street_type,
819
+ :unit,
820
+ :unit_prefix,
821
+ :suffix,
822
+ :prefix,
823
+ :city,
824
+ :state,
825
+ :postal_code,
826
+ :postal_code_ext,
827
+ :street2,
828
+ :street_type2,
829
+ :suffix2,
830
+ :prefix2
831
+ )
832
+
833
+ def initialize(args)
834
+ args.keys.each do |attrib|
835
+ self.send("#{attrib}=", args[attrib])
836
+ end
837
+ return
838
+ end
839
+
840
+ def state_fips
841
+ StreetAddress::US::FIPS_STATES[state]
842
+ end
843
+
844
+ def state_name
845
+ name = StreetAddress::US::STATE_NAMES[state] and name.capitalize
846
+ end
847
+
848
+ def intersection?
849
+ !street2.nil?
850
+ end
851
+
852
+ def line1(s = "")
853
+ s += number unless number.nil?
854
+ s += " " + prefix unless prefix.nil?
855
+ s += " " + street unless street.nil?
856
+ s += " " + street_type unless street_type.nil?
857
+ if( !unit_prefix.nil? && !unit.nil? )
858
+ s += " " + unit_prefix
859
+ s += " " + unit
860
+ elsif( unit_prefix.nil? && !unit.nil? )
861
+ s += " #" + unit
862
+ end
863
+ s += " " + suffix unless suffix.nil?
864
+ return s
865
+ end
866
+
867
+ def to_s(format = :default)
868
+ s = ""
869
+ case format
870
+ when :line1
871
+ s += line1(s)
872
+ else
873
+ if intersection?
874
+ s += prefix + " " unless prefix.nil?
875
+ s += street
876
+ s += " " + street_type unless street_type.nil?
877
+ s += " " + suffix unless suffix.nil?
878
+ s += " and"
879
+ s += " " + prefix2 unless prefix2.nil?
880
+ s += " " + street2
881
+ s += " " + street_type2 unless street_type2.nil?
882
+ s += " " + suffix2 unless suffix2.nil?
883
+ s += ", " + city unless city.nil?
884
+ s += ", " + state unless state.nil?
885
+ s += " " + postal_code unless postal_code.nil?
886
+ else
887
+ s += line1(s)
888
+ s += ", " + city unless city.nil?
889
+ s += ", " + state unless state.nil?
890
+ s += " " + postal_code unless postal_code.nil?
891
+ s += "-" + postal_code_ext unless postal_code_ext.nil?
892
+ end
893
+ end
894
+ return s
895
+ end
896
+ end
897
+ end
898
+ end
@@ -0,0 +1,174 @@
1
+ require 'test/unit'
2
+ require 'street_address'
3
+
4
+
5
+ class StreetAddressUsTest < Test::Unit::TestCase
6
+ def setup
7
+ @addr1 = "2730 S Veitch St Apt 207, Arlington, VA 22206"
8
+ @addr2 = "44 Canal Center Plaza Suite 500, Alexandria, VA 22314"
9
+ @addr3 = "1600 Pennsylvania Ave Washington DC"
10
+ @addr4 = "1005 Gravenstein Hwy N, Sebastopol CA 95472"
11
+ @addr5 = "PO BOX 450, Chicago IL 60657"
12
+ @addr6 = "2730 S Veitch St #207, Arlington, VA 22206"
13
+
14
+ @int1 = "Hollywood & Vine, Los Angeles, CA"
15
+ @int2 = "Hollywood Blvd and Vine St, Los Angeles, CA"
16
+ @int3 = "Mission Street at Valencia Street, San Francisco, CA"
17
+
18
+ end
19
+
20
+ def test_zip_plus_4_with_dash
21
+ addr = StreetAddress::US.parse("2730 S Veitch St, Arlington, VA 22206-3333")
22
+ assert_equal "3333", addr.postal_code_ext
23
+ end
24
+
25
+ def test_zip_plus_4_without_dash
26
+ addr = StreetAddress::US.parse("2730 S Veitch St, Arlington, VA 222064444")
27
+ assert_equal "4444", addr.postal_code_ext
28
+ end
29
+
30
+ def test_informal_parse_normal_address
31
+ a = StreetAddress::US.parse("2730 S Veitch St, Arlington, VA 222064444", :informal => true)
32
+ assert_equal "2730", a.number
33
+ assert_equal "S", a.prefix
34
+ assert_equal "Veitch", a.street
35
+ assert_equal "St", a.street_type
36
+ assert_equal "Arlington", a.city
37
+ assert_equal "VA", a.state
38
+ assert_equal "22206", a.postal_code
39
+ assert_equal "4444", a.postal_code_ext
40
+ end
41
+
42
+ def test_informal_parse_informal_address
43
+ a = StreetAddress::US.parse("2730 S Veitch St", :informal => true)
44
+ assert_equal "2730", a.number
45
+ assert_equal "S", a.prefix
46
+ assert_equal "Veitch", a.street
47
+ assert_equal "St", a.street_type
48
+ end
49
+
50
+ def test_informal_parse_informal_address_trailing_words
51
+ a = StreetAddress::US.parse("2730 S Veitch St in the south of arlington", :informal => true)
52
+ assert_equal "2730", a.number
53
+ assert_equal "S", a.prefix
54
+ assert_equal "Veitch", a.street
55
+ assert_equal "St", a.street_type
56
+ end
57
+
58
+ def test_parse
59
+ assert_equal StreetAddress::US.parse("&"), nil
60
+ assert_equal StreetAddress::US.parse(" and "), nil
61
+
62
+ addr = StreetAddress::US.parse(@addr1)
63
+ assert_equal addr.number, "2730"
64
+ assert_equal addr.postal_code, "22206"
65
+ assert_equal addr.prefix, "S"
66
+ assert_equal addr.state, "VA"
67
+ assert_equal addr.street, "Veitch"
68
+ assert_equal addr.street_type, "St"
69
+ assert_equal addr.unit, "207"
70
+ assert_equal addr.unit_prefix, "Apt"
71
+ assert_equal addr.city, "Arlington"
72
+ assert_equal addr.prefix2, nil
73
+ assert_equal addr.postal_code_ext, nil
74
+
75
+ addr = StreetAddress::US.parse(@addr2)
76
+ assert_equal addr.number, "44"
77
+ assert_equal addr.postal_code, "22314"
78
+ assert_equal addr.prefix, nil
79
+ assert_equal addr.state, "VA"
80
+ assert_equal addr.street, "Canal Center"
81
+ assert_equal addr.street_type, "Plz"
82
+ assert_equal addr.unit, "500"
83
+ assert_equal addr.unit_prefix, "Suite"
84
+ assert_equal addr.city, "Alexandria"
85
+ assert_equal addr.street2, nil
86
+
87
+ addr = StreetAddress::US.parse(@addr3)
88
+ assert_equal addr.number, "1600"
89
+ assert_equal addr.postal_code, nil
90
+ assert_equal addr.prefix, nil
91
+ assert_equal addr.state, "DC"
92
+ assert_equal addr.street, "Pennsylvania"
93
+ assert_equal addr.street_type, "Ave"
94
+ assert_equal addr.unit, nil
95
+ assert_equal addr.unit_prefix, nil
96
+ assert_equal addr.city, "Washington"
97
+ assert_equal addr.street2, nil
98
+
99
+
100
+
101
+ addr = StreetAddress::US.parse(@addr4)
102
+ assert_equal addr.number, "1005"
103
+ assert_equal addr.postal_code, "95472"
104
+ assert_equal addr.prefix, nil
105
+ assert_equal addr.state, "CA"
106
+ assert_equal addr.street, "Gravenstein"
107
+ assert_equal addr.street_type, "Hwy"
108
+ assert_equal addr.unit, nil
109
+ assert_equal addr.unit_prefix, nil
110
+ assert_equal addr.city, "Sebastopol"
111
+ assert_equal addr.street2, nil
112
+ assert_equal addr.suffix, "N"
113
+
114
+
115
+ addr = StreetAddress::US.parse(@addr5)
116
+ assert_equal addr, nil
117
+
118
+
119
+ addr = StreetAddress::US.parse(@addr6)
120
+ assert_equal("207", addr.unit)
121
+
122
+ addr = StreetAddress::US.parse(@int1)
123
+ assert_equal addr.city, "Los Angeles"
124
+ assert_equal addr.state, "CA"
125
+ assert_equal addr.street, "Hollywood"
126
+ assert_equal addr.street2, "Vine"
127
+ assert_equal addr.number, nil
128
+ assert_equal addr.postal_code, nil
129
+ assert_equal addr.intersection?, true
130
+
131
+ addr = StreetAddress::US.parse(@int2)
132
+ assert_equal addr.city, "Los Angeles"
133
+ assert_equal addr.state, "CA"
134
+ assert_equal addr.street, "Hollywood"
135
+ assert_equal addr.street2, "Vine"
136
+ assert_equal addr.number, nil
137
+ assert_equal addr.postal_code, nil
138
+ assert_equal addr.intersection?, true
139
+ assert_equal addr.street_type, "Blvd"
140
+ assert_equal addr.street_type2, "St"
141
+
142
+ addr = StreetAddress::US.parse(@int3)
143
+ assert_equal addr.city, "San Francisco"
144
+ assert_equal addr.state, "CA"
145
+ assert_equal addr.street, "Mission"
146
+ assert_equal addr.street2, "Valencia"
147
+ assert_equal addr.number, nil
148
+ assert_equal addr.postal_code, nil
149
+ assert_equal addr.intersection?, true
150
+ assert_equal addr.street_type, "St"
151
+ assert_equal addr.street_type2, "St"
152
+
153
+ parseable = ["1600 Pennsylvania Ave Washington DC 20006",
154
+ "1600 Pennsylvania Ave #400, Washington, DC, 20006",
155
+ "1600 Pennsylvania Ave Washington, DC",
156
+ "1600 Pennsylvania Ave #400 Washington DC",
157
+ "1600 Pennsylvania Ave, 20006",
158
+ "1600 Pennsylvania Ave #400, 20006",
159
+ "1600 Pennsylvania Ave 20006",
160
+ "1600 Pennsylvania Ave #400 20006",
161
+ "Hollywood & Vine, Los Angeles, CA",
162
+ "Hollywood Blvd and Vine St, Los Angeles, CA",
163
+ "Mission Street at Valencia Street, San Francisco, CA",
164
+ "Hollywood & Vine, Los Angeles, CA, 90028",
165
+ "Hollywood Blvd and Vine St, Los Angeles, CA, 90028",
166
+ "Mission Street at Valencia Street, San Francisco, CA, 90028"]
167
+
168
+ parseable.each do |location|
169
+ assert_not_nil(StreetAddress::US.parse(location), location + " was not parse able")
170
+ end
171
+
172
+ end
173
+
174
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: TempStreetAddress
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Derrek Long
8
+ - Nicholas Schleuter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'StreetAddress::US allows you to send any string to parse and if the
15
+ string is a US address returns an object of the address broken into it''s substituent
16
+ parts.
17
+
18
+
19
+ A port of Geo::StreetAddress::US by Schuyler D. Erle and Tim Bunce.
20
+
21
+ '
22
+ email: derreklong@gmail.com
23
+ executables: []
24
+ extensions: []
25
+ extra_rdoc_files: []
26
+ files:
27
+ - README.rdoc
28
+ - Rakefile
29
+ - LICENSE
30
+ - lib/street_address.rb
31
+ - test/test_street_address.rb
32
+ homepage: https://github.com/derrek/street-address
33
+ licenses: []
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.0.3
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Parse Addresses into substituent parts. This gem includes US only.
55
+ test_files:
56
+ - test/test_street_address.rb
57
+ has_rdoc: