coderifous-address_extractor 0.1.0 → 0.1.1
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/README.textile +4 -1
- data/Rakefile +1 -1
- data/address_extractor.gemspec +1 -1
- data/lib/address_extractor.rb +16 -2
- data/test/test_address_extractor.rb +7 -0
- metadata +1 -1
data/README.textile
CHANGED
@@ -5,6 +5,9 @@ Find and/or replace mailing addresses in strings.
|
|
5
5
|
h2. Examples
|
6
6
|
|
7
7
|
<pre><code>
|
8
|
+
require 'rubygems'
|
9
|
+
require 'address_extractor'
|
10
|
+
|
8
11
|
string = <<EOF
|
9
12
|
Please send the package to 123 Foo St., Someplace FL
|
10
13
|
|
@@ -38,7 +41,7 @@ end
|
|
38
41
|
|
39
42
|
h3. About
|
40
43
|
|
41
|
-
Written by Jim Garvin at RubyConf '08 at the request of Chris Murphy and Ryan McGeary so they could use it in
|
44
|
+
Written by Jim Garvin ("coderifous":http://github.com/coderifous) at RubyConf '08 at the request of Chris Murphy ("chmurph2":http://github.com/chmurph2) and Ryan McGeary ("rmm5t":http://github.com/rmm5t) so they could use it in their awesome "invitation and survey app":http://yarp.com.
|
42
45
|
|
43
46
|
You can use it, too.
|
44
47
|
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('address_extractor', '0.1.
|
5
|
+
Echoe.new('address_extractor', '0.1.1') do |p|
|
6
6
|
p.description = "Give it text. It finds addresses in it."
|
7
7
|
p.url = "http://github.com/coderifous/address_extractor"
|
8
8
|
p.author = "Jim Garvin"
|
data/address_extractor.gemspec
CHANGED
data/lib/address_extractor.rb
CHANGED
@@ -1,21 +1,32 @@
|
|
1
1
|
class AddressExtractor
|
2
2
|
class << self
|
3
3
|
|
4
|
+
# Returns hash for address if address found.
|
5
|
+
# Returns nil if no address found.
|
4
6
|
def first_address(string)
|
5
7
|
hashify_results string.scan(ADDRESS_PATTERN).first
|
6
8
|
end
|
7
|
-
|
9
|
+
|
10
|
+
# Returns array of hashes for each address found.
|
11
|
+
# Returns empty array if no addresses found.
|
8
12
|
def find_addresses(string)
|
9
13
|
string.scan(ADDRESS_PATTERN).collect { |a| hashify_results(a) }.compact
|
10
14
|
end
|
11
15
|
|
16
|
+
# Pass it a block that recieves 2 parameters:
|
17
|
+
# address hash
|
18
|
+
# matched address string ($&)
|
19
|
+
# Whatever your block returns will be used for the substition.
|
20
|
+
# Returns new string with substition applied to first identified address.
|
21
|
+
# If no address found, returns same string unaltered.
|
12
22
|
def replace_first_address(string)
|
13
23
|
hash = first_address(string)
|
14
24
|
string.sub(ADDRESS_PATTERN) do |match|
|
15
25
|
yield(hash, $&)
|
16
26
|
end
|
17
27
|
end
|
18
|
-
|
28
|
+
|
29
|
+
# Same as +replace_first_address+ but applies substition to all identified addresses.
|
19
30
|
def replace_addresses(string)
|
20
31
|
string.gsub(ADDRESS_PATTERN) do |match|
|
21
32
|
hash = hashify_results match.scan(ADDRESS_PATTERN).first
|
@@ -23,7 +34,10 @@ class AddressExtractor
|
|
23
34
|
end
|
24
35
|
end
|
25
36
|
|
37
|
+
private
|
38
|
+
|
26
39
|
def hashify_results(matches)
|
40
|
+
return nil if matches.nil?
|
27
41
|
result = { }
|
28
42
|
capture_index = 0
|
29
43
|
CAPTURE_MAP.each do |field|
|
@@ -33,6 +33,13 @@ class AddressExtractorTest < Test::Unit::TestCase
|
|
33
33
|
assert string =~ /via mail at:\n skidoosh/
|
34
34
|
end
|
35
35
|
|
36
|
+
def test_no_addresses_found
|
37
|
+
assert_nil AddressExtractor.first_address("foo")
|
38
|
+
assert_equal [], AddressExtractor.find_addresses("foo")
|
39
|
+
assert_equal "foo", AddressExtractor.replace_first_address("foo")
|
40
|
+
assert_equal "foo", AddressExtractor.replace_addresses("foo")
|
41
|
+
end
|
42
|
+
|
36
43
|
module Helpers
|
37
44
|
def assert_first_address(a)
|
38
45
|
assert_not_nil a
|