-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_patients.rb
executable file
·35 lines (27 loc) · 1.55 KB
/
load_patients.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
#!/usr/bin/env ruby
require 'csv'
require 'httparty'
require_relative 'vars.rb'
timestamp = Time.now.strftime('%Y_%m_%d__%H_%M_%S')
working_dir = "/var/lib/mysql-files"
input_csv = "origins_msbio_etl_patients.csv"
log_directory = "/home/arenschen/origins_msbio_pipeline/logs"
transaction_log = "create_new_patients_transaction_log_#{timestamp}.txt"
csv_text = File.read(File.join(working_dir, input_csv))
csv = CSV.parse(csv_text, :headers => true)
File.open(File.join(log_directory,transaction_log),'w') do |file|
csv.each do |row|
# if row["dob"]
# # puts "#{row["dob"]}, #{row["dob"].class}"
# # row["dob"] = Date.strptime(row["dob"].to_s, '%m/%d/%y')
# end
file.write("POST: #{row.to_hash.to_json}\n")
@response = HTTParty.post("#{@production_root_url}#{@patients_url}", :body => row.to_hash.to_json, :headers => { 'Content-type'=>'application/json', 'Authorization'=>'Token token=""'})
file.write("RESPONSE:\n#{@response.headers["status"]}\n#{@response}\n-------------------------------------------------------------------\n")
unless @response.headers["status"] == "201 Created"
file.write("PUT: #{row.to_hash.to_json}\n")
@put_response = HTTParty.put("#{@production_root_url}#{@patients_put_url}#{row["external_identifier"]}", :body => row.to_hash.to_json, :headers => { "Content-type"=>"application/json", "Authorization"=>'Token token=""'})
file.write("RESPONSE:\n#{@put_response.headers["status"]}\n#{@put_response}\n-------------------------------------------------------------------\n")
end
end
end