Module: Twilio::Verb

Included in:
Conference, Dial, Gather, Hangup, Number, Pause, Play, Record, Redirect, Response, Say, Sms
Defined in:
lib/twiliolib.rb

Overview

Twiml Response Helpers

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#addConference(room, opts = {}) ⇒ Object



283
284
285
# File 'lib/twiliolib.rb', line 283

def addConference(room, opts = {})
  append Twilio::Conference.new(room, opts)
end

#addDial(number = nil, opts = {}) ⇒ Object



263
264
265
# File 'lib/twiliolib.rb', line 263

def addDial(number = nil, opts = {})
  append Twilio::Dial.new(number, opts)
end

#addGather(opts = {}) ⇒ Object



255
256
257
# File 'lib/twiliolib.rb', line 255

def addGather(opts = {})
  append Twilio::Gather.new(opts)
end

#addHangupObject



275
276
277
# File 'lib/twiliolib.rb', line 275

def addHangup
  append Twilio::Hangup.new 
end

#addNumber(number, opts = {}) ⇒ Object



279
280
281
# File 'lib/twiliolib.rb', line 279

def addNumber(number, opts = {})
  append Twilio::Number.new(number, opts)
end

#addPause(opts = {}) ⇒ Object



271
272
273
# File 'lib/twiliolib.rb', line 271

def addPause(opts = {})
  append Twilio::Pause.new(opts)
end

#addPlay(file_to_play = nil, opts = {}) ⇒ Object



251
252
253
# File 'lib/twiliolib.rb', line 251

def addPlay(file_to_play = nil, opts = {})
  append Twilio::Play.new(file_to_play, opts)
end

#addRecord(opts = {}) ⇒ Object



259
260
261
# File 'lib/twiliolib.rb', line 259

def addRecord(opts = {})
  append Twilio::Record.new(opts)
end

#addRedirect(url = nil, opts = {}) ⇒ Object



267
268
269
# File 'lib/twiliolib.rb', line 267

def addRedirect(url = nil, opts = {})
  append Twilio::Redirect.new(url, opts)
end

#addSay(string_to_say = nil, opts = {}) ⇒ Object

Verb Convenience Methods



247
248
249
# File 'lib/twiliolib.rb', line 247

def addSay(string_to_say = nil, opts = {})
  append Twilio::Say.new(string_to_say, opts)
end

#addSms(msg, opts = {}) ⇒ Object



287
288
289
# File 'lib/twiliolib.rb', line 287

def addSms(msg, opts = {})
  append Twilio::Sms.new(msg, opts)
end

#allowed?(verb) ⇒ true, false

Parameters:

  • Verb (Object)

    to be appended

Returns:

  • (true, false)


169
170
171
# File 'lib/twiliolib.rb', line 169

def allowed?(verb)
  self.class.allowed_verbs.nil? ? false : self.class.allowed_verbs.include?(verb.class.name.split('::')[1])
end

#append(verb) ⇒ Object



237
238
239
240
241
242
243
244
# File 'lib/twiliolib.rb', line 237

def append(verb)
  if(allowed?(verb))
    @children << verb
    @children[-1]
  else
    raise ArgumentError, "Verb Not Supported"
  end    
end

#asURLString

Returns URL encoded Twilio Markup (XML).

Parameters:

  • []

Returns:

  • (String)

    URL encoded Twilio Markup (XML)



233
234
235
# File 'lib/twiliolib.rb', line 233

def asURL()
  CGI::escape(self.respond)
end

#attributesObject



161
162
163
# File 'lib/twiliolib.rb', line 161

def attributes
  self.class.attributes
end

#initialize(body = nil, params = {}) ⇒ Object

Returns Twilio Verb object.

Parameters:

  • Body (String, Hash)

    of the verb, and a hash of the attributes

Returns:

  • (Object)

    Twilio Verb object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/twiliolib.rb', line 179

def initialize(body = nil, params = {})
  @children = []
  if body.class == String
    @body = body
  else
    @body = nil
    params = body || {}
  end
  params.each do |k,v|
    if !self.class.attributes.nil? && self.class.attributes.include?(k)
      send(k.to_s+"=",v)
    else
      raise ArgumentError, "Attribute Not Supported"
    end
  end
end

#respond(opts = {}) ⇒ String

Returns Twilio Markup (in XML).

Parameters:

  • Hash (Hash)

    of options

Returns:

  • (String)

    Twilio Markup (in XML)



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/twiliolib.rb', line 212

def respond(opts = {})
  opts[:builder]  ||= Builder::XmlMarkup.new(:indent => opts[:indent])
  b = opts[:builder]
  attrs = {}
  attributes.each {|a| attrs[a] = send(a) unless send(a).nil? } unless attributes.nil?

  if @children and @body.nil?
    b.__send__(self.class.to_s.split(/::/)[-1], attrs) do
      @children.each {|e|e.respond( opts.merge(:skip_instruct => true) )}
    end
  elsif @body and @children == []
    b.__send__(self.class.to_s.split(/::/)[-1], @body, attrs)
  else
    raise ArgumentError, "Cannot have children and a body at the same time"
  end
end

#set(params = {}) ⇒ Object

no error checking

Parameters:

  • Hash (Hash)

    of options

Returns:

  • void



201
202
203
204
205
206
# File 'lib/twiliolib.rb', line 201

def set(params = {})
  params.each do |k,v|
    self.class.attributes k.to_s
    send(k.to_s+"=",v)
  end     
end