coderifous-address_extractor 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
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 Yarp.com.
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.0') do |p|
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"
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{address_extractor}
3
- s.version = "0.1.0"
3
+ s.version = "0.1.1"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["Jim Garvin"]
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coderifous-address_extractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Garvin