-
-
Save therippa/6371033 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'mechanize' | |
FIRST_NAME = 'Albert' | |
LAST_NAME = 'Taruc' | |
PHONE = '555-555-5555' | |
EMAIL = '[email protected]' | |
PARTY_SIZE = 2 | |
SCHEDULE_RANGE = { :start_time => '19:00', :end_time => '20:30' } | |
def start_url(restaurant=2086) | |
return "http://rez.urbanspoon.com/reservation/start/#{restaurant}" | |
end | |
def to_minutes(time) | |
hour, minutes = time.split(':') | |
raise "Malformed time: #{time}. Should be in the HH:MM format." if hour.nil? || minutes.nil? | |
return (hour.to_i * 60) + minutes.to_i | |
end | |
def to_time(minutes) | |
seating_time_minutes = minutes % 100 | |
seating_time_hours = (minutes - seating_time_minutes) / 60 | |
return "#{seating_time_hours}:#{seating_time_minutes}" | |
end | |
url = start_url() | |
agent = Mechanize.new { |agent| | |
agent.user_agent_alias = 'Mac Safari' | |
} | |
# Get the start page | |
start_page = agent.get(url) | |
# Bail if there are no reservations | |
if start_page.forms.count != 1 | |
puts "Damn, no reservations available" | |
exit | |
end | |
# Fill in the details for the reservation | |
start_form = start_page.forms.first | |
start_form.size = PARTY_SIZE | |
# Verify if the available times are in the allowed range | |
available_times = start_form.field_with(:name => 'seating_time').options | |
possible_times = available_times.select do |time| | |
(to_minutes(SCHEDULE_RANGE[:start_time])..to_minutes(SCHEDULE_RANGE[:end_time])).member?(time.value.to_i) | |
end | |
# Select the first of the possible times for the reservation | |
if possible_times.count == 0 | |
puts "Nothing in your time range boss, sorry" | |
exit | |
end | |
start_form.seating_time = possible_times.first | |
seating_time = to_time(possible_times.first.value.to_i) | |
puts "Found an available seating time at #{seating_time}" | |
# Submit the details and get back the contact form | |
contact_info_page = start_form.submit | |
# Check for the existence and get the contact form | |
if contact_info_page.forms.count != 1 | |
puts "No contact form, shutting down" | |
exit | |
end | |
contact_form = contact_info_page.forms.first | |
# Fill in the contact details | |
contact_form["user[first_name]"] = FIRST_NAME | |
contact_form["user[last_name]"] = LAST_NAME | |
contact_form["user[phone]"] = PHONE | |
contact_form["user[email]"] = EMAIL | |
# Submit the contact details and get confirmation page | |
confirmation_page = contact_form.submit | |
# Confirm the reservation | |
if confirmation_page.forms.count != 1 | |
puts "Did not reach reservation confirmation page" | |
exit | |
end | |
confirmation_form = confirmation_page.forms.first | |
confirmation_form["is_contactable"] = false | |
confirmation_form["us_contactable"] = false | |
final_page = confirmation_form.submit | |
puts "Got reservation for: #{seating_time}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment