<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>bin/mail_receiver.rb</filename>
    </added>
    <added>
      <filename>bin/mail_receiver_ctl.rb</filename>
    </added>
    <added>
      <filename>config/mail.sample.yml</filename>
    </added>
    <added>
      <filename>spec/fixtures/emails/red</filename>
    </added>
    <added>
      <filename>spec/fixtures/emails/red_address_two_lines</filename>
    </added>
    <added>
      <filename>spec/fixtures/emails/red_address_two_lines_sig</filename>
    </added>
    <added>
      <filename>spec/fixtures/emails/red_mixedcase_subject</filename>
    </added>
    <added>
      <filename>spec/fixtures/emails/red_no_body</filename>
    </added>
    <added>
      <filename>spec/fixtures/emails/red_no_subject</filename>
    </added>
    <added>
      <filename>spec/fixtures/emails/red_uppercase_subject</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,4 +1,21 @@
 class Location &lt; ActiveRecord::Base
+  def self.existing_address(location)
+    location.send :attach_geocode
+    # find others with the same address
+    Location.first(:conditions =&gt; {:street =&gt; location.street, :city =&gt; location.city, :state =&gt; location.state, :zip =&gt; location.zip})
+  end
+  
+  def self.new_from_email(source)
+    attrs, email = {}, TMail::Mail.parse(source)
+    attrs[:signs]   = email.subject.blank? ? '' : email.subject.downcase.strip.titleize
+    attrs[:street]  = parse_address(email.body)
+    new(attrs)
+  end
+  
+  def self.parse_address(body)
+    body.split(&quot;\n\n&quot;).first
+  end
+  
   SIGN_OPTIONS = {
     'Blue'   =&gt; 'Obama &amp; Democrats',
     'Red'    =&gt; 'McCain &amp; Republicans',
@@ -50,14 +67,8 @@ class Location &lt; ActiveRecord::Base
   def self.state_count 
     count(:select =&gt; 'DISTINCT(state)')
   end
-
-private
-  def self.existing_address(location)
-    location.send :attach_geocode
-    # find others with the same address
-    Location.first(:conditions =&gt; {:street =&gt; location.street, :city =&gt; location.city, :state =&gt; location.state, :zip =&gt; location.zip})
-  end
   
+private
   def check_geocode_precision
     attach_geocode
     errors.add_to_base &quot;We can't find a precise enough address match.&quot; if geocode.blank? || geocode.precision != 'address'</diff>
      <filename>app/models/location.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,17 +1,48 @@
 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
 
 describe Location do
-  before(:each) do
-    @valid_attributes = {
-      :street =&gt; &quot;value for street&quot;,
-      :city =&gt; &quot;value for city&quot;,
-      :state =&gt; &quot;value for state&quot;,
-      :zip =&gt; &quot;value for zip&quot;,
-      :signs =&gt; &quot;Blue&quot;
-    }
-  end
-
-  it &quot;should create a new instance given valid attributes&quot; do
-    Location.create!(@valid_attributes)
+  describe &quot;#new_from_email&quot; do
+    it &quot;should be subject&quot; do
+      location = Location.new_from_email(email_fixture(:red_no_subject))
+      location.should have_error_on(:signs)
+    end
+    
+    it &quot;should require body&quot; do
+      location = Location.new_from_email(email_fixture(:red_no_body))
+      location.valid?
+      location.errors.full_messages.should include(&quot;We can't find a precise enough address match.&quot;)
+    end
+    
+    it &quot;should set street to email body&quot; do
+      location = Location.new_from_email(email_fixture(:red))
+      location.street.should == '1600 Pennsylvania Ave. Washington, D.C.'
+    end
+    
+    it &quot;should set sign equal to subject&quot; do
+      location = Location.new_from_email(email_fixture(:red))
+      location.signs.should == 'Red'
+    end
+    
+    it &quot;should work with mixed case subject&quot; do
+      location = Location.new_from_email(email_fixture(:red_mixedcase_subject))
+      location.signs.should == 'Red'
+    end
+    
+    it &quot;should work with upper case subject&quot; do
+      location = Location.new_from_email(email_fixture(:red_uppercase_subject))
+      location.signs.should == 'Red'
+    end
+    
+    it &quot;should work with multi line body&quot; do
+      location = Location.new_from_email(email_fixture(:red_address_two_lines))
+      location.valid?
+      location.to_location.to_s.should == &quot;1600 Pennsylvania Ave Nw\nWashington, DC 20006&quot;
+    end
+    
+    it &quot;should work with multi line body and email signature&quot; do
+      location = Location.new_from_email(email_fixture(:red_address_two_lines_sig))
+      location.valid?
+      location.to_location.to_s.should == &quot;1600 Pennsylvania Ave Nw\nWashington, DC 20006&quot;
+    end
   end
 end</diff>
      <filename>spec/models/location_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -45,3 +45,7 @@ Spec::Runner.configure do |config|
   # 
   # For more information take a look at Spec::Example::Configuration and Spec::Runner
 end
+
+def email_fixture(name)
+  File.read(File.join(File.dirname(__FILE__), 'fixtures', 'emails', name.to_s))
+end
\ No newline at end of file</diff>
      <filename>spec/spec_helper.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>51d2e57accc841ef4048e1ea76413e36c484890b</id>
    </parent>
    <parent>
      <id>4870b7aee4107494c5f93c3ea40151f6cd29a64f</id>
    </parent>
  </parents>
  <author>
    <name>Daniel Morrison</name>
    <email>daniel@collectiveidea.com</email>
  </author>
  <url>http://github.com/collectiveidea/yardvote.com/commit/948a7b6bf78a81eed50668472ddd2789f6aa8775</url>
  <id>948a7b6bf78a81eed50668472ddd2789f6aa8775</id>
  <committed-date>2008-10-23T14:34:48-07:00</committed-date>
  <authored-date>2008-10-23T14:34:48-07:00</authored-date>
  <message>Merge branch 'master' of git://github.com/jnunemaker/yardvote.com</message>
  <tree>1bd6396f283354baa2fdf93d7bdf7fcbb4aa3651</tree>
  <committer>
    <name>Daniel Morrison</name>
    <email>daniel@collectiveidea.com</email>
  </committer>
</commit>
