StreetAddress 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,34 @@
1
+ StreetAddress
2
+ by Derrek Long
3
+ https://github.com/derrek/street-address
4
+
5
+ == DESCRIPTION:
6
+
7
+ Parses one line street addresses and returns a normalized address object.
8
+
9
+ This is a near direct port of the of the perl module
10
+ Geo::StreetAddress::US originally written by Schuyler D. Erle.
11
+ For more information see
12
+ http://search.cpan.org/~sderle/Geo-StreetAddress-US-0.99/
13
+
14
+ == SYNOPSIS:
15
+
16
+ Parse United States addresses.
17
+
18
+ === Basic Usage:
19
+ require 'street_address'
20
+ address = StreetAddress::US.parse("1600 Pennsylvania Ave, Washington, DC, 20500")
21
+ address.street # Pennsylvania
22
+ address.number # 1600
23
+ address.postal_code # 20500
24
+ address.city # Washington
25
+ address.state # DC
26
+ address.state_name # District of columbia
27
+ address.street_type # Ave
28
+ address.intersection? # false
29
+
30
+ StreetAddress::US.parse("1600 Pennsylvania Ave") # nil not a full address
31
+
32
+ === Gemfile:
33
+ To use from your Gemfile:
34
+ gem 'StreetAddress', '1.0.2', :require => "street_address
data/Rakefile CHANGED
@@ -1,22 +1,8 @@
1
- # -*- ruby -*-
1
+ require 'rake/testtask'
2
2
 
3
- require 'rubygems'
4
- require 'hoe'
5
- require './lib/street_address.rb'
6
-
7
- Hoe.new('StreetAddress', StreetAddress::VERSION) do |p|
8
- p.rubyforge_name = 'streetaddress'
9
- p.summary = 'Ruby port of the perl module Geo::StreetAddress::US to parse one line street addresses'
10
- p.description = "Parses one line addresses and returns a normalized address object.
11
-
12
- This is a near direct port of the of the perl module
13
- Geo::StreetAddress::US originally written by Schuyler D. Erle.
14
- For more information see
15
- http://search.cpan.org/~sderle/Geo-StreetAddress-US-0.99/"
16
- p.url = "http://streetaddress.rubyforge.org"
17
- p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
18
- p.author = "Derrek Long"
19
- p.email = "derrek.long@gmail.com"
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
20
5
  end
21
6
 
22
- # vim: syntax=Ruby
7
+ desc "Run tests"
8
+ task :default => :test
@@ -63,9 +63,9 @@
63
63
  =end
64
64
 
65
65
  module StreetAddress
66
- VERSION = '1.0.1'
67
66
  class US
68
- @@directional = {
67
+ VERSION = '1.0.2'
68
+ DIRECTIONAL = {
69
69
  "north" => "N",
70
70
  "northeast" => "NE",
71
71
  "east" => "E",
@@ -75,9 +75,9 @@ module StreetAddress
75
75
  "west" => "W",
76
76
  "northwest" => "NW"
77
77
  }
78
- @@direction_code = @@directional.invert
78
+ DIRECTION_CODES = DIRECTIONAL.invert
79
79
 
80
- @@street_type = {
80
+ STREET_TYPES = {
81
81
  "allee" => "aly",
82
82
  "alley" => "aly",
83
83
  "ally" => "aly",
@@ -442,10 +442,13 @@ module StreetAddress
442
442
  "wy" => "way"
443
443
  }
444
444
 
445
- @@street_type_list = {}
446
- @@street_type.to_a.each{|item| @@street_type_list[item[0]] = true; @@street_type_list[item[1]] = true}
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
447
450
 
448
- @@state_code = {
451
+ STATE_CODES = {
449
452
  "alabama" => "AL",
450
453
  "alaska" => "AK",
451
454
  "american samoa" => "AS",
@@ -507,9 +510,9 @@ module StreetAddress
507
510
  "wyoming" => "WY"
508
511
  }
509
512
 
510
- @@state_name = @@state_code.invert
513
+ STATE_NAMES = STATE_CODES.invert
511
514
 
512
- @@state_fips = {
515
+ STATE_FIPS = {
513
516
  "01" => "AL",
514
517
  "02" => "AK",
515
518
  "04" => "AZ",
@@ -565,52 +568,74 @@ module StreetAddress
565
568
  "78" => "VI"
566
569
  }
567
570
 
568
- @@fips_state = @@state_fips.invert
571
+ FIPS_STATES = STATE_FIPS.invert
569
572
 
570
- @@street_type_regexp = @@street_type_list.keys.join("|")
571
- @@number_regexp = '\d+-?\d*'
572
- @@fraction_regexp = '\d+\/\d+'
573
- @@state_regexp = @@state_code.to_a.join("|").gsub(/ /, "\\s")
574
- @@direct_regexp = @@directional.keys.join("|") + "|" + @@directional.values.sort{|a,b| b.length <=> a.length }.map{|x| f = x.gsub(/(\w)/, '\1.'); [Regexp::quote(f), Regexp::quote(x)] }.join("|")
575
- @@zip_regexp = '(\d{5})(?:-(\d{4}))?'
576
- @@corner_regexp = '(?:\band\b|\bat\b|&|\@)'
577
- @@unit_regexp = '(?:(su?i?te|p\W*[om]\W*b(?:ox)?|dept|apt|apartment|ro*m|fl|unit|box)\W+|\#\W*)([\w-]+)'
578
- @@street_regexp =
573
+ class << self
574
+ attr_accessor(
575
+ :street_type_regexp,
576
+ :number_regexp,
577
+ :fraction_regexp,
578
+ :state_regexp,
579
+ :direct_regexp,
580
+ :zip_regexp,
581
+ :corner_regexp,
582
+ :unit_regexp,
583
+ :street_regexp,
584
+ :place_regexp,
585
+ :address_regexp
586
+ )
587
+ end
588
+
589
+ self.street_type_regexp = STREET_TYPES_LIST.keys.join("|")
590
+ self.number_regexp = '\d+-?\d*'
591
+ self.fraction_regexp = '\d+\/\d+'
592
+ self.state_regexp = STATE_CODES.to_a.join("|").gsub(/ /, "\\s")
593
+ self.direct_regexp = DIRECTIONAL.keys.join("|") +
594
+ "|" +
595
+ DIRECTIONAL.values.sort{ |a,b|
596
+ b.length <=> a.length
597
+ }.map{ |x|
598
+ f = x.gsub(/(\w)/, '\1.')
599
+ [Regexp::quote(f), Regexp::quote(x)]
600
+ }.join("|")
601
+ self.zip_regexp = '(\d{5})(?:-(\d{4}))?'
602
+ self.corner_regexp = '(?:\band\b|\bat\b|&|\@)'
603
+ self.unit_regexp = '(?:(su?i?te|p\W*[om]\W*b(?:ox)?|dept|apt|apartment|ro*m|fl|unit|box)\W+|\#\W*)([\w-]+)'
604
+ self.street_regexp =
579
605
  '(?:
580
- (?:(' + @@direct_regexp + ')\W+
581
- (' + @@street_type_regexp + ')\b)
606
+ (?:(' + direct_regexp + ')\W+
607
+ (' + street_type_regexp + ')\b)
582
608
  |
583
- (?:(' + @@direct_regexp + ')\W+)?
609
+ (?:(' + direct_regexp + ')\W+)?
584
610
  (?:
585
611
  ([^,]+)
586
- (?:[^\w,]+(' + @@street_type_regexp + ')\b)
587
- (?:[^\w,]+(' + @@direct_regexp + ')\b)?
612
+ (?:[^\w,]+(' + street_type_regexp + ')\b)
613
+ (?:[^\w,]+(' + direct_regexp + ')\b)?
588
614
  |
589
615
  ([^,]*\d)
590
- (' + @@direct_regexp + ')\b
616
+ (' + direct_regexp + ')\b
591
617
  |
592
618
  ([^,]+?)
593
- (?:[^\w,]+(' + @@street_type_regexp + ')\b)?
594
- (?:[^\w,]+(' + @@direct_regexp + ')\b)?
619
+ (?:[^\w,]+(' + street_type_regexp + ')\b)?
620
+ (?:[^\w,]+(' + direct_regexp + ')\b)?
595
621
  )
596
622
  )'
597
- @@place_regexp =
623
+ self.place_regexp =
598
624
  '(?:
599
625
  ([^\d,]+?)\W+
600
- ($' + @@state_regexp + ')\W*
626
+ ($' + state_regexp + ')\W*
601
627
  )?
602
- (?:' + @@zip_regexp + ')?'
628
+ (?:' + zip_regexp + ')?'
603
629
 
604
- @@address_regexp =
630
+ self.address_regexp =
605
631
  '\A\W*
606
- (' + @@number_regexp + ')\W*
607
- (?:' + @@fraction_regexp + '\W*)?' +
608
- @@street_regexp + '\W+
609
- (?:' + @@unit_regexp + '\W+)?' +
610
- @@place_regexp +
632
+ (' + number_regexp + ')\W*
633
+ (?:' + fraction_regexp + '\W*)?' +
634
+ street_regexp + '\W+
635
+ (?:' + unit_regexp + '\W+)?' +
636
+ place_regexp +
611
637
  '\W*\Z'
612
638
 
613
- class << self
614
639
  =begin rdoc
615
640
 
616
641
  parses either an address or intersection and returns an instance of
@@ -622,10 +647,9 @@ module StreetAddress
622
647
  StreetAddress::US.parse('Hollywood & Vine, Los Angeles, CA')
623
648
 
624
649
  =end
650
+ class << self
625
651
  def parse(location)
626
- regex = Regexp.new(@@corner_regexp, Regexp::IGNORECASE)
627
- if regex.match(location)
628
- z = regex.match(location)
652
+ if Regexp.new(corner_regexp, Regexp::IGNORECASE).match(location)
629
653
  parse_intersection(location);
630
654
  else
631
655
  parse_address(location);
@@ -644,13 +668,14 @@ module StreetAddress
644
668
  =end
645
669
  def parse_intersection(inter)
646
670
  regex = Regexp.new(
647
- '\A\W*' + @@street_regexp + '\W*?
648
- \s+' + @@corner_regexp + '\s+' +
649
- @@street_regexp + '\W+' +
650
- @@place_regexp + '\W*\Z', Regexp::IGNORECASE + Regexp::EXTENDED)
651
- match = regex.match(inter)
652
- return if match.nil?
653
-
671
+ '\A\W*' + street_regexp + '\W*?
672
+ \s+' + corner_regexp + '\s+' +
673
+ street_regexp + '\W+' +
674
+ place_regexp + '\W*\Z', Regexp::IGNORECASE + Regexp::EXTENDED
675
+ )
676
+
677
+ return unless match = regex.match(inter)
678
+
654
679
  normalize_address(
655
680
  StreetAddress::US::Address.new(
656
681
  :street => match[4] || match[9],
@@ -679,9 +704,9 @@ module StreetAddress
679
704
 
680
705
  =end
681
706
  def parse_address(addr)
682
- regex = Regexp.new(@@address_regexp, Regexp::IGNORECASE + Regexp::EXTENDED)
683
- match = regex.match(addr)
684
- return if match.nil?
707
+ regex = Regexp.new(address_regexp, Regexp::IGNORECASE + Regexp::EXTENDED)
708
+
709
+ return unless match = regex.match(addr)
685
710
 
686
711
  normalize_address(
687
712
  StreetAddress::US::Address.new(
@@ -700,14 +725,6 @@ module StreetAddress
700
725
  )
701
726
  end
702
727
 
703
- def state_name #:nodoc:
704
- @@state_name
705
- end
706
-
707
- def fips_state #:nodoc:
708
- @@fips_state
709
- end
710
-
711
728
  private
712
729
  def normalize_address(addr)
713
730
  addr.state = normalize_state(addr.state) unless addr.state.nil?
@@ -728,13 +745,13 @@ module StreetAddress
728
745
  if state.length < 3
729
746
  state.upcase
730
747
  else
731
- @@state_code[state.downcase]
748
+ STATE_CODES[state.downcase]
732
749
  end
733
750
  end
734
751
 
735
752
  def normalize_street_type(s_type)
736
753
  s_type.downcase!
737
- s_type = @@street_type[s_type] || s_type if @@street_type_list[s_type]
754
+ s_type = STREET_TYPES[s_type] || s_type if STREET_TYPES_LIST[s_type]
738
755
  s_type.capitalize
739
756
  end
740
757
 
@@ -742,7 +759,7 @@ module StreetAddress
742
759
  if dir.length < 3
743
760
  dir.upcase
744
761
  else
745
- @@directional[dir.downcase]
762
+ DIRECTIONAL[dir.downcase]
746
763
  end
747
764
  end
748
765
  end
@@ -755,19 +772,37 @@ module StreetAddress
755
772
 
756
773
  =end
757
774
  class Address
758
- attr_accessor :number, :street, :street_type, :unit, :unit_prefix, :suffix, :prefix, :city, :state, :postal_code, :postal_code_ext, :street2, :street_type2, :suffix2, :prefix2
775
+ attr_accessor(
776
+ :number,
777
+ :street,
778
+ :street_type,
779
+ :unit,
780
+ :unit_prefix,
781
+ :suffix,
782
+ :prefix,
783
+ :city,
784
+ :state,
785
+ :postal_code,
786
+ :postal_code_ext,
787
+ :street2,
788
+ :street_type2,
789
+ :suffix2,
790
+ :prefix2
791
+ )
759
792
 
760
793
  def initialize(args)
761
- args.keys.each { |attrib| self.send("#{attrib}=", args[attrib]) }
794
+ args.keys.each do |attrib|
795
+ self.send("#{attrib}=", args[attrib])
796
+ end
797
+ return
762
798
  end
763
799
 
764
800
  def state_fips
765
- StreetAddress::US::fips_state[@state]
801
+ StreetAddress::US::FIPS_STATES[state]
766
802
  end
767
803
 
768
804
  def state_name
769
- s_name = StreetAddress::US.state_name[state]
770
- s_name.capitalize unless s_name.nil?
805
+ name = StreetAddress::US::STATE_NAMES[state] and name.capitalize
771
806
  end
772
807
 
773
808
  def intersection?
@@ -1,8 +1,8 @@
1
- require 'test/unit/testcase'
2
- require File.dirname(__FILE__) + '/../lib/street_address'
1
+ require 'test/unit'
2
+ require 'street_address'
3
3
 
4
4
 
5
- class StreetAddressUs < Test::Unit::TestCase
5
+ class StreetAddressUsTest < Test::Unit::TestCase
6
6
  def setup
7
7
  @addr1 = "2730 S Veitch St Apt 207, Arlington, VA 22206"
8
8
  @addr2 = "44 Canal Center Plaza Suite 500, Alexandria, VA 22314"
metadata CHANGED
@@ -1,69 +1,76 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: StreetAddress
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 2
10
+ version: 1.0.2
5
11
  platform: ruby
6
12
  authors:
7
13
  - Derrek Long
14
+ - Nicholas Schleuter
8
15
  autorequire:
9
16
  bindir: bin
10
17
  cert_chain: []
11
18
 
12
- date: 2008-02-29 00:00:00 -05:00
19
+ date: 2011-08-07 00:00:00 -07:00
13
20
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: hoe
17
- version_requirement:
18
- version_requirements: !ruby/object:Gem::Requirement
19
- requirements:
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 1.5.0
23
- version:
24
- description: Parses one line addresses and returns a normalized address object. This is a near direct port of the of the perl module Geo::StreetAddress::US originally written by Schuyler D. Erle. For more information see http://search.cpan.org/~sderle/Geo-StreetAddress-US-0.99/
25
- email: derrek.long@gmail.com
21
+ dependencies: []
22
+
23
+ description: |
24
+ StreetAddress::US allows you to send any string to parse and if the string is a US address returns an object of the address broken into it's substituent parts.
25
+
26
+ A port of Geo::StreetAddress::US by Schuyler D. Erle and Tim Bunce.
27
+
28
+ email: derreklong@gmail.com
26
29
  executables: []
27
30
 
28
31
  extensions: []
29
32
 
30
- extra_rdoc_files:
31
- - History.txt
32
- - Manifest.txt
33
- - README.txt
33
+ extra_rdoc_files: []
34
+
34
35
  files:
35
- - History.txt
36
- - Manifest.txt
37
- - README.txt
36
+ - README.rdoc
38
37
  - Rakefile
38
+ - LICENSE
39
39
  - lib/street_address.rb
40
40
  - test/test_street_address.rb
41
41
  has_rdoc: true
42
- homepage: http://streetaddress.rubyforge.org
42
+ homepage: https://github.com/derrek/street-address
43
+ licenses: []
44
+
43
45
  post_install_message:
44
- rdoc_options:
45
- - --main
46
- - README.txt
46
+ rdoc_options: []
47
+
47
48
  require_paths:
48
49
  - lib
49
50
  required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
50
52
  requirements:
51
53
  - - ">="
52
54
  - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
53
58
  version: "0"
54
- version:
55
59
  required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
56
61
  requirements:
57
62
  - - ">="
58
63
  - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
59
67
  version: "0"
60
- version:
61
68
  requirements: []
62
69
 
63
- rubyforge_project: streetaddress
64
- rubygems_version: 1.0.1
70
+ rubyforge_project:
71
+ rubygems_version: 1.4.2
65
72
  signing_key:
66
- specification_version: 2
67
- summary: Ruby port of the perl module Geo::StreetAddress::US to parse one line street addresses
73
+ specification_version: 3
74
+ summary: Parse Addresses into substituent parts. This gem includes US only.
68
75
  test_files:
69
76
  - test/test_street_address.rb
@@ -1,8 +0,0 @@
1
- == 1.0.1 / 2008-02-29
2
-
3
- * BUG FIX: Parse two word states correctly
4
-
5
- == 1.0.0 / 2007-06-28
6
-
7
- * Initial Version
8
-
@@ -1,6 +0,0 @@
1
- History.txt
2
- Manifest.txt
3
- README.txt
4
- Rakefile
5
- lib/street_address.rb
6
- test/test_street_address.rb
data/README.txt DELETED
@@ -1,45 +0,0 @@
1
- StreetAddress
2
- by Riderway (Derrek Long, Nicholas Schlueter)
3
- http://streetaddress.rubyforge.org
4
-
5
- == DESCRIPTION:
6
-
7
- Parses one line street addresses and returns a normalized address object.
8
-
9
- This is a near direct port of the of the perl module
10
- Geo::StreetAddress::US originally written by Schuyler D. Erle.
11
- For more information see
12
- http://search.cpan.org/~sderle/Geo-StreetAddress-US-0.99/
13
-
14
- == SYNOPSIS:
15
-
16
- Currently parses United States Addresses.
17
-
18
- === Basic Usage:
19
-
20
- StreetAddress::US.parse("1600 Pennsylvania Ave, Washington, DC, 2006")
21
-
22
- == LICENSE:
23
-
24
- (The MIT License)
25
-
26
- Copyright (c) 2007 Riderway
27
-
28
- Permission is hereby granted, free of charge, to any person obtaining
29
- a copy of this software and associated documentation files (the
30
- 'Software'), to deal in the Software without restriction, including
31
- without limitation the rights to use, copy, modify, merge, publish,
32
- distribute, sublicense, and/or sell copies of the Software, and to
33
- permit persons to whom the Software is furnished to do so, subject to
34
- the following conditions:
35
-
36
- The above copyright notice and this permission notice shall be
37
- included in all copies or substantial portions of the Software.
38
-
39
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
40
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
42
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
43
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
44
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
45
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.