Module: TwilioContactable::Controller

Defined in:
lib/controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(controller) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/controller.rb', line 4

def self.included(controller)
  controller.instance_eval do
    # the developer should specify which model(s) will be sought
    # when the app receives incoming requests.
    # See the 'Controller' part of the README for details
    protected
    def twilio_contactable(*klasses)
      @@contactable_classes = klasses
      name = self.name.underscore.chomp('_controller')
      klasses.each do |klass|
        klass.twilio_contactable.controller = name
      end
    end
  end
end

Instance Method Details

#receive_sms_messageObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/controller.rb', line 20

def receive_sms_message
  unless defined?(@@contactable_classes) && !@@contactable_classes.blank?
    raise RuntimeError, "Please define which model(s) this controller should receive for via the 'twilio_contactable' method"
  end

  request = params[:request]
  render :text => 'unknown format', :status => 500 and return unless request
  case request['type']
  when 'BLOCK'
    @contactable = find_contactable_by_phone_number(request[:block][:recipient][:id])
    @contactable._TC_sms_blocked = true
    @contactable.save!
  when 'MESSAGE'
    @contactable = find_contactable_by_phone_number(request[:message][:sender][:id])
    if @contactable.respond_to?(:receive_sms)
      @contactable.receive_sms(request[:message][:text])
    else
      warn "An SMS message was received but #{@contactable.class.name.inspect} doesn't have a receive_sms method!"
    end
  end
  render :text => 'OK', :status => 200
end

#receive_voice_confirmationObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/controller.rb', line 49

def receive_voice_confirmation
  @contactable = params[:contactable_type].constantize.find(params[:contactable_id])
  if @contactable.voice_confirm_with(params['Digits'])
    render :xml => (Twilio::Response.new.tap do |response|
      response.addSay "Thank you, please return to the website and continue"
      response.addHangup
    end.respond)
  else
    render :xml => (Twilio::Response.new.tap do |response|
      response.addSay "Sorry, those aren't the correct numbers."
      gather(response)
    end.respond)
  end
end

#start_voice_confirmationObject



43
44
45
46
47
# File 'lib/controller.rb', line 43

def start_voice_confirmation
  render :xml => (Twilio::Response.new.tap do |response|
    gather(response)
  end.respond)
end