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:


541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/plivo.rb', line 541

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


567
568
569
570
571
572
# File 'lib/plivo.rb', line 567

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


534
535
536
# File 'lib/plivo.rb', line 534

def nestables
  @nestables
end

.valid_attributesObject

Returns the value of attribute valid_attributes


534
535
536
# File 'lib/plivo.rb', line 534

def valid_attributes
  @valid_attributes
end

Instance Attribute Details

#nameObject

Returns the value of attribute name


539
540
541
# File 'lib/plivo.rb', line 539

def name
  @name
end

#nodeObject

Returns the value of attribute node


539
540
541
# File 'lib/plivo.rb', line 539

def node
  @node
end

Instance Method Details

#add(element) ⇒ Object


591
592
593
594
595
596
597
598
599
600
601
# File 'lib/plivo.rb', line 591

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


575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/plivo.rb', line 575

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


607
608
609
# File 'lib/plivo.rb', line 607

def to_s
    return @node.to_s
end

#to_xmlObject


603
604
605
# File 'lib/plivo.rb', line 603

def to_xml
    return @node.to_s
end