mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-29 13:28:49 +01:00
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.
This commit is contained in:
parent
dc0c5bffa4
commit
6210d3033d
4 changed files with 118 additions and 4 deletions
|
|
@ -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
|
||||
|
|
|
|||
9
test/fixtures/contexts.yml
vendored
9
test/fixtures/contexts.yml
vendored
|
|
@ -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 %>
|
||||
|
|
|
|||
2
test/fixtures/sample_sms.txt
vendored
2
test/fixtures/sample_sms.txt
vendored
|
|
@ -9,4 +9,4 @@ Content-Type: text/plain;charset=utf-8
|
|||
Content-Transfer-Encoding: 7bit
|
||||
Importance: Normal
|
||||
|
||||
This is a todo 4112093
|
||||
message_content
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue