Class: Renalware::UKRDC::Incoming::XmlDocument

Inherits:
Object
  • Object
show all
Defined in:
app/models/renalware/ukrdc/incoming/xml_document.rb

Overview

Encapsulates the structure of the UKRDC XML Document. Assumes one patient per file.

Constant Summary collapse

XPATHS =
{
  dob: "string(Patient[1]/BirthTime)",
  family_name: "string(Patient[1]/Names[1]/Name/Family)",
  nhs_number: <<-XPATH.squish
    string(
      Patient[1]/
        PatientNumbers/
          PatientNumber[Organization[text()='NHS']][NumberType[text()='MRN']][1]/
            Number
    )
  XPATH
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ XmlDocument

Returns a new instance of XmlDocument.



26
27
28
29
# File 'app/models/renalware/ukrdc/incoming/xml_document.rb', line 26

def initialize(file)
  xml_document = File.open(file) { |f| Nokogiri::XML(f) }
  @root = xml_document.root
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args, &_block) ⇒ Object

Accessing eg XmlDocument.new(file).nhs_number will search the document with looked-up entry in the XPATHS constant, to save us having to write attributes for the things we need.



34
35
36
37
38
39
# File 'app/models/renalware/ukrdc/incoming/xml_document.rb', line 34

def method_missing(method_name, *_args, &_block)
  xpath = XPATHS[method_name]
  return root.xpath(xpath) if xpath

  super
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



24
25
26
# File 'app/models/renalware/ukrdc/incoming/xml_document.rb', line 24

def root
  @root
end

Instance Method Details

#question_hash_from(survey_node) ⇒ Object

Note that if a response has a QuestionText this can be some text the user entered (presumably so they can then assign it a response value) so we need to store the question text (e.g. Paranoia) as well as the response (eg 4). If we find a QuestionText we store an array containing it and the response in the hash otherwise we just add the response.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/renalware/ukrdc/incoming/xml_document.rb', line 61

def question_hash_from(survey_node)
  question_nodes = survey_node.xpath("Questions/Question")
  question_nodes.each_with_object({}) do |question_node, responses|
    code = question_node.xpath("string(QuestionType/Code)")
    response = question_node.xpath("string(Response)")
    question_text = question_node.xpath("string(QuestionText)")
    responses[code] = if question_text.present?
                        [response, question_text]
                      else
                        response
                      end
  end
end

#respond_to_missing?(method_name, *_args) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/renalware/ukrdc/incoming/xml_document.rb', line 41

def respond_to_missing?(method_name, *_args)
  XPATHS.key?(method_name)
end

#surveysObject

Turn the surveys XML into a hash becuase it is easier to consume



46
47
48
49
50
51
52
53
54
# File 'app/models/renalware/ukrdc/incoming/xml_document.rb', line 46

def surveys
  root.xpath("Surveys/Survey").each_with_object([]) do |survey_node, arr|
    hash = {}
    hash[:code] = survey_node.xpath("string(SurveyType/Code)")
    hash[:time] = survey_node.xpath("string(SurveyTime)")
    hash[:responses] = question_hash_from(survey_node)
    arr << hash
  end
end