Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ddc882d172
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 37 lines (31 sloc) 0.951 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
require 'rubygems'
require File.join(File.dirname(__FILE__), 'sms', 'twiliolib')

module SMS

  class << self
    attr_accessor :twilio_id
    attr_accessor :twilio_phone
    attr_accessor :twilio_secret
    def twilio_send
      "/2008-08-01/Accounts/#{self.twilio_id||ENV['TWILIO_ID']}/SMS/Messages"
    end
  end
  

  def self.text(message, options={})
    twilio = Twilio::RestAccount.new self.twilio_id||ENV['TWILIO_ID'], self.twilio_secret||ENV['TWILIO_SECRET']
    res = twilio.request self.twilio_send, 'POST',
      'To' => options[:to],
      'From' => options[:from] || (self.twilio_phone||ENV['TWILIO_PHONE']),
      'Body' => message
    if res.code.to_i == 201
      true
    else
      if options[:raise]
        raise(res.body.match(/<Message>(Message body is required)<\/Message>/)[1])
      else
        false
      end
    end
  end

  def self.text!(message, options={})
    text(message, options.merge(:raise => true))
  end
end
Something went wrong with that request. Please try again.