Class: Twilio::TwiML::TwiML

Inherits:
Object
  • Object
show all
Defined in:
lib/twilio-ruby/twiml/twiml.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**keyword_args) ⇒ TwiML

Returns a new instance of TwiML.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/twilio-ruby/twiml/twiml.rb', line 32

def initialize(**keyword_args)
  @overrides = {
      aliasAttribute: 'alias',
      xmlLang: 'xml:lang',
      interpretAs: 'interpret-as',
      for_: 'for',
  }
  @name = self.class.name.split('::').last
  @value = nil
  @verbs = []
  @attrs = {}

  keyword_args.each do |key, val|
    corrected_key = @overrides.fetch(key, TwiML.to_lower_camel_case(key))
    @attrs[corrected_key] = val unless val.nil?
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



30
31
32
# File 'lib/twilio-ruby/twiml/twiml.rb', line 30

def name
  @name
end

Class Method Details

.to_lower_camel_case(symbol) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/twilio-ruby/twiml/twiml.rb', line 50

def self.to_lower_camel_case(symbol)
  # Symbols don't have the .split method, so convert to string first
  result = symbol.to_s
  if result.include? '_'
    result = result.split('_').map(&:capitalize).join
  end
  result[0].downcase + result[1..result.length]
end

Instance Method Details

#add_child(name, value = nil, **keyword_args) {|node| ... } ⇒ Object

Yields:

  • (node)


108
109
110
111
112
113
# File 'lib/twilio-ruby/twiml/twiml.rb', line 108

def add_child(name, value=nil, **keyword_args)
  node = GenericNode.new(name, value, **keyword_args)

  yield node if block_given?
  append(node)
end

#add_text(content) ⇒ Object



104
105
106
# File 'lib/twilio-ruby/twiml/twiml.rb', line 104

def add_text(content)
  append(Text.new(content))
end

#append(verb) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/twilio-ruby/twiml/twiml.rb', line 91

def append(verb)
  unless verb.is_a?(TwiML) || verb.is_a?(LeafNode)
      raise TwiMLError, 'Only appending of TwiML is allowed'
  end

  @verbs << verb
  self
end

#comment(body) ⇒ Object



100
101
102
# File 'lib/twilio-ruby/twiml/twiml.rb', line 100

def comment(body)
  append(Comment.new(body))
end

#to_s(xml_declaration = true) ⇒ Object Also known as: to_xml



59
60
61
62
63
64
65
66
67
68
# File 'lib/twilio-ruby/twiml/twiml.rb', line 59

def to_s(xml_declaration = true)
  save_opts = Nokogiri::XML::Node::SaveOptions::DEFAULT_XML
  if !xml_declaration
    save_opts = save_opts | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
  end
  opts = { encoding: 'UTF-8', indent: 0, save_with: save_opts }
  document = Nokogiri::XML::Document.new
  document << self.xml(document)
  document.to_xml(opts)
end

#xml(document) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/twilio-ruby/twiml/twiml.rb', line 70

def xml(document)
  # create XML element
  value = (@value.is_a?(String) or @value == nil) ? @value : JSON.generate(@value)
  elem = Nokogiri::XML::Node.new(@name, document)
  elem.content = value
  # set element attributes
  keys = @attrs.keys.sort
  keys.each do |key|
    value = @attrs[key]

    value_is_boolean = value.is_a?(TrueClass) || value.is_a?(FalseClass)
    elem[key] = value_is_boolean ? value.to_s.downcase : value.to_s
  end

  @verbs.each do |verb|
    elem << verb.xml(document)
  end

  elem
end