Class: Plivo::Element

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Element.

Yields:

  • (_self)

Yield Parameters:



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/plivo.rb', line 530

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



556
557
558
559
560
561
# File 'lib/plivo.rb', line 556

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

.nestablesObject

Returns the value of attribute nestables.



523
524
525
# File 'lib/plivo.rb', line 523

def nestables
  @nestables
end

.valid_attributesObject

Returns the value of attribute valid_attributes.



523
524
525
# File 'lib/plivo.rb', line 523

def valid_attributes
  @valid_attributes
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



528
529
530
# File 'lib/plivo.rb', line 528

def name
  @name
end

#nodeObject

Returns the value of attribute node.



528
529
530
# File 'lib/plivo.rb', line 528

def node
  @node
end

Instance Method Details

#add(element) ⇒ Object



580
581
582
583
584
585
586
587
588
589
590
# File 'lib/plivo.rb', line 580

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



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/plivo.rb', line 564

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_sObject



596
597
598
# File 'lib/plivo.rb', line 596

def to_s
    return @node.to_s
end

#to_xmlObject



592
593
594
# File 'lib/plivo.rb', line 592

def to_xml
    return @node.to_s
end