From 6e4258cfbc590cead5845c7d0441c6cf19a93f9e Mon Sep 17 00:00:00 2001 From: Eric Allen Date: Sun, 21 Sep 2008 09:36:32 -0700 Subject: [PATCH] Use Rich Todo API for Message Gateway --- app/models/message_gateway.rb | 32 +-------------- test/unit/message_gateway_test.rb | 65 ++----------------------------- 2 files changed, 5 insertions(+), 92 deletions(-) diff --git a/app/models/message_gateway.rb b/app/models/message_gateway.rb index a59927ad..8fcb78d3 100644 --- a/app/models/message_gateway.rb +++ b/app/models/message_gateway.rb @@ -28,35 +28,7 @@ class MessageGateway < ActionMailer::Base # stupid T-Mobile often sends the same message multiple times return if user.todos.find(:first, :conditions => {:description => description}) - # parse context - context_data = description.match(/^([^ ]*): (.*)/) - if context_data - context_name = context_data[1] - custom_context = user.contexts.find(:first, :conditions => {:name => context_name}) - if custom_context - context = custom_context - description = context_data[2] - end - end - - # parse due date - due_regex = / ?due:([0-9\/-]{3,})/ - due_date = description.match(due_regex)[1] rescue nil - if due_date - #strip from description - description.sub!(due_regex, '').strip! - end - - # parse due date - show_regex = / ?show:([0-9\/-]{3,})/ - show_date = description.match(show_regex)[1] rescue nil - if show_date - #strip from description - description.sub!(show_regex, '').strip! - end - - # p "creating todo with description '#{description}', show from #{show_date}, context #{context.name}" - todo = user.todos.create(:context => context, :description => description, :notes => notes, :due => due_date, :show_from => show_date) - # p todo.validate + todo = Todo.from_rich_message(user, context.id, description, notes) + todo.save! end end diff --git a/test/unit/message_gateway_test.rb b/test/unit/message_gateway_test.rb index 12b6aadf..32f77159 100644 --- a/test/unit/message_gateway_test.rb +++ b/test/unit/message_gateway_test.rb @@ -60,8 +60,8 @@ class MessageGatewayTest < Test::Rails::TestCase def test_direct_to_context message = File.read(File.join(RAILS_ROOT, 'test', 'fixtures', 'sample_sms.txt')) - valid_context_msg = message.gsub('message_content', 'anothercontext: this is a task') - invalid_context_msg = message.gsub('message_content', 'notacontext: this is a task') + valid_context_msg = message.gsub('message_content', 'this is a task @ anothercontext') + invalid_context_msg = message.gsub('message_content', 'this is also a task @ notacontext') MessageGateway.receive(valid_context_msg) valid_context_todo = Todo.find(:first, :conditions => {:description => "this is a task"}) @@ -69,67 +69,8 @@ class MessageGatewayTest < Test::Rails::TestCase assert_equal(contexts(:anothercontext), valid_context_todo.context) MessageGateway.receive(invalid_context_msg) - invalid_context_todo = Todo.find(:first, :conditions => {:description => 'notacontext: this is a task'}) + invalid_context_todo = Todo.find(:first, :conditions => {:description => 'this is also a task'}) assert_not_nil(invalid_context_todo) assert_equal(@inbox, invalid_context_todo.context) end - - def test_due_date - message = File.read(File.join(RAILS_ROOT, 'test', 'fixtures', 'sample_sms.txt')) - - valid_due_msg1 = message.gsub('message_content', 'do something tomorrow due:6/15/2008') - valid_due_msg2 = message.gsub('message_content', 'do something tomorrow due:6/28/2008 and remember it!') - valid_due_msg3 = message.gsub('message_content', 'due:1/28/2008 funky!') - invalid_due_msg1 = message.gsub('message_content', 'do something tomorrow due:xxxx and remember it!') - - MessageGateway.receive(valid_due_msg1) - valid_due_todo1 = Todo.find(:first, :conditions => {:description => "do something tomorrow"}) - assert_not_nil(valid_due_todo1) - assert_equal(Date.civil(2008, 6, 15), valid_due_todo1.due) - - MessageGateway.receive(valid_due_msg2) - valid_due_todo2 = Todo.find(:first, :conditions => {:description => "do something tomorrow and remember it!"}) - assert_not_nil(valid_due_todo2) - assert_equal(Date.civil(2008, 6, 28), valid_due_todo2.due) - - MessageGateway.receive(valid_due_msg3) - valid_due_todo3 = Todo.find(:first, :conditions => {:description => "funky!"}) - assert_not_nil(valid_due_todo3) - assert_equal(Date.civil(2008, 1, 28), valid_due_todo3.due) - - MessageGateway.receive(invalid_due_msg1) - invalid_due_todo1 = Todo.find(:first, :conditions => {:description => "do something tomorrow due:xxxx and remember it!"}) - assert_not_nil(invalid_due_todo1) - assert_nil(invalid_due_todo1.due) - end - - def test_show_date - message = File.read(File.join(RAILS_ROOT, 'test', 'fixtures', 'sample_sms.txt')) - - valid_show_msg1 = message.gsub('message_content', "do something tomorrow show:#{Date.tomorrow.to_s}") - valid_show_msg2 = message.gsub('message_content', "do something next week show:#{Date.today.next_week.to_s} and remember it!") - valid_show_msg3 = message.gsub('message_content', "show:#{Date.tomorrow.to_s} alternative format") - invalid_show_msg1 = message.gsub('message_content', 'do something tomorrow show:xxxx and remember it!') - - MessageGateway.receive(valid_show_msg1) - valid_show_todo1 = Todo.find(:first, :conditions => {:description => "do something tomorrow"}) - assert_not_nil(valid_show_todo1) - assert_equal(Date.tomorrow, valid_show_todo1.show_from) - - MessageGateway.receive(valid_show_msg2) - valid_show_todo2 = Todo.find(:first, :conditions => {:description => "do something next week and remember it!"}) - assert_not_nil(valid_show_todo2) - assert_equal(Date.today.next_week, valid_show_todo2.show_from) - - MessageGateway.receive(valid_show_msg3) - valid_show_todo3 = Todo.find(:first, :conditions => {:description => "alternative format"}) - # p @user.todos.last - assert_not_nil(valid_show_todo3) - assert_equal(Date.tomorrow, valid_show_todo3.show_from) - - MessageGateway.receive(invalid_show_msg1) - invalid_show_todo1 = Todo.find(:first, :conditions => {:description => "do something tomorrow show:xxxx and remember it!"}) - assert_not_nil(invalid_show_todo1) - assert_nil(invalid_show_todo1.show_from) - end end