StreetAddress 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,3 @@
1
- StreetAddress
2
- by Derrek Long
3
- https://github.com/derrek/street-address
4
-
5
1
  == DESCRIPTION:
6
2
 
7
3
  Parses one line street addresses and returns a normalized address object.
@@ -30,6 +26,20 @@ Parse United States addresses.
30
26
  address.to_s(:line1) # 1600 Pennsylvania Ave
31
27
  StreetAddress::US.parse("1600 Pennsylvania Ave") # nil not a full address
32
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
+
33
43
  === Gemfile:
34
44
  To use from your Gemfile:
35
- gem 'StreetAddress', '1.0.2', :require => "street_address"
45
+ gem 'StreetAddress', '1.0.3', :require => "street_address"
@@ -64,7 +64,7 @@
64
64
 
65
65
  module StreetAddress
66
66
  class US
67
- VERSION = '1.0.3'
67
+ VERSION = '1.0.4'
68
68
  DIRECTIONAL = {
69
69
  "north" => "N",
70
70
  "northeast" => "NE",
@@ -576,13 +576,15 @@ module StreetAddress
576
576
  :number_regexp,
577
577
  :fraction_regexp,
578
578
  :state_regexp,
579
+ :city_and_state_regexp,
579
580
  :direct_regexp,
580
581
  :zip_regexp,
581
582
  :corner_regexp,
582
583
  :unit_regexp,
583
584
  :street_regexp,
584
585
  :place_regexp,
585
- :address_regexp
586
+ :address_regexp,
587
+ :informal_address_regexp
586
588
  )
587
589
  end
588
590
 
@@ -590,6 +592,12 @@ module StreetAddress
590
592
  self.number_regexp = '\d+-?\d*'
591
593
  self.fraction_regexp = '\d+\/\d+'
592
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
+
593
601
  self.direct_regexp = DIRECTIONAL.keys.join("|") +
594
602
  "|" +
595
603
  DIRECTIONAL.values.sort{ |a,b|
@@ -598,7 +606,7 @@ module StreetAddress
598
606
  f = x.gsub(/(\w)/, '\1.')
599
607
  [Regexp::quote(f), Regexp::quote(x)]
600
608
  }.join("|")
601
- self.zip_regexp = '(\d{5})(?:-(\d{4}))?'
609
+ self.zip_regexp = '(\d{5})(?:-?(\d{4})?)'
602
610
  self.corner_regexp = '(?:\band\b|\bat\b|&|\@)'
603
611
  self.unit_regexp = '(?:(su?i?te|p\W*[om]\W*b(?:ox)?|dept|apt|apartment|ro*m|fl|unit|box)\W+|\#\W*)([\w-]+)'
604
612
  self.street_regexp =
@@ -621,10 +629,7 @@ module StreetAddress
621
629
  )
622
630
  )'
623
631
  self.place_regexp =
624
- '(?:
625
- ([^\d,]+?)\W+
626
- ($' + state_regexp + ')\W*
627
- )?
632
+ '(?:' + city_and_state_regexp + '\W*)?
628
633
  (?:' + zip_regexp + ')?'
629
634
 
630
635
  self.address_regexp =
@@ -636,26 +641,39 @@ module StreetAddress
636
641
  place_regexp +
637
642
  '\W*\Z'
638
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
+
639
652
  =begin rdoc
640
653
 
641
654
  parses either an address or intersection and returns an instance of
642
655
  StreetAddress::US::Address or nil if the location cannot be parsed
656
+
657
+ pass the arguement, :informal => true, to make parsing more lenient
643
658
 
644
659
  ====example
645
660
  StreetAddress::US.parse('1600 Pennsylvania Ave Washington, DC 20006')
646
661
  or:
647
662
  StreetAddress::US.parse('Hollywood & Vine, Los Angeles, CA')
663
+ or
664
+ StreetAddress::US.parse("1600 Pennsylvania Ave", :informal => true)
648
665
 
649
666
  =end
650
667
  class << self
651
- def parse(location)
668
+ def parse(location, args = {})
652
669
  if Regexp.new(corner_regexp, Regexp::IGNORECASE).match(location)
653
- parse_intersection(location);
670
+ parse_intersection(location)
671
+ elsif args[:informal]
672
+ parse_address(location) || parse_informal_address(location)
654
673
  else
655
674
  parse_address(location);
656
675
  end
657
676
  end
658
-
659
677
  =begin rdoc
660
678
 
661
679
  parses only an intersection and returnsan instance of
@@ -724,6 +742,28 @@ module StreetAddress
724
742
  )
725
743
  )
726
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
727
767
 
728
768
  private
729
769
  def normalize_address(addr)
@@ -17,6 +17,44 @@ class StreetAddressUsTest < Test::Unit::TestCase
17
17
 
18
18
  end
19
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
+
20
58
  def test_parse
21
59
  assert_equal StreetAddress::US.parse("&"), nil
22
60
  assert_equal StreetAddress::US.parse(" and "), nil
metadata CHANGED
@@ -1,76 +1,58 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: StreetAddress
3
- version: !ruby/object:Gem::Version
4
- hash: 17
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 3
10
- version: 1.0.3
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Derrek Long
14
9
  - Nicholas Schleuter
15
10
  autorequire:
16
11
  bindir: bin
17
12
  cert_chain: []
18
-
19
- date: 2011-09-21 00:00:00 -07:00
20
- default_executable:
13
+ date: 2013-01-17 00:00:00.000000000 Z
21
14
  dependencies: []
15
+ description: ! 'StreetAddress::US allows you to send any string to parse and if the
16
+ string is a US address returns an object of the address broken into it''s substituent
17
+ parts.
18
+
22
19
 
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
20
  A port of Geo::StreetAddress::US by Schuyler D. Erle and Tim Bunce.
27
21
 
22
+ '
28
23
  email: derreklong@gmail.com
29
24
  executables: []
30
-
31
25
  extensions: []
32
-
33
26
  extra_rdoc_files: []
34
-
35
- files:
27
+ files:
36
28
  - README.rdoc
37
29
  - Rakefile
38
30
  - LICENSE
39
31
  - lib/street_address.rb
40
32
  - test/test_street_address.rb
41
- has_rdoc: true
42
33
  homepage: https://github.com/derrek/street-address
43
34
  licenses: []
44
-
45
35
  post_install_message:
46
36
  rdoc_options: []
47
-
48
- require_paths:
37
+ require_paths:
49
38
  - lib
50
- required_ruby_version: !ruby/object:Gem::Requirement
39
+ required_ruby_version: !ruby/object:Gem::Requirement
51
40
  none: false
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- hash: 3
56
- segments:
57
- - 0
58
- version: "0"
59
- required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
46
  none: false
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- hash: 3
65
- segments:
66
- - 0
67
- version: "0"
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
68
51
  requirements: []
69
-
70
52
  rubyforge_project:
71
- rubygems_version: 1.4.2
53
+ rubygems_version: 1.8.24
72
54
  signing_key:
73
55
  specification_version: 3
74
56
  summary: Parse Addresses into substituent parts. This gem includes US only.
75
- test_files:
57
+ test_files:
76
58
  - test/test_street_address.rb