From 6210d3033d74c8e507058b2434dfed21eee90d8e Mon Sep 17 00:00:00 2001 From: epall Date: Sat, 14 Jun 2008 22:12:13 -0700 Subject: [PATCH] Modified sample_sms.txt to be subbed into in a sane way. Added ability to set context, due date, and show_from date from within an emailed message. --- app/models/sms_gateway.rb | 33 ++++++++++++++- test/fixtures/contexts.yml | 9 ++++ test/fixtures/sample_sms.txt | 2 +- test/unit/sms_gateway_test.rb | 78 ++++++++++++++++++++++++++++++++++- 4 files changed, 118 insertions(+), 4 deletions(-) diff --git a/app/models/sms_gateway.rb b/app/models/sms_gateway.rb index 563fa8a4..d7b3cefa 100644 --- a/app/models/sms_gateway.rb +++ b/app/models/sms_gateway.rb @@ -25,9 +25,38 @@ class SMSGateway < ActionMailer::Base end end - unless user.todos.find(:first, :conditions => {:description => description}) # stupid T-Mobile often sends the same message multiple times - todo = user.todos.create(:context => context, :description => description, :notes => notes) + 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 end end diff --git a/test/fixtures/contexts.yml b/test/fixtures/contexts.yml index c9d3d5e0..49ce9d2f 100644 --- a/test/fixtures/contexts.yml +++ b/test/fixtures/contexts.yml @@ -122,3 +122,12 @@ inbox: user_id: 4 created_at: <%= today %> updated_at: <%= today %> + +anothercontext: + id: 14 + name: anothercontext + position: 2 + hide: false + user_id: 4 + created_at: <%= today %> + updated_at: <%= today %> diff --git a/test/fixtures/sample_sms.txt b/test/fixtures/sample_sms.txt index 002cc204..86b449a3 100644 --- a/test/fixtures/sample_sms.txt +++ b/test/fixtures/sample_sms.txt @@ -9,4 +9,4 @@ Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: 7bit Importance: Normal -This is a todo 4112093 \ No newline at end of file +message_content \ No newline at end of file diff --git a/test/unit/sms_gateway_test.rb b/test/unit/sms_gateway_test.rb index 1c3879c8..6852566e 100644 --- a/test/unit/sms_gateway_test.rb +++ b/test/unit/sms_gateway_test.rb @@ -19,7 +19,7 @@ class SMSGatewayTest < Test::Rails::TestCase # assert some stuff about it being created assert_equal(todo_count+1, Todo.count) - message_todo = Todo.find(:first, :conditions => {:description => "This is a todo 4112093"}) + message_todo = Todo.find(:first, :conditions => {:description => "message_content"}) assert_not_nil(message_todo) assert_equal(@inbox, message_todo.context) @@ -56,4 +56,80 @@ class SMSGatewayTest < Test::Rails::TestCase SMSGateway.receive(badmessage) assert_equal(todo_count, Todo.count) end + + 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') + + SMSGateway.receive(valid_context_msg) + valid_context_todo = Todo.find(:first, :conditions => {:description => "this is a task"}) + assert_not_nil(valid_context_todo) + assert_equal(contexts(:anothercontext), valid_context_todo.context) + + SMSGateway.receive(invalid_context_msg) + invalid_context_todo = Todo.find(:first, :conditions => {:description => 'notacontext: this is 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!') + + SMSGateway.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) + + SMSGateway.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) + + SMSGateway.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) + + SMSGateway.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!') + + SMSGateway.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) + + SMSGateway.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.tomorrow.next_week, valid_show_todo2.show_from) + + SMSGateway.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) + + SMSGateway.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