Class: Plivo::XML::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/plivo/xml/element.rb

Constant Summary collapse

SSML_TAGS =
%w[Break Emphasis Lang P Phoneme Prosody S SayAs Sub W]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body = nil, attributes = {}, nestables = self.class.nestables) {|_self| ... } ⇒ Element

Returns a new instance of Element.

Yields:

  • (_self)

Yield Parameters:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plivo/xml/element.rb', line 20

def initialize(body = nil, attributes = {}, nestables=self.class.nestables)
  @name = self.class.name.split('::')[2]
  @body = body
  tagname = @name
  if SSML_TAGS.include?(@name)
    tagname = hyphenate(@name)
  end
  @node = REXML::Element.new tagname
  attributes.each do |k, v|
    if self.class.valid_attributes.include?(k.to_s)
      @node.attributes[k.to_s] = convert_value(v)
    else
      raise PlivoXMLError, "invalid attribute #{k} for #{@name}"
    end
  end
  @nestables = nestables
  @node.text = @body if @body

  # Allow for nested "nestable" elements using a code block
  # ie
  # Plivo::XML::Response.new do |r|
  #   r.Dial do |n|
  #     n.Number '+15557779999'
  #   end
  # end
  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



52
53
54
55
56
57
58
59
60
61
# File 'lib/plivo/xml/element.rb', line 52

def method_missing(method, *args, &block)
  # Handle the addElement methods
  method = Regexp.last_match(1).to_sym if method.to_s =~ /^add(.*)/
  # Add the element
  begin
    add(Plivo::XML.const_get(method).new(*args, &block))
  rescue StandardError => e
    raise PlivoXMLError, e.message
  end
end

Class Attribute Details

.nestablesObject

Returns the value of attribute nestables.



5
6
7
# File 'lib/plivo/xml/element.rb', line 5

def nestables
  @nestables
end

.valid_attributesObject

Returns the value of attribute valid_attributes.



5
6
7
# File 'lib/plivo/xml/element.rb', line 5

def valid_attributes
  @valid_attributes
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/plivo/xml/element.rb', line 11

def name
  @name
end

#nodeObject

Returns the value of attribute node.



11
12
13
# File 'lib/plivo/xml/element.rb', line 11

def node
  @node
end

Instance Method Details

#add(element) ⇒ Object

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/plivo/xml/element.rb', line 80

def add(element)
  raise PlivoXMLError, 'invalid element' unless element
  if @nestables.include?(element.name)
    if element.name == "Cont"
      @node.elements << element.node 
      element
      temp = REXML::Text.new element.node.text
      @node.elements['Cont'] = temp
    else
      @node.elements << element.node 
      element
    end
  else
    raise PlivoXMLError, "#{element.name} not nestable in #{@name}"
  end
end

#add_attribute(attribute, value) ⇒ Object



48
49
50
# File 'lib/plivo/xml/element.rb', line 48

def add_attribute(attribute, value)
  @node.add_attribute(attribute, value)
end

#convert_value(v) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/plivo/xml/element.rb', line 63

def convert_value(v)
  case v
  when true
    'true'
  when false
    'false'
  when nil
    'none'
  when 'get'
    'GET'
  when 'post'
    'POST'
  else
    v
  end
end

#hyphenate(pascal_cased_word) ⇒ Object



13
14
15
16
17
18
# File 'lib/plivo/xml/element.rb', line 13

def hyphenate(pascal_cased_word)
  pascal_cased_word.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1-\2').
    gsub(/([a-z\d])([A-Z])/,'\1-\2').
    downcase
end

#to_sObject



101
102
103
# File 'lib/plivo/xml/element.rb', line 101

def to_s
  @node.to_s
end

#to_xmlObject



97
98
99
# File 'lib/plivo/xml/element.rb', line 97

def to_xml
  @node.to_s
end