-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathupload.rb.deprecated
executable file
·136 lines (105 loc) · 3.8 KB
/
upload.rb.deprecated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# THIS SCRIPT HAS BEEN DEPRECATED AND WILL NO LONGER BE MAINTAINED. PLEASE SWITCH TO USING THE PYTHON VERSION, i.e. upload.py
# A quick script to iterate over all of the json objects in our directory tree and post them up
# to a FHIR server
require 'bundler'
require 'yaml'
require 'logger'
log = Logger.new(STDOUT)
Bundler::require
######
# The HAPI FHIR server requires dependent objects to be loaded first
#
######
load_order = ["ValueSet",
"Device",
"Organization",
"StructureDefinition",
"Medication",
"Practitioner",
"Patient",
"AllergyIntolerance",
"AdverseReaction",
"Alert",
"MedicationRequest",
"MedicationDispense",
"MedicationAdministration",
"ImagingReference",
"Observation",
"Order",
"OrderResponse",
"Procedure",
"Endpoint",
"ImagingStudy",
"DiagnosticReport",
"Specimen",
"Condition",
"DocumentReference"]
if ARGV.length < 2
raise "usage:\n\nruby upload.rb server_config.yml path_for_recursion"
end
if !File.exists?(ARGV[0])
raise "Please specify valid yml config file"
else
server = YAML.load_file(ARGV[0])
end
# Test a basic Get against the FHIR server
# if this fails, it throws an error and the script doesn't proceed
begin
result = RestClient.get server[:url] + "Patient", apikey: server[:apikey], :params => {:_format => server[:format]}
rescue RestClient::ExceptionWithResponse => err
log.error err
end
fhir = {}
Dir["#{ARGV[1]}/**/*.json"].each do |data|
# Iterates over a directory, and loads each JSON object into a hash with
# with the key being the resourceType of the object
log.info("Loading: #{data}\n")
data_string = File.read(data)
resource = JSON.parse(File.read(data))
id = resource["id"]
if id.nil?
log.error("Error reading #{data}, make sure an ID is specified in the header comment")
raise
end
if fhir[resource["resourceType"].to_s].nil?
fhir[resource["resourceType"].to_s] = []
end
fhir[resource["resourceType"].to_s] << resource
end
# Make sure we have load_order for all of our resource types
missing_keys = fhir.keys - load_order
if missing_keys.length > 0
log.error("Resource Types missing from load order: #{missing_keys.inspect}")
raise
end
load_order.each do |resource_type|
unless fhir[resource_type].nil?
fhir[resource_type].each do |resource|
resource_type = resource["resourceType"]
id = resource["id"]
if id.nil?
log.error("Error reading #{resource}, make sure an ID is specified in the header comment")
raise
end
if id == "random"
begin
url = server[:url] + resource_type
log.info("POST - #{url}")
result = RestClient.post url, resource.to_json, :content_type => server[:format] + '+fhir', :params => {:_format => server[:format]}, apikey: server[:apikey]
rescue => e
raise e.response
end
else
begin
url = server[:url] + resource_type + "/" + id
log.info("PUT - #{url}")
result = RestClient.put url, resource.to_json, :content_type => server[:format] + '+fhir', :params => {:_format => server[:format]}, apikey: server[:apikey]
rescue => e
raise e.response
end
end
result = JSON.parse(result)
#log.info("Submission Status: #{result["issue"][0]["diagnostics"]}")
end
end
end