Class: Services::MdsXmlFileParser

Inherits:
Object
  • Object
show all
Includes:
LibXML
Defined in:
app/utils/services/mds_xml_file_parser.rb

Constant Summary collapse

ROOT_NODE =
"ASSESSMENT"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assessment_file) ⇒ MdsXmlFileParser

Returns a new instance of MdsXmlFileParser.



12
13
14
15
16
# File 'app/utils/services/mds_xml_file_parser.rb', line 12

def initialize(assessment_file)
  @assessment_file = assessment_file
  @base_name = File.basename(assessment_file)
  @file_errors = { @base_name => [] }
end

Instance Attribute Details

#assessment_fileObject (readonly)

Returns the value of attribute assessment_file.



8
9
10
# File 'app/utils/services/mds_xml_file_parser.rb', line 8

def assessment_file
  @assessment_file
end

#file_errorsObject (readonly)

Returns the value of attribute file_errors.



8
9
10
# File 'app/utils/services/mds_xml_file_parser.rb', line 8

def file_errors
  @file_errors
end

Instance Method Details

#parseObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/utils/services/mds_xml_file_parser.rb', line 18

def parse

  begin
    sax_handler = MdsSaxHandler.new
    File.open(assessment_file, "r") do | f |
      parser = LibXML::XML::SaxParser.io(f)
      parser.callbacks = sax_handler
      parser.parse
    end

  rescue LibXML::XML::Error
    add_error "XML root element ASSESSMENT not found."
    return
  rescue
    add_error "There was a problem parsing this file. Please ensure that it has the correct MDS 3.0 XML format."
    return
  end

  add_error(sax_handler.errors) if sax_handler.errors.present?
  return if has_errors?

  add_error("XML data elements were not found.") if sax_handler.assessment_hash.empty?
  return if has_errors?

  sax_handler.assessment_hash
end