Class: HealthDataStandards::Import::GreenC32::SectionImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/health-data-standards/import/green_c32/section_importer.rb

Instance Method Summary collapse

Constructor Details

#initializeSectionImporter

Returns a new instance of SectionImporter.



8
9
10
11
12
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 8

def initialize
  @description = "./gc32:code/gc32:originalText"
  @status = "./gc32:status"
  @value = "./gc32:value"
end

Instance Method Details

#extract_address(address_element) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 142

def extract_address(address_element)
  return unless address_element
  address = Address.new
  address.street = address_element.xpath("./gc32:street").map { |st| extract_node_text(st)  }
  address.city = extract_node_text(address_element.xpath("./gc32:city"))
  address.state = extract_node_text(address_element.xpath("./gc32:state"))
  address.zip = extract_node_text(address_element.xpath("./gc32:postalCode"))
  address
end

#extract_code(element, entry, xpath = "./gc32:code", attribute = :codes) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 27

def extract_code(element, entry, xpath="./gc32:code", attribute=:codes)
  
  code_element = element.at_xpath(xpath)

  return unless code_element

  codes = build_code(code_element)
  
  code_element.xpath("./gc32:translation").each do |trans|
    codes.merge!(build_code(trans))
  end
  
  entry.send("#{attribute}=", codes)
end

#extract_description(element, entry) ⇒ Object



42
43
44
45
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 42

def extract_description(element, entry)
  description = element.at_xpath(@description)
  entry.description = extract_node_text(description)
end

#extract_effective_time(element, entry) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 53

def extract_effective_time(element, entry)
  if element.at_xpath("./gc32:effectiveTime/gc32:start")
    extract_interval(element,entry)  
  else
    extract_time(element, entry)
  end
end

#extract_entry(element, entry) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 102

def extract_entry(element, entry)
  extract_code(element, entry)
  extract_description(element, entry)
  extract_status(element, entry)
  extract_value(element, entry)
  extract_effective_time(element, entry)
  entry.free_text = element.at_xpath("./gc32:freeText").try(:inner_text)
  entry
end

#extract_facility(facility_element) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 126

def extract_facility(facility_element)
  facility = Facility.new
  facility.name = extract_node_text(facility_element.xpath("./gc32:name"))
  facility.addresses = facility_element.xpath("./gc32:address").map { |addr| extract_address(addr)  }
  facility.telecoms = facility_element.xpath("./gc32:telecom").map { |tele| extract_telecom(tele) }
  start_time = facility_element.at_xpath("./gc32:duration/gc32:start").try(:text)
  if start_time
    facility.start_time = Time.parse(start_time).utc.to_i
  end
  end_time = facility_element.at_xpath("./gc32:duration/gc32:end").try(:text)
  if end_time
    facility.end_time = Time.parse(end_time).utc.to_i
  end
  facility
end

#extract_free_text(element, entry, free_text_element = "freeText") ⇒ Object



161
162
163
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 161

def extract_free_text(element, entry, free_text_element="freeText")
  entry.free_text = extract_node_text(element.at_xpath("./gc32:#{free_text_element}"))
end

#extract_interval(element, entry, element_name = "effectiveTime") ⇒ Object



77
78
79
80
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 77

def extract_interval(element, entry, element_name="effectiveTime")
  extract_time(element, entry, "./gc32:#{element_name}/gc32:start", "start_time")
  extract_time(element, entry, "./gc32:#{element_name}/gc32:end", "end_time")
end

#extract_name(element, entry, name_element = "name") ⇒ Object



61
62
63
64
65
66
67
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 61

def extract_name(element, entry, name_element="name")
  name_element = element.at_xpath("./gc32:#{name_element}")
  return unless name_element
  entry.title = name_element.at_xpath("./gc32:title").try(:content)
  entry.given_name = name_element.at_xpath("./gc32:givenName").try(:content)
  entry.family_name = name_element.at_xpath("./gc32:familyName").try(:content)
end

#extract_organization(organization_element) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 112

def extract_organization(organization_element)
  org_id = extract_node_text(organization_element.xpath("./gc32:id"))
  organization = org_id ? Organization.find_or_create_by(id: org_id) : Organization.new
  if organization.new_record?
  else
    organization.name = extract_node_text(organization_element.xpath("./gc32:name"))
    organization.addresses = organization_element.xpath("./gc32:address").map { |addr| extract_address(addr)  }
    organization.telecoms = organization_element.xpath("./gc32:telecom").map { |tele| extract_telecom(tele) }
    organization.save!
  end
  
  return organization
end

#extract_quantity(element, xpath) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 82

def extract_quantity(element, xpath)
  value_element = element.at_xpath(xpath)
  
  return unless value_element
  
  node_value = extract_node_attribute(value_element, "amount", true)
  node_units = extract_node_attribute(value_element, "unit")
  
  return {} unless node_value
  
  {"scalar" => node_value, "unit" => node_units}
end

#extract_status(element, entry) ⇒ Object



47
48
49
50
51
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 47

def extract_status(element, entry)
  status = extract_node_text(element.at_xpath(@status))
  return unless status
  entry.status = status
end

#extract_telecom(telecom_element) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 152

def extract_telecom(telecom_element)
  return unless telecom_element
  telecom = Telecom.new
  telecom.use = extract_node_attribute(telecom_element, :type)
  telecom.value = extract_node_attribute(telecom_element, :value)
  telecom.preferred = extract_node_attribute(telecom_element, :preferred)
  telecom
end

#extract_time(element, entry, xpath = "./gc32:effectiveTime", attribute = "time") ⇒ Object



69
70
71
72
73
74
75
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 69

def extract_time(element, entry, xpath = "./gc32:effectiveTime", attribute = "time")
  datetime = element.at_xpath(xpath)
  
  return unless datetime && datetime['value']
  
  entry.send("#{attribute}=", Time.parse(datetime['value']).utc.to_i)
end

#extract_value(element, entry) ⇒ Object



95
96
97
98
99
100
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 95

def extract_value(element, entry)
  pq = extract_quantity(element, @value)
  if pq.present?
    entry.values << PhysicalQuantityResultValue.new(pq)
  end
end

#generic_import(element_xml, element_name = "entry") ⇒ Object



19
20
21
22
23
24
25
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 19

def generic_import(element_xml, element_name="entry")
  entry = Entry.new
  element_xml.root.add_namespace_definition('gc32', "urn:hl7-org:greencda:c32")
  element = element_xml.at_xpath("./gc32:#{element_name}")
  extract_entry(element, entry)
  entry
end

#import(entry_xml) ⇒ Object



15
16
17
# File 'lib/health-data-standards/import/green_c32/section_importer.rb', line 15

def import(entry_xml)
  generic_import(entry_xml)
end