Class: Plivo::XML::Element
- Inherits:
-
Object
- Object
- Plivo::XML::Element
- Defined in:
- lib/plivo/xml/element.rb
Direct Known Subclasses
Conference, DTMF, Dial, GetDigits, Hangup, Message, Number, Play, PreAnswer, Record, Redirect, Response, Speak, User, Wait
Class Attribute Summary collapse
-
.nestables ⇒ Object
Returns the value of attribute nestables.
-
.valid_attributes ⇒ Object
Returns the value of attribute valid_attributes.
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#node ⇒ Object
Returns the value of attribute node.
Instance Method Summary collapse
- #add(element) ⇒ Object
- #convert_value(v) ⇒ Object
-
#initialize(body = nil, attributes = {}) {|_self| ... } ⇒ Element
constructor
A new instance of Element.
- #method_missing(method, *args, &block) ⇒ Object
- #to_s ⇒ Object
- #to_xml ⇒ Object
Constructor Details
#initialize(body = nil, attributes = {}) {|_self| ... } ⇒ Element
Returns a new instance of Element.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/plivo/xml/element.rb', line 12 def initialize(body = nil, attributes = {}) @name = self.class.name.split('::')[2] @body = body @node = REXML::Element.new @name 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 @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
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/plivo/xml/element.rb', line 36 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. end end |
Class Attribute Details
.nestables ⇒ Object
Returns the value of attribute nestables.
5 6 7 |
# File 'lib/plivo/xml/element.rb', line 5 def nestables @nestables end |
.valid_attributes ⇒ Object
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
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/plivo/xml/element.rb', line 10 def name @name end |
#node ⇒ Object
Returns the value of attribute node.
10 11 12 |
# File 'lib/plivo/xml/element.rb', line 10 def node @node end |
Instance Method Details
#add(element) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/plivo/xml/element.rb', line 64 def add(element) raise PlivoXMLError, 'invalid element' unless element if self.class.nestables.include?(element.name) @node.elements << element.node element else raise PlivoXMLError, "#{element.name} not nestable in #{@name}" end end |
#convert_value(v) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/plivo/xml/element.rb', line 47 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 |
#to_s ⇒ Object
78 79 80 |
# File 'lib/plivo/xml/element.rb', line 78 def to_s @node.to_s end |
#to_xml ⇒ Object
74 75 76 |
# File 'lib/plivo/xml/element.rb', line 74 def to_xml @node.to_s end |