Modified:
trunk/app/models/photo_receiver.rb
trunk/test/unit/photo_receiver_test.rb
Log:
Fixed invalid email errors.
Modified: trunk/app/models/photo_receiver.rb
==============================================================================
--- trunk/app/models/photo_receiver.rb (original)
+++ trunk/app/models/photo_receiver.rb Fri Dec 7 19:45:21 2007
@@ -1,22 +1,63 @@
+require "rexml/document"
+require "open-uri"
+
class PhotoReceiver < ActionMailer::Base
# email is a TMail::Mail
def receive(email)
- #email.attachments are TMail::Attachment
- #but they ignore a text/mail parts.
+ puts email.from[0]
+ # Iterate through parts of the email.
email.parts.each_with_index do |part, index|
if part.content_type =~ /^image/
@picture = Picture.new(
- :user_email => email.from,
+ :user_email => email.from[0],
:tag => email.subject,
:content_type => part.content_type.chomp,
:data => part.body,
:status => 'PENDING',
:date => Date.today
)
- @picture.save
- #Else, check for HTML formatted email.
+
elsif part.content_type =~ /text\/html/
+ # Check for Sprint Picture Mail.
+ if email.from[0] =~ /@pm.sprint.com\Z/
+ picture_uri = parse_sprint(part.body)
+ open(picture_uri) do |p|
+ @picture = Picture.new(
+ :user_email => email.from[0],
+ :tag => email.subject,
+ :content_type => p.content_type.chomp,
+ :data => p.read,
+ :status => 'PENDING',
+ :date => Date.today
+ )
+ end
+ end
end
+
end
+
+ if @picture.save
+ # Create reply email.
+ else
+ raise "Could not save picture."
+ end
+ end
+
+ private
+
+ # Sprint Picture Mail picture URL pattern. Matches "pictures.sprintpcs.com//".
+ SPRINT_URL_PATTERN = /pictures.sprintpcs.com\/\//
+
+ # Parse through Sprint Picture Mail and return the URI to the picture.
+ def parse_sprint(message)
+ doc = REXML::Document.new(message)
+ doc.elements.each("//img") do |e|
+ if e.attribute("src").to_s() =~ SPRINT_URL_PATTERN
+ return e.attribute("src").to_s()
+ end
+ end
+
+ return nil
end
+
end
Modified: trunk/test/unit/photo_receiver_test.rb
==============================================================================
--- trunk/test/unit/photo_receiver_test.rb (original)
+++ trunk/test/unit/photo_receiver_test.rb Fri Dec 7 19:45:21 2007
@@ -18,8 +18,12 @@
end
def test_photo_receiver
- email_text = read_fixture('hello-world.mail').join
- PhotoReceiver.receive(email_text)
+ puts "Testing hello-world.mail"
+ email_text = read_fixture('hello-world.mail').join
+ PhotoReceiver.receive(email_text)
+ puts "Testing Sprint Picture Mail."
+ email_text2 = read_fixture('sprint_picturemail.mail').join
+ PhotoReceiver.receive(email_text2)
end
private