Class: Renalware::UKRDC::XmlRenderer

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

Defined Under Namespace

Classes: Failure, Success

Constant Summary collapse

DEFAULT_TEMPLATE =
"/renalware/api/ukrdc/patients/show"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template: nil, xsd_path: nil, locals: {}) ⇒ XmlRenderer

Returns a new instance of XmlRenderer.



19
20
21
22
23
# File 'app/models/renalware/ukrdc/xml_renderer.rb', line 19

def initialize(template: nil, xsd_path: nil, locals: {})
  @template = template || DEFAULT_TEMPLATE
  @xsd_path = xsd_path || default_xsd_path
  @locals = locals
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



9
10
11
# File 'app/models/renalware/ukrdc/xml_renderer.rb', line 9

def errors
  @errors
end

#localsObject (readonly)

Returns the value of attribute locals.



9
10
11
# File 'app/models/renalware/ukrdc/xml_renderer.rb', line 9

def locals
  @locals
end

#templateObject (readonly)

Returns the value of attribute template.



9
10
11
# File 'app/models/renalware/ukrdc/xml_renderer.rb', line 9

def template
  @template
end

#xsd_pathObject (readonly)

Returns the value of attribute xsd_path.



9
10
11
# File 'app/models/renalware/ukrdc/xml_renderer.rb', line 9

def xsd_path
  @xsd_path
end

Instance Method Details

#callObject

If we successfully generate the UKRDC XML for a patient, return a Success object where success#xml is the valid XML If there are XSD validation messages, we return a Failure object where failure#validation_messages is an array of XSD validation messages.



29
30
31
32
33
# File 'app/models/renalware/ukrdc/xml_renderer.rb', line 29

def call
  return XmlRenderer::Failure.new(validation_errors) if validation_errors.any?

  XmlRenderer::Success.new(xml)
end

#validation_errorsObject

Returns an array of SchemaValidation errors



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

def validation_errors
  @validation_errors ||= begin
    document = Nokogiri::XML(xml)
    xsddoc = Nokogiri::XML(File.read(xsd_path), xsd_path)
    schema = Nokogiri::XML::Schema.from_document(xsddoc)
    schema.validate(document)
  end
end

#xmlObject



35
36
37
38
39
40
41
42
43
44
# File 'app/models/renalware/ukrdc/xml_renderer.rb', line 35

def xml
  @xml ||= begin
    API::UKRDC::PatientsController.new.render_to_string(
      template: template,
      format: :xml,
      locals: locals,
      encoding: "UTF-8"
    )
  end
end