Class: TwimlTemplate::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/twiml_template/response.rb

Constant Summary collapse

MESSAGING_VERBS =
[:message, :redirect]
VOICE_VERBS =
[:say, :play, :dial, :gather, :pause, :enqueue, :hangup, :leave, :record, :redirect, :reject, :sms, :connect, :autopilot, :pay, :start]

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Response

Returns a new instance of Response.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
11
# File 'lib/twiml_template/response.rb', line 6

def initialize
  @voice_response = Twilio::TwiML::VoiceResponse.new
  @messaging_response = Twilio::TwiML::MessagingResponse.new

  yield(self) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/twiml_template/response.rb', line 18

def method_missing(method, *args, &block)
  if (respond_to?(method))
    if (MESSAGING_VERBS.include?(method) && @messaging_response)
      if ((MESSAGING_VERBS - VOICE_VERBS).include?(method))
        @voice_response = nil
      end
      @messaging_response.send(method, *args, &block)
    end
    if (VOICE_VERBS.include?(method) && @voice_response)
      if ((VOICE_VERBS - MESSAGING_VERBS).include?(method))
        @messaging_response = nil
      end
      @voice_response.send(method, *args, &block)
    end
  else
    raise ArgumentError.new("Method `#{method}` doesn't exist.")
  end
  return @messaging_response || @voice_response
end

Instance Method Details

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/twiml_template/response.rb', line 38

def respond_to?(method, include_private = false)
  verbs = []
  verbs = verbs | MESSAGING_VERBS if @messaging_response
  verbs = verbs | VOICE_VERBS if @voice_response
  verbs.include?(method.to_sym) || super
end

#to_xmlObject



13
14
15
16
# File 'lib/twiml_template/response.rb', line 13

def to_xml
  twiml_response = @messaging_response || @voice_response
  twiml_response.to_xml
end