forked from HL7-DaVinci/pdex-plan-net-sample-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertBundles.rb
56 lines (52 loc) · 1.67 KB
/
convertBundles.rb
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
require 'json'
require 'pry'
def create_bundle
bundle = {}
bundle["entry"] = []
bundle["resourceType"] = "Bundle"
bundle["id"] = "bundle-transaction"
bundle["type"] = "transaction"
bundle
end
indir = ARGV[0] # point to output directory
Dir.glob("#{indir}/*") do |typedir|
puts "working on input directory: #{typedir}..."
resourceType = File.basename(typedir)
outfile = "#{resourceType}-bundle.json"
puts "writing to #{outfile}"
section = 0
count = 0
sectionsize = 250
o = File.open(outfile,"w")
bundle = create_bundle
Dir.glob("#{typedir}/*.json") do |jsonfile|
count = count % sectionsize
if count == 0
if section > 0
puts "Closing #{outfile}"
o.write(JSON.pretty_generate(bundle))
o.close
end
section = section + 1
outfile = "#{resourceType}-bundle-#{section}.json"
puts "Opening #{outfile}"
o = File.open(outfile,"w")
end
count = count + 1
puts "working on: #{jsonfile}..."
s = File.read(jsonfile)
resource = JSON.parse(s)
entry = {}
resourceId = resource["resourceType"] + "/" + resource["id"]
entry["resource"] = resource
entry["request"] = {
"method" => "PUT",
"url" => resourceId
}
bundle["entry"] << entry
end
if count > 0
o.write(JSON.pretty_generate(bundle))
o.close
end
end