class Xml2js::Parser

Constants

DEFAULT_SCRIPT

Public Class Methods

new(script) click to toggle source
# File lib/xml2js/parser.rb, line 11
def initialize(script)
  @parser = script
end
parse(data, script=DEFAULT_SCRIPT) click to toggle source
# File lib/xml2js/parser.rb, line 7
def self.parse(data, script=DEFAULT_SCRIPT)
  parser = new script
  parser.xml_to_hash data
end

Public Instance Methods

xml_to_hash(data) click to toggle source
# File lib/xml2js/parser.rb, line 15
def xml_to_hash(data)
  tmp_dir = "#{File.expand_path(__dir__)}/tmp"
  file = Tempfile.new(['xml-data', '.xml'], tmp_dir)
  json_file = Tempfile.new(['json-data', '.json'], tmp_dir)
  begin
    file.write(data)
    file.rewind
    json_file.rewind
    %x{ node #{@parser} #{file.path} #{json_file.path}}
    JSON.parse(File.read(json_file.path), {symbolize_names: true})
  ensure
    file.close
    file.unlink
  end
end