Class: Transcoder::XML

Inherits:
Parser
  • Object
show all
Defined in:
lib/transcoder/parser/xml/identifier.rb,
lib/transcoder/parser/xml/parser_handler.rb,
lib/transcoder/parser/xml/generator_handler.rb

Constant Summary collapse

ParserHandler =
Crack::XML
GeneratorHandler =
Nokogiri::XML::Builder

Class Method Summary collapse

Class Method Details

.generate(object) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/transcoder/parser/xml/identifier.rb', line 10

def self.generate(object)
  raise TypeError unless object.is_a? Hash or object.is_a? Array
  builder = XML::GeneratorHandler.new do |xml|
    xml.items {
      if object.is_a? Array
        object.each{|item| xml.item(:value => item) }
      elsif object.is_a? Hash
        # stringify all the keys
        object.each {|key, value| object[key.to_s] = object.delete(key) }
        
        xml.item {
          object.each do |key, value|
            # id, type, class and fork must be appended with _
            key = "#{key}_" if key =~ /(id|type|class|fork)/
            
            xml.send(key, value)
          end
        }
      end
    }
  end

  builder.to_xml
end

.parse(raw) ⇒ Object



35
36
37
# File 'lib/transcoder/parser/xml/identifier.rb', line 35

def self.parse(raw)
  XML::ParserHandler.parse(raw)
end