From bac063d9dc590c005624a8a23bfea73e7e447879 Mon Sep 17 00:00:00 2001 From: 360dgries Date: Mon, 23 Sep 2024 17:36:18 -0400 Subject: [PATCH] Version Json and Xml --- lib/fhir_models/bootstrap/json.rb | 18 +-- lib/fhir_models/bootstrap/xml.rb | 151 ++++++++---------- lib/fhir_models/r4.rb | 8 +- .../r4/fhir_ext/element_definition.rb | 17 -- .../r4/fhir_ext/structure_definition.rb | 17 -- lib/fhir_models/r4b.rb | 8 +- .../r4b/fhir_ext/element_definition.rb | 15 -- .../r4b/fhir_ext/structure_definition.rb | 15 -- 8 files changed, 86 insertions(+), 163 deletions(-) delete mode 100644 lib/fhir_models/r4/fhir_ext/element_definition.rb delete mode 100644 lib/fhir_models/r4/fhir_ext/structure_definition.rb delete mode 100644 lib/fhir_models/r4b/fhir_ext/element_definition.rb delete mode 100644 lib/fhir_models/r4b/fhir_ext/structure_definition.rb diff --git a/lib/fhir_models/bootstrap/json.rb b/lib/fhir_models/bootstrap/json.rb index 1b89395a..3a188ddb 100644 --- a/lib/fhir_models/bootstrap/json.rb +++ b/lib/fhir_models/bootstrap/json.rb @@ -1,16 +1,12 @@ require 'json' module FHIR - module Json + module ClassJson # # This module includes methods to serialize or deserialize FHIR resources to and from JSON. # - def to_json(opts = nil) - JSON.pretty_generate(to_hash, opts) - end - - def self.from_json(json) + def from_json(json) hash = JSON.parse(json) resource = nil begin @@ -24,13 +20,11 @@ def self.from_json(json) end resource end + end - def self.fhir_version_string - FHIR.fhir_version_string - end - - def self.versioned_fhir_module - FHIR.versioned_fhir_module + module InstanceJson + def to_json(opts = nil) + JSON.pretty_generate(to_hash, opts) end end end \ No newline at end of file diff --git a/lib/fhir_models/bootstrap/xml.rb b/lib/fhir_models/bootstrap/xml.rb index beb4972a..7892f432 100644 --- a/lib/fhir_models/bootstrap/xml.rb +++ b/lib/fhir_models/bootstrap/xml.rb @@ -1,75 +1,7 @@ require 'nokogiri' module FHIR - module Xml - extend FHIR::Deprecate - # - # This module includes methods to serialize or deserialize FHIR resources to and from XML. - # - - def to_xml - hash = to_hash - hash.delete('resourceType') - doc = Nokogiri::XML::Document.new - doc.encoding = 'utf-8' - doc.root = hash_to_xml_node(resourceType, hash, doc) - doc.root.default_namespace = 'http://hl7.org/fhir' - doc.to_xml - end - - # Hash keys are ordered in Ruby 1.9 and later, so we can rely on their order - # to be the correct order for the XML serialization. - def hash_to_xml_node(name, hash, doc) - node = Nokogiri::XML::Node.new(name, doc) - - # if hash contains resourceType - # create a child node with the name==resourceType - # fill that, and place the child under the above `node` - if hash['resourceType'].is_a?(String) && name != 'instance' - child_name = hash['resourceType'] - hash.delete('resourceType') - child = hash_to_xml_node(child_name, hash, doc) - node.add_child(child) - return node - end - - hash.each do |key, value| - next if ['extension', 'modifierExtension'].include?(name) && key == 'url' - next if key == 'id' && !versioned_fhir_module::RESOURCES.include?(name) - - case value - when Hash - node.add_child(hash_to_xml_node(key, value, doc)) - when Array - value.each do |v| - if v.is_a?(Hash) - node.add_child(hash_to_xml_node(key, v, doc)) - else - child = Nokogiri::XML::Node.new(key, doc) - child.set_attribute('value', v) - node.add_child(child) - end - end - else - child = Nokogiri::XML::Node.new(key, doc) - if name == 'text' && key == 'div' - child.set_attribute('xmlns', 'http://www.w3.org/1999/xhtml') - html = value.strip - if html.start_with?('') - html = html[html.index('>') + 1..-7] - end - child.inner_html = html - else - child.set_attribute('value', value) - end - node.add_child(child) - end - end - node.set_attribute('url', hash['url']) if ['extension', 'modifierExtension'].include?(name) - node.set_attribute('id', hash['id']) if hash['id'] && !versioned_fhir_module::RESOURCES.include?(name) - node - end - - def self.from_xml(xml) + module ClassXml + def from_xml(xml) doc = Nokogiri::XML(xml) doc.root.add_namespace_definition('f', 'http://hl7.org/fhir') doc.root.add_namespace_definition('x', 'http://www.w3.org/1999/xhtml') @@ -88,7 +20,7 @@ def self.from_xml(xml) resource end - def self.xml_node_to_hash(node) + def xml_node_to_hash(node) hash = {} node.children.each do |child| next if [Nokogiri::XML::Text, Nokogiri::XML::Comment].include?(child.class) @@ -126,27 +58,84 @@ def self.xml_node_to_hash(node) end end - def self.valid?(xml) + def valid?(xml) validate(xml).empty? end - deprecate :is_valid?, :valid? - def self.validate(xml) + def validate(xml) defns = File.expand_path '../definitions/schema', File.dirname(File.absolute_path(__FILE__)) schema = File.join(defns, 'fhir-all.xsd') xsd = Nokogiri::XML::Schema(File.new(schema)) xsd.validate(Nokogiri::XML(xml)) end + end - def self.fhir_version_string - FHIR.fhir_version_string - end + module InstanceXml + # + # This module includes methods to serialize or deserialize FHIR resources to and from XML. + # - def self.versioned_fhir_module - FHIR.versioned_fhir_module + def to_xml + hash = to_hash + hash.delete('resourceType') + doc = Nokogiri::XML::Document.new + doc.encoding = 'utf-8' + doc.root = hash_to_xml_node(resourceType, hash, doc) + doc.root.default_namespace = 'http://hl7.org/fhir' + doc.to_xml end - private :hash_to_xml_node - private_class_method :xml_node_to_hash + # Hash keys are ordered in Ruby 1.9 and later, so we can rely on their order + # to be the correct order for the XML serialization. + def hash_to_xml_node(name, hash, doc) + node = Nokogiri::XML::Node.new(name, doc) + + # if hash contains resourceType + # create a child node with the name==resourceType + # fill that, and place the child under the above `node` + if hash['resourceType'].is_a?(String) && name != 'instance' + child_name = hash['resourceType'] + hash.delete('resourceType') + child = hash_to_xml_node(child_name, hash, doc) + node.add_child(child) + return node + end + + hash.each do |key, value| + next if ['extension', 'modifierExtension'].include?(name) && key == 'url' + next if key == 'id' && !versioned_fhir_module::RESOURCES.include?(name) + + case value + when Hash + node.add_child(hash_to_xml_node(key, value, doc)) + when Array + value.each do |v| + if v.is_a?(Hash) + node.add_child(hash_to_xml_node(key, v, doc)) + else + child = Nokogiri::XML::Node.new(key, doc) + child.set_attribute('value', v) + node.add_child(child) + end + end + else + child = Nokogiri::XML::Node.new(key, doc) + if name == 'text' && key == 'div' + child.set_attribute('xmlns', 'http://www.w3.org/1999/xhtml') + html = value.strip + if html.start_with?('') + html = html[html.index('>') + 1..-7] + end + child.inner_html = html + else + child.set_attribute('value', value) + end + node.add_child(child) + end + end + node.set_attribute('url', hash['url']) if ['extension', 'modifierExtension'].include?(name) + node.set_attribute('id', hash['id']) if hash['id'] && !versioned_fhir_module::RESOURCES.include?(name) + node + end end end diff --git a/lib/fhir_models/r4.rb b/lib/fhir_models/r4.rb index 11915302..7367d044 100644 --- a/lib/fhir_models/r4.rb +++ b/lib/fhir_models/r4.rb @@ -45,12 +45,14 @@ module FHIR module R4 - def self.fhir_version_string + module_function + + def fhir_version_string 'R4' end - def self.versioned_fhir_module - self + def versioned_fhir_module + FHIR::R4 end end end diff --git a/lib/fhir_models/r4/fhir_ext/element_definition.rb b/lib/fhir_models/r4/fhir_ext/element_definition.rb deleted file mode 100644 index a18b505d..00000000 --- a/lib/fhir_models/r4/fhir_ext/element_definition.rb +++ /dev/null @@ -1,17 +0,0 @@ -module FHIR - module R4 - class ElementDefinition < Model - include FHIR::CommonElementDefinition - - def self.fhir_version_string - 'R4' - end - - def self.versioned_fhir_module - FHIR::R4 - end - end - end - - ElementDefinition = FHIR::R4::ElementDefinition -end diff --git a/lib/fhir_models/r4/fhir_ext/structure_definition.rb b/lib/fhir_models/r4/fhir_ext/structure_definition.rb deleted file mode 100644 index 35abeb35..00000000 --- a/lib/fhir_models/r4/fhir_ext/structure_definition.rb +++ /dev/null @@ -1,17 +0,0 @@ -module FHIR - module R4 - class StructureDefinition - include FHIR::CommonStructureDefinition - - def self.fhir_version_string - 'R4' - end - - def self.versioned_fhir_module - FHIR::R4 - end - end - end - - StructureDefinition = FHIR::R4::StructureDefinition -end \ No newline at end of file diff --git a/lib/fhir_models/r4b.rb b/lib/fhir_models/r4b.rb index c5b8c859..b5a3e404 100644 --- a/lib/fhir_models/r4b.rb +++ b/lib/fhir_models/r4b.rb @@ -45,12 +45,14 @@ module FHIR module R4B - def self.fhir_version_string + module_function + + def fhir_version_string 'R4B' end - def self.versioned_fhir_module - self + def versioned_fhir_module + FHIR::R4B end end end diff --git a/lib/fhir_models/r4b/fhir_ext/element_definition.rb b/lib/fhir_models/r4b/fhir_ext/element_definition.rb deleted file mode 100644 index fdcc5826..00000000 --- a/lib/fhir_models/r4b/fhir_ext/element_definition.rb +++ /dev/null @@ -1,15 +0,0 @@ -module FHIR - module R4B - class ElementDefinition < Model - include CommonElementDefinition - - def self.fhir_version_string - 'R4B' - end - - def self.versioned_fhir_module - FHIR::R4B - end - end - end -end diff --git a/lib/fhir_models/r4b/fhir_ext/structure_definition.rb b/lib/fhir_models/r4b/fhir_ext/structure_definition.rb deleted file mode 100644 index beeeb12e..00000000 --- a/lib/fhir_models/r4b/fhir_ext/structure_definition.rb +++ /dev/null @@ -1,15 +0,0 @@ -module FHIR - module R4B - class StructureDefinition - include CommonStructureDefinition - - def self.fhir_version_string - 'R4B' - end - - def self.versioned_fhir_module - FHIR::R4B - end - end - end -end \ No newline at end of file