Skip to content

Commit

Permalink
'retry' command
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-burns committed Nov 11, 2010
1 parent b999ee7 commit 0da6646
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
10 changes: 10 additions & 0 deletions app/models/meetup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def self.proposed
where('state = "proposed"')
end

# All dates that are not yet canceled but are not actively being searched.
def self.retryable
where('state = "retryable"')
end

# All meetups for the given user
def self.for(user)
where('meetups.first_user_id = :id OR meetups.second_user_id = :id', :id => user.id)
Expand Down Expand Up @@ -60,6 +65,11 @@ def cancel!
self.save!
end

def retryable!
self.state = 'retryable'
self.save!
end

def schedule_with!(second_user)
self.state = 'scheduled'
self.second_user = second_user
Expand Down
27 changes: 25 additions & 2 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def self.handle_incoming(phone_number, message_text)
handle_texting_proxy(user, $1)
elsif message_text =~ /^\s*#{COMMANDS[:quit].gsub(' ','.*')}/i
handle_safeword(user)
elsif message_text =~ /^\s*#{COMMANDS[:retry].gsub(' ','.*')}/i
handle_retry(user)
else
handle_unknown(user)
end
Expand Down Expand Up @@ -116,6 +118,27 @@ def self.handle_new_date(user)
end
end

def self.handle_retry(user)
if within_dating_hours?
if meetup = user.founded_meetups.retryable.last
meetup.unschedule!
user.matching.first(5).each do |matching_user|
Offer.create(:offered_user => matching_user, :meetup => meetup)
end
QUEUE.enqueue_at(5.minutes.from_now, RejectMessageDelayer, :user_id => user.id)
user.tell("Trying to get you a date. Back in five.")
else
handle_unknown_retry(user)
end
else
outside_dating_hours(user)
end
end

def self.handle_unknown_retry(user)
raise "handle_unknown_retry"
end

def self.within_dating_hours?
now = Time.zone.now
now.hour >= DATING_START.hour &&
Expand Down Expand Up @@ -184,12 +207,12 @@ def self.handle_dfln(user, message)
def self.handle_no_responses(user)
meetup = user.founded_meetups.unscheduled.first
if !meetup.nil?
user.tell("We called every number in our little black book, but only got answering machines. Try again later? Reply '#{COMMANDS[:new_date]}' to start again.")
user.tell("We called every number in our little black book, but only got answering machines. Try again with '#{COMMANDS[:retry]}'.")
meetup.offers.pending.each do |offer|
offer.offered_user.tell("Too slow! Would you like to get a date? Reply '#{COMMANDS[:new_date]}'.")
offer.cancel!
end
meetup.cancel!
meetup.retryable!
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class User < ActiveRecord::Base
attr_accessor :secret_code_confirmation

def self.without_offers
where('users.id NOT IN (SELECT offered_user_id FROM offers WHERE offers.state = "pending" OR (offers.state = "canceled" AND offers.created_at > ?))', 1.hour.ago)
where('users.id NOT IN (SELECT offered_user_id FROM offers WHERE offers.state = "pending" OR ((offers.state = "canceled" OR offers.state = "retryable") AND offers.created_at > ?))', 1.hour.ago)
end

def self.sort_by_least_offered
Expand Down
3 changes: 2 additions & 1 deletion config/initializers/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
:ok => 'ok',
:accept => 'accept',
:sext => 'say',
:quit => 'safeword'
:quit => 'safeword',
:retry => 'retry'
}
23 changes: 20 additions & 3 deletions features/asking_for_a_date.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Feature: Asking for a date

Scenario: People ask for dates
Background:
Given the following users exist:
| Phone Number | Male | Female | Looking For Male | Looking For Female | Dob | Looking For Minimum Age | Looking For Maximum Age | Description | Name |
| 11111111111 | true | false | false | true | 11/06/1989 | 18 | 34 | red hair | Mike |
Expand All @@ -17,13 +17,14 @@ Feature: Asking for a date
| 19999999999 | true | true | 11/06/1989 | 18 | 34 |
| 10000000000 | true | true | 11/06/1989 | 18 | 34 |
| 11111111112 | true | true | 11/06/1989 | 18 | 34 |
And the day and time is "October 16, 2010 8:00pm edt"
And the day and time is "October 16, 2010 8:00pm est"
And the following date suggestions exist:
| text |
| Silvertone |
| Mike's Apartment |
And jobs are cleared

Scenario: People ask for dates
When "18004688487" texts instalover with "new date"
Then "18004688487" should get a text "Should we find you a date at Silvertone at 09:00PM? Reply 'ok' or 'new date' to try again."

Expand All @@ -37,7 +38,7 @@ Feature: Asking for a date

When jobs in 5 minutes from now are processed

Then "18004688487" should get a text "We called every number in our little black book, but only got answering machines. Try again later? Reply 'new date' to start again."
Then "18004688487" should get a text "We called every number in our little black book, but only got answering machines. Try again with 'retry'."
And "11111111111" should get a text "Too slow! Would you like to get a date? Reply 'new date'."
And "12222222222" should get a text "Too slow! Would you like to get a date? Reply 'new date'."
And "13333333333" should get a text "Too slow! Would you like to get a date? Reply 'new date'."
Expand Down Expand Up @@ -75,3 +76,19 @@ Feature: Asking for a date
When "18004688487" texts instalover with "new date"
Then "18004688487" should get a text whose message includes "Reply 'ok' or 'new date'"
When "18004688487" texts instalover with "ok"

Scenario: Ask for a date then retry
When "18004688487" texts instalover with "new date"
And "18004688487" texts instalover with "ok"
And jobs in 5 minutes from now are processed
Then "18004688487" should get a text "We called every number in our little black book, but only got answering machines. Try again with 'retry'."
And "11111111111" should get a text "Too slow! Would you like to get a date? Reply 'new date'."
Given I clear the text message history
When "18004688487" texts instalover with "retry"
Then "11111111111" should not get a text whose message includes "Want to go on a date"
And "12222222222" should not get a text whose message includes "Want to go on a date"
And "13333333333" should not get a text whose message includes "Want to go on a date"
And "14444444444" should not get a text whose message includes "Want to go on a date"
And "15555555555" should not get a text whose message includes "Want to go on a date"
But "16666666666" should get a text whose message includes "Want to go on a date"
And "18004688487" should get a text "Trying to get you a date. Back in five."

0 comments on commit 0da6646

Please sign in to comment.