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:



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

.nestablesObject

Returns the value of attribute nestables.



505
506
507
# File 'lib/plivo.rb', line 505

def nestables
  @nestables
end

.valid_attributesObject

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

#nameObject

Returns the value of attribute name.



510
511
512
# File 'lib/plivo.rb', line 510

def name
  @name
end

#nodeObject

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_sObject



578
579
580
# File 'lib/plivo.rb', line 578

def to_s
  return @node.to_s
end

#to_xmlObject



574
575
576
# File 'lib/plivo.rb', line 574

def to_xml
  return @node.to_s
end