Class: Plivo::Element
- Inherits:
-
Object
- Object
- Plivo::Element
- Defined in:
- lib/plivo.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.
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/plivo.rb', line 512 def initialize(body=nil, attributes={}, &block) @name = self.class.name.split('::')[1] @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 PlivoError, "invalid attribute #{k.to_s} for #{@name}" end end if @body @node.text = @body end # Allow for nested "nestable" elements using a code block # ie # Plivo::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
538 539 540 541 542 543 |
# File 'lib/plivo.rb', line 538 def method_missing(method, *args, &block) # Handle the addElement methods method = $1.to_sym if method.to_s =~ /^add(.*)/ # Add the element add(Plivo.const_get(method).new(*args, &block)) end |
Class Attribute Details
.nestables ⇒ Object
Returns the value of attribute nestables.
505 506 507 |
# File 'lib/plivo.rb', line 505 def nestables @nestables end |
.valid_attributes ⇒ Object
Returns the value of attribute valid_attributes.
505 506 507 |
# File 'lib/plivo.rb', line 505 def valid_attributes @valid_attributes end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
510 511 512 |
# File 'lib/plivo.rb', line 510 def name @name end |
#node ⇒ Object
Returns the value of attribute node.
510 511 512 |
# File 'lib/plivo.rb', line 510 def node @node end |
Instance Method Details
#add(element) ⇒ Object
562 563 564 565 566 567 568 569 570 571 572 |
# File 'lib/plivo.rb', line 562 def add(element) if not element raise PlivoError, "invalid element" end if self.class.nestables.include?(element.name) @node.elements << element.node return element else raise PlivoError, "#{element.name} not nestable in #{@name}" end end |
#convert_value(v) ⇒ Object
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/plivo.rb', line 546 def convert_value(v) if v == true return "true" elsif v == false return "false" elsif v == nil return "none" elsif v == "get" return "GET" elsif v == "post" return "POST" else return v end end |
#to_s ⇒ Object
578 579 580 |
# File 'lib/plivo.rb', line 578 def to_s return @node.to_s end |
#to_xml ⇒ Object
574 575 576 |
# File 'lib/plivo.rb', line 574 def to_xml return @node.to_s end |