module Robocall

Constants

VERSION

Attributes

auth_token[RW]
base_path[RW]
from_phone_number[RW]
sid[RW]

Public Class Methods

cleanup(minutes_old: 360) click to toggle source
# File lib/robocall.rb, line 60
def cleanup(minutes_old: 360)
  Robocall.delete_all(["updated_at < ?", minutes_old.minutes.ago])
end
get_twilio() click to toggle source
# File lib/robocall.rb, line 41
def get_twilio
  verify_configuration_values(:sid, :auth_token, :from_phone_number, :base_path)
  return Twilio::REST::Client.new sid, auth_token
end
render_say(text: text, language: 'en-US', voice: 'alice') click to toggle source
# File lib/robocall.rb, line 46
    def render_say(text: text, language: 'en-US', voice: 'alice')
      template = <<'HAML'

%Response
  %Say{:voice => voice, :language => language}
    = text
HAML
      data = {}
      data['text'] = text
      data['language'] = language
      data['voice']  = voice
      xml = Haml::Engine.new(template).to_html(Object.new, data )
    end
send_robocall(to: to, text: text, language: 'en-US', voice: 'alice', from: from_phone_number) click to toggle source
# File lib/robocall.rb, line 35
def send_robocall(to: to, text: text, language: 'en-US', voice: 'alice', from: from_phone_number)
  # Render XML
  xml = render_say(text: text, language: language, voice: voice)
  send_robocall_xml(to: to, xml: xml, from: from)
end
send_robocall_xml(to: to, xml: xml, from: from_phone_number) click to toggle source
# File lib/robocall.rb, line 20
def send_robocall_xml(to: to, xml: xml, from: from_phone_number)
  twilio = get_twilio
  # Store the xml in a record
  callback_record = Robocall.new
  callback_record.xml = xml
  callback_record.save
  # construct the callback URL
  url = base_path+"/robocall/#{callback_record.id}/#{callback_record.token}"
  twilio.account.calls.create(
    :from => from,
    :to   => to,
    :url  => url
  )
end
send_text(to: to, text: text, from: from_phone_number) click to toggle source
# File lib/robocall.rb, line 11
def send_text(to: to, text: text, from: from_phone_number)
  twilio = get_twilio
  twilio.account.sms.messages.create(
    :from => from,
    :to   => to,
    :body => text
  )
end

Private Class Methods

verify_configuration_values(*symbols) click to toggle source
# File lib/robocall.rb, line 66
def verify_configuration_values(*symbols)
  absent_values = symbols.select{|symbol| instance_variable_get("@#{symbol}").nil? }
  raise("Must configure #{absent_values.join(", ")} before making this request.") unless absent_values.empty?
end