Indirizzo 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{Indirizzo}
3
- s.version = "0.1.1"
3
+ s.version = "0.1.3"
4
4
 
5
5
  s.authors = [%q{Dave Worth}]
6
6
  s.date = %q{2011-12-14}
data/README.md CHANGED
@@ -27,6 +27,28 @@ require 'Indirizzo'
27
27
  Indirizzo::Address.new("some address")
28
28
  ```
29
29
 
30
+ ### Options
31
+
32
+ `#new` takes a string or a (pre-parsed address) hash as its first parameter and
33
+ an options hash.
34
+
35
+ In the case of a string specifying and address, Indirizzo will do its best to
36
+ parse any matter of string, though results can be complicated. In the cases
37
+ where things are complicated the various attributes in Indirizzo do their best
38
+ to keep all reasonable answers in an array which you can inspect. (ex: "1600
39
+ Pensylvania Washington", in this case the state is difficult to determine so
40
+ both "Pennsylvania" and "Washington" are returned for City and Street)
41
+
42
+ In the case of the pre-parsed address hash the keys of the
43
+ hash be symbols matching the various Address fields in Indirizzo (specifically
44
+ `:prenum`, :`number`, `:sufnum`, `:street`, `:city`, `:state`, `:zip`, `:plus4`,
45
+ and `:country`)
46
+
47
+ Currently only one option is supported for the option hash:
48
+
49
+ * `:expand_streets` - a boolean which determines if "1 First St"'s street parameter
50
+ is expanded into "1 st", "first st", and "one st" or simply left as "first st"
51
+
30
52
  ## License
31
53
 
32
54
  Indirizzo is a direct derivative of [Geocoder::US 2.0](https://github.com/geocommons/geocoder)
@@ -25,9 +25,12 @@ module Indirizzo
25
25
  attr_accessor :state
26
26
  attr_accessor :zip, :plus4
27
27
  attr_accessor :country
28
+ attr_accessor :options
28
29
 
29
30
  # Takes an address or place name string as its sole argument.
30
- def initialize (text)
31
+ def initialize (text, options={})
32
+ @options = {:expand_streets => true}.merge(options)
33
+
31
34
  raise ArgumentError, "no text provided" unless text and !text.empty?
32
35
  if text.class == Hash
33
36
  @text = ""
@@ -59,16 +62,16 @@ module Indirizzo
59
62
  @number = ""
60
63
  if !@street.nil?
61
64
  if text[:number].nil?
62
- @street.map! { |single_street|
63
- single_street.downcase!
64
- @number = single_street.scan(Match[:number])[0].reject{|n| n.nil? || n.empty?}.first.to_s
65
- single_street.sub! @number, ""
66
- single_street.sub! /^\s*,?\s*/o, ""
67
- }
68
- else
65
+ @street.map! { |single_street|
66
+ single_street.downcase!
67
+ @number = single_street.scan(Match[:number])[0].reject{|n| n.nil? || n.empty?}.first.to_s
68
+ single_street.sub! @number, ""
69
+ single_street.sub! /^\s*,?\s*/o, ""
70
+ }
71
+ else
69
72
  @number = text[:number].to_s
70
73
  end
71
- @street = expand_streets(@street)
74
+ @street = expand_streets(@street) if @options[:expand_streets]
72
75
  street_parts
73
76
  end
74
77
  @city = []
@@ -79,10 +82,10 @@ module Indirizzo
79
82
  @city.push("")
80
83
  end
81
84
  if !text[:region].nil?
82
- # @state = []
83
- @state = text[:region]
85
+ # @state = []
86
+ @state = text[:region]
84
87
  if @state.length > 2
85
- # full_state = @state.strip # special case: New York
88
+ # full_state = @state.strip # special case: New York
86
89
  @state = State[@state]
87
90
  end
88
91
  elsif !text[:country].nil?
@@ -94,7 +97,7 @@ module Indirizzo
94
97
  @zip = text[:postal_code]
95
98
  @plus4 = text[:plus4]
96
99
  if !@zip
97
- @zip = @plus4 = ""
100
+ @zip = @plus4 = ""
98
101
  end
99
102
  end
100
103
  end
@@ -180,7 +183,7 @@ module Indirizzo
180
183
  street_search_end_index = [state_index,zip_index,text.length].reject(&:nil?).min-1
181
184
  @street = text[number_end_index+1..street_search_end_index].scan(Match[:street]).map { |s| s and s.strip }
182
185
 
183
- @street = expand_streets(@street)
186
+ @street = expand_streets(@street) if @options[:expand_streets]
184
187
  # SPECIAL CASE: 1600 Pennsylvania 20050
185
188
  @street << @full_state if @street.empty? and @state.downcase != @full_state.downcase
186
189
 
@@ -5,10 +5,12 @@ require 'indirizzo/address'
5
5
  include Indirizzo
6
6
 
7
7
  class TestAddress < Test::Unit::TestCase
8
+
8
9
  def test_new
9
10
  addr = Address.new("1600 Pennsylvania Av., Washington DC")
10
11
  assert_equal "1600 Pennsylvania Av, Washington DC", addr.text
11
12
  end
13
+
12
14
  def test_expand_numbers
13
15
  num_list = ["5", "fifth", "five"]
14
16
  num_list.each {|n|
@@ -17,6 +19,19 @@ class TestAddress < Test::Unit::TestCase
17
19
  }
18
20
  end
19
21
 
22
+ def test_expand_street
23
+ addr = Address.new("1 First St, Atlanta GA, 12345")
24
+ expected_streets = ["1 st", "first st", "one st"]
25
+ expected_streets.each_with_index do |street, index|
26
+ addr.street[index] = expected_streets[index]
27
+ end
28
+ end
29
+
30
+ def test_no_expand_street
31
+ addr = Address.new("1 First St, Atlanta GA, 12345", :expand_streets => false)
32
+ assert_equal addr.street.first, "first st"
33
+ end
34
+
20
35
  def test_po_box
21
36
  addr_po = Address.new "PO Box 1111 Herndon VA 20171"
22
37
  assert addr_po.po_box?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Indirizzo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-12-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70302219305600 !ruby/object:Gem::Requirement
16
+ requirement: &70264404189660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70302219305600
24
+ version_requirements: *70264404189660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: cover_me
27
- requirement: &70302219305160 !ruby/object:Gem::Requirement
27
+ requirement: &70264404189220 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70302219305160
35
+ version_requirements: *70264404189220
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: awesome_print
38
- requirement: &70302219304740 !ruby/object:Gem::Requirement
38
+ requirement: &70264404188800 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70302219304740
46
+ version_requirements: *70264404188800
47
47
  description: Indirizzo is simply an extraction of the US Street Address parsing code
48
48
  from Geocoder::US
49
49
  email: dave@highgroove.com