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(schema:, template: nil, locals: {}) ⇒ XmlRenderer

Schema is an instance of Nokogiri::XML::Schema passed in for optimisation reasons. If it is not passed in we create it.



22
23
24
25
26
# File 'app/models/renalware/ukrdc/xml_renderer.rb', line 22

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#localsObject (readonly)

Returns the value of attribute locals.



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

def locals
  @locals
end

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

#templateObject (readonly)

Returns the value of attribute template.



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

def template
  @template
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.



32
33
34
35
36
# File 'app/models/renalware/ukrdc/xml_renderer.rb', line 32

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



49
50
51
# File 'app/models/renalware/ukrdc/xml_renderer.rb', line 49

def validation_errors
  @validation_errors ||= schema.validate(xml)
end

#xmlObject



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

def xml
  @xml ||= begin
    Ox.default_options = { no_empty: false, encoding: "UTF-8" }
    doc = Ox::Document.new
    doc << instruct_element
    doc << UKRDC::Outgoing::Rendering::Patient.new(locals).xml
    Ox.dump(doc)
  end
end