module HQMF2JS module Generator class CodesToJson def self.hash_to_js(hash) hash.to_json.gsub(/\"/, "'") end def self.from_value_sets(value_sets) # make sure we have a string keyed hash value_sets = JSON.parse(value_sets.to_json) translation = {} value_sets.each do |value_set| code_sets = {} value_set["concepts"].each do |code_set| code_sets[code_set["code_system_name"]] ||= [] code_sets[code_set["code_system_name"]].concat(code_set["code"].to_a) end translation[value_set["oid"]] = code_sets end translation end # Create a new Nokogiri::XML::Document instance by parsing at file at the supplied path # from an IHE SVS XML document then converts into a JSON format. The original XML is of the format: # # Originally formatted like this: # # # # # # # # # The translated JSON will be in this structure: # { # '2.16.840.1.113883.3.464.1.14' => { # 'HL7' => [ 00110 ] # } # } def self.from_xml(code_systems_file) doc = HQMF2JS::Generator::CodesToJson.parse(code_systems_file) translation = {} doc.xpath('//ValueSet').each do |set| set_list = {} set_id = set.at_xpath('@id').value set.xpath('ConceptList').each do |list| list.xpath('Concept').each do |concept| system = concept.at_xpath('@codeSystemName').value code = concept.at_xpath('@code').value codes = set_list[system] || [] codes << code set_list[system] = codes end end translation[set_id] = set_list end translation end # Parse an XML document at the supplied path # @return [Nokogiri::XML::Document] def self.parse(path) doc = Nokogiri::XML(File.new(path)) end end end end