This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
generated from clinical-meteor/node-on-fhir
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
98 lines (80 loc) · 3.42 KB
/
Rakefile
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
# frozen_string_literal: true
require 'json'
require 'rest-client'
require 'fhir_models'
task default: %w[help]
## ========= Server endpoints (UPDATE THESE) ====================================================
INDEX_PATIENT_URL = "http://127.0.0.1:3000/baseR4/Patient" # assumes GET
CREATE_PATIENT_URL = "http://127.0.0.1:3000/baseR4/Patient" # assumes POST
DELETE_PATIENT_URL = "http://127.0.0.1:3000/baseR4/Patient/:id" # assumes DELETE
## ========= Helper functions ===================================================================
# iterate through fixtures/*.json
def for_each_json_fixture(&block)
Dir.each_child( File.join(__dir__, "fixtures", "patient") ) do |filename|
path = File.join(__dir__, "fixtures", "patient", filename)
if filename.end_with? '.json'
json = File.read(path)
yield(filename, json)
end
end
end
# report rest api call status
def put_status(response, success_msg = "success", error_msg = "error")
if response.code >= 200 && response.code < 300 or (response.code == 303)
puts success_msg
else
puts error_msg
end
end
## ========= Tasks ==============================================================================
desc "Print help information for Rake (Ruby Make)"
task :help do
puts <<~EOS
Rake (Ruby Make) tasks for Identity Matching Server.
Go to #{__FILE__} line 10 and set your server endpoints.
First launch server as listed in README, then run the commands below as needed:
$ bundle exec rake --tasks
# => print available tasks and descriptions
$ bundle exec rake seed
# => send POST requests to seed server with all FHIR patients from fixtures/patients/
$ bundle exec rake "seed:bundle[/path/to/bundle/file]"
# => sends POST request for each patient entry in FHIR bundle to create entire bundle
$ bundle exec rake drop
# => delete all FHIR patients from server
$ bundle exec rake help
# => print this message
EOS
end
desc "create FHIR patients from fixtures/ into server database"
task :seed do
for_each_json_fixture do |filename, json|
print "Creating #{filename} ... "
response = RestClient.post(CREATE_PATIENT_URL, json, {'Content-Type' => 'application/fhir+json', 'Content-Length' => json.length})
put_status(response, "success (code = #{response.code})", "error (code = #{response.code})")
end
end
namespace :seed do
desc "create FHIR patients from a bundle"
task :bundle, [:filename] do |t, args|
json = File.read(args.filename)
bundle = FHIR.from_contents(json)
total = bundle.entry.length
bundle.entry.map(&:resource).each_with_index do |resource, i|
if resource.resourceType == 'Patient'
response = RestClient.post(CREATE_PATIENT_URL, resource.to_json, {'Content-Type' => 'application/fhir+json', 'Content-Length' => resource.to_json.length})
put_status(response, "created entry #{i + 1}/#{total} (code = #{response.code})", "error on entry #{i + 1}/#{total} (code = #{response.code})")
end
end
end
end
desc "delete all FHIR patients from server"
task :drop do
response = RestClient.get(INDEX_PATIENT_URL, headers: {'Accept' => 'application/fhir+json'})
bundle = FHIR.from_contents( response.body )
bundle.entry.map(&:resource).each do |resource|
print "Deleting patient #{resource.id} ... "
delete_url = DELETE_PATIENT_URL.gsub(':id', resource.id)
response = RestClient.delete(delete_url)
put_status(response)
end
end