Class: Praxis::Handlers::XML

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/handlers/xml_sample.rb

Instance Method Summary collapse

Constructor Details

#initializeXML

Construct an XML handler and initialize any related libraries.

Raises:



15
16
17
18
19
20
21
22
23
# File 'lib/praxis/handlers/xml_sample.rb', line 15

def initialize
  require 'nokogiri'
  require 'builder'
  require 'active_support'
  ActiveSupport::XmlMini.backend = 'Nokogiri'
rescue LoadError
  raise Praxis::Exceptions::InvalidConfiguration,
        'XML handler depends on builder ~> 3.2 and nokogiri ~> 1.6; please add them to your Gemfile'
end

Instance Method Details

#generate(structured_data) ⇒ String

Generate a pretty-printed XML document from structured data.

Parameters:

  • structured_data (Hash, Array)

Returns:

  • (String)


38
39
40
41
# File 'lib/praxis/handlers/xml_sample.rb', line 38

def generate(structured_data)
  # courtesy of active_support + builder
  structured_data.to_xml
end

#parse(document) ⇒ Hash, Array

Parse an XML document into structured data.

Parameters:

  • document (String)

Returns:

  • (Hash, Array)

    the structured-data representation of the document



29
30
31
32
# File 'lib/praxis/handlers/xml_sample.rb', line 29

def parse(document)
  p = Nokogiri::XML(document)
  process(p.root, p.root.attributes['type'])
end