Sha256: 424fbefd7f658024f29f503c807fb564648346383bbb0c413c8ca0b52435b2f5

Contents?: true

Size: 1.82 KB

Versions: 7

Compression:

Stored size: 1.82 KB

Contents

require 'libxml'

##
# This code was generated by
# \ / _    _  _|   _  _
#  | (_)\/(_)(_|\/| |(/_  v1.0.0
#       /       /

module Twilio
  module TwiML
    class TwiMLError < StandardError; end

    class TwiML
        attr_accessor :name, :indent

        def initialize(indent: false, **keyword_args)
          @name = self.class.name.split('::').last
          @indent = indent
          @value = nil
          @verbs = []
          @attrs = {}

          keyword_args.each do |key, val|
            @attrs[TwiML.to_lower_camel_case(key)] = val unless val.nil?
          end
        end

        def self.to_lower_camel_case(symbol)
          # Symbols don't have the .split method, so convert to string first
          result = symbol.to_s.split('_').map(&:capitalize).join
          result[0].downcase + result[1..result.length]
        end

        def to_s(xml_declaration = true)
          xml = self.xml.to_s(indent: indent)

          return ('<?xml version="1.0" encoding="UTF-8"?>' + xml) if xml_declaration
          xml
        end

        def xml
          # create XML element
          value = (@value.is_a?(String) or @value == nil) ? @value : JSON.generate(@value)
          elem = LibXML::XML::Node.new(@name, value)

          # set element attributes
          keys = @attrs.keys.sort
          keys.each do |key|
            value = @attrs[key]

            value_is_boolean = value.is_a?(TrueClass) || value.is_a?(FalseClass)
            elem[key] = value_is_boolean ? value.to_s.downcase : value.to_s
          end

          @verbs.each do |verb|
            elem << verb.xml
          end

          elem
        end

        def append(verb)
          raise TwiMLError, 'Only appending of TwiML is allowed' unless verb.is_a?(TwiML)

          @verbs << verb
          self
        end

      alias to_xml to_s
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
twilio-ruby-5.4.1 lib/twilio-ruby/twiml/twiml.rb
twilio-ruby-5.4.0 lib/twilio-ruby/twiml/twiml.rb
twilio-ruby-5.3.1 lib/twilio-ruby/twiml/twiml.rb
twilio-ruby-5.3.0 lib/twilio-ruby/twiml/twiml.rb
twilio-ruby-5.2.3 lib/twilio-ruby/twiml/twiml.rb
twilio-ruby-5.2.2 lib/twilio-ruby/twiml/twiml.rb
twilio-ruby-5.2.1 lib/twilio-ruby/twiml/twiml.rb