Skip to content

Instantly share code, notes, and snippets.

@jarodreyes
Last active August 29, 2015 14:16
Show Gist options
  • Save jarodreyes/6933d9d52c8663cc59a4 to your computer and use it in GitHub Desktop.
Save jarodreyes/6933d9d52c8663cc59a4 to your computer and use it in GitHub Desktop.
class Appointment < ActiveRecord::Base
validates :name, presence: true
validates :phone_number, presence: true
validates :time, presence: true
after_save :notify
@@REMINDER_TIME = 10.minutes # minutes before appointment
attr_accessor :time
protected
# Notify our appointment attendee X minutes before the appointment time
def notify
@twilio_number = ENV['TWILIO_NUMBER']
@client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']
time_str = (self.time).strftime("%I:%M%p on %b %d, %y")
reminder = "Hi #{self.name}. Just a reminder that you have an appointment coming up at #{time_str}."
message = @client.account.messages.create(
:from => @twilio_number,
:to => self.phone_number,
:body => reminder,
)
puts message.to
end
def self.when_to_run
time - @@REMINDER_TIME
end
handle_asynchronously :notify, :run_at => Proc.new { when_to_run }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment