class Twilio::TwiML::TwiML

Attributes

name[RW]

Public Class Methods

new(**keyword_args) click to toggle source
   # File lib/twilio-ruby/twiml/twiml.rb
32 def initialize(**keyword_args)
33   @overrides = {
34       aliasAttribute: 'alias',
35       xmlLang: 'xml:lang',
36       interpretAs: 'interpret-as',
37       for_: 'for',
38   }
39   @name = self.class.name.split('::').last
40   @value = nil
41   @verbs = []
42   @attrs = {}
43 
44   keyword_args.each do |key, val|
45     corrected_key = @overrides.fetch(key, TwiML.to_lower_camel_case(key))
46     @attrs[corrected_key] = val unless val.nil?
47   end
48 end
to_lower_camel_case(symbol) click to toggle source
   # File lib/twilio-ruby/twiml/twiml.rb
50 def self.to_lower_camel_case(symbol)
51   # Symbols don't have the .split method, so convert to string first
52   result = symbol.to_s
53   if result.include? '_'
54     result = result.split('_').map(&:capitalize).join
55   end
56   result[0].downcase + result[1..result.length]
57 end

Public Instance Methods

add_child(name, value=nil, **keyword_args) { |node| ... } click to toggle source
    # File lib/twilio-ruby/twiml/twiml.rb
108 def add_child(name, value=nil, **keyword_args)
109   node = GenericNode.new(name, value, **keyword_args)
110 
111   yield node if block_given?
112   append(node)
113 end
add_text(content) click to toggle source
    # File lib/twilio-ruby/twiml/twiml.rb
104 def add_text(content)
105   append(Text.new(content))
106 end
append(verb) click to toggle source
   # File lib/twilio-ruby/twiml/twiml.rb
91 def append(verb)
92   unless verb.is_a?(TwiML) || verb.is_a?(LeafNode)
93       raise TwiMLError, 'Only appending of TwiML is allowed'
94   end
95 
96   @verbs << verb
97   self
98 end
comment(body) click to toggle source
    # File lib/twilio-ruby/twiml/twiml.rb
100 def comment(body)
101   append(Comment.new(body))
102 end
to_s(xml_declaration = true) click to toggle source
   # File lib/twilio-ruby/twiml/twiml.rb
59 def to_s(xml_declaration = true)
60   save_opts = Nokogiri::XML::Node::SaveOptions::DEFAULT_XML
61   if !xml_declaration
62     save_opts = save_opts | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
63   end
64   opts = { encoding: 'UTF-8', indent: 0, save_with: save_opts }
65   document = Nokogiri::XML::Document.new
66   document << self.xml(document)
67   document.to_xml(opts)
68 end
Also aliased as: to_xml
to_xml(xml_declaration = true)
Alias for: to_s
xml(document) click to toggle source
   # File lib/twilio-ruby/twiml/twiml.rb
70 def xml(document)
71   # create XML element
72   value = (@value.is_a?(String) or @value == nil) ? @value : JSON.generate(@value)
73   elem = Nokogiri::XML::Node.new(@name, document)
74   elem.content = value
75   # set element attributes
76   keys = @attrs.keys.sort
77   keys.each do |key|
78     value = @attrs[key]
79 
80     value_is_boolean = value.is_a?(TrueClass) || value.is_a?(FalseClass)
81     elem[key] = value_is_boolean ? value.to_s.downcase : value.to_s
82   end
83 
84   @verbs.each do |verb|
85     elem << verb.xml(document)
86   end
87 
88   elem
89 end