class REXMLUtilityNode

underlying parser.
This represents the hard part of the work, all I did was change the
It’s mainly just adding vowels, as I ht cd wth n vwls :)
(has.sox@gmail.com)
This is a slighly modified version of the XMLUtilityNode from

def self.available_typecasts

def self.available_typecasts
  @@typecasts
end

def self.available_typecasts=(obj)

def self.available_typecasts=(obj)
  @@typecasts = obj
end

def self.typecasts

def self.typecasts
  @@typecasts
end

def self.typecasts=(obj)

def self.typecasts=(obj)
  @@typecasts = obj
end

def add_node(node)

def add_node(node)
  @text = true if node.is_a? String
  @children << node
end

def initialize(name, attributes = {})

def initialize(name, attributes = {})
  @name         = name.tr("-", "_")
  # leave the type alone if we don't know what it is
  @type         = self.class.available_typecasts.include?(attributes["type"]) ? attributes.delete("type") : attributes["type"]
  @nil_element  = attributes.delete("nil") == "true"
  @attributes   = undasherize_keys(attributes)
  @children     = []
  @text         = false
end

def inner_html

Get the inner_html of the REXML node.
def inner_html
  @children.join
end

def to_hash

def to_hash
  if @type == "file"
    f = StringIO.new((@children.first || '').unpack('m').first)
    class << f
      attr_accessor :original_filename, :content_type
    end
    f.original_filename = attributes['name'] || 'untitled'
    f.content_type = attributes['content_type'] || 'application/octet-stream'
    return {name => f}
  end
  if @text
    return { name => typecast_value( translate_xml_entities( inner_html ) ) }
  else
    #change repeating groups into an array
    groups = @children.inject({}) { |s,e| (s[e.name] ||= []) << e; s }
    out = nil
    if @type == "array"
      out = []
      groups.each do |k, v|
        if v.size == 1
          out << v.first.to_hash.entries.first.last
        else
          out << v.map{|e| e.to_hash[k]}
        end
      end
      out = out.flatten
    else # If Hash
      out = {}
      groups.each do |k,v|
        if v.size == 1
          out.merge!(v.first)
        else
          out.merge!( k => v.map{|e| e.to_hash[k]})
        end
      end
      out.merge! attributes unless attributes.empty?
      out = out.empty? ? nil : out
    end
    if @type && out.nil?
      { name => typecast_value(out) }
    else
      { name => out }
    end
  end
end

def to_html

Returns:
  • (String) - The HTML node in text form.
def to_html
  attributes.merge!(:type => @type ) if @type
  "<#{name}#{attributes.to_xml_attributes}>#{@nil_element ? '' : inner_html}</#{name}>"
end

def to_s

@alias #to_html #to_s
def to_s
  to_html
end

def translate_xml_entities(value)

Returns:
  • (#gsub) - The XML fragment after converting entities.

Parameters:
  • value (#gsub) -- An XML fragment.
def translate_xml_entities(value)
  value.gsub(/&lt;/,   "<").
        gsub(/&gt;/,   ">").
        gsub(/&quot;/, '"').
        gsub(/&apos;/, "'").
        gsub(/&amp;/,  "&")
end

def typecast_value(value)

Other tags:
    Note: -

Returns:
  • (Integer, TrueClass, FalseClass, Time, Date, Object) -

Parameters:
  • value (String) -- The value that is being typecast.
def typecast_value(value)
  return value unless @type
  proc = self.class.typecasts[@type]
  proc.nil? ? value : proc.call(value)
end

def undasherize_keys(params)

Take keys of the form foo-bar and convert them to foo_bar
def undasherize_keys(params)
  params.keys.each do |key, value|
    params[key.tr("-", "_")] = params.delete(key)
  end
  params
end