Module: SMS

Defined in:
lib/sms.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#fromObject

Returns the value of attribute from.



5
6
7
# File 'lib/sms.rb', line 5

def from
  @from
end

#secretObject

Returns the value of attribute secret.



5
6
7
# File 'lib/sms.rb', line 5

def secret
  @secret
end

#sidObject

Returns the value of attribute sid.



5
6
7
# File 'lib/sms.rb', line 5

def sid
  @sid
end

Class Method Details

.text(options = {}) ⇒ Object



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
# File 'lib/sms.rb', line 7

def self.text(options={})
  @sid = ENV.delete('TWILIO_ID') if ENV.key?('TWILIO_ID')
  @secret = ENV.delete('TWILIO_SECRET') if ENV.key?('TWILIO_SECRET')
  @from = ENV.delete('TWILIO_PHONE') if ENV.key?('TWILIO_PHONE')
  @from = options[:from] if options.key?(:from)
  return false if @from.nil? or @from == ''
  return false unless options.key?(:message)
  return false unless options.key?(:to)
  
  url = URI.parse "https://api.twilio.com/2010-04-01/Accounts/#{@sid}/SMS/Messages"
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      
  request = Net::HTTP::Post.new(url.path)
  request.basic_auth @sid, @secret
  request.set_form_data({
 'From' => @from,
 'To' => options[:to],
 'Body' => options[:message],
  })
  
  response = http.start do |http|
    http.request(request)
  end
  response.code[0..0] == '2' # eg, 2xx
end