Commit 0316ea13 authored by Jacob Schatz's avatar Jacob Schatz Committed by Micaël Bergeron

Add yml file instead of config.

parent 89c97fc8
This diff is collapsed.
require 'digest'
require 'csv'
require 'yaml'
module Pseudonymity
class Anon
......@@ -8,8 +9,12 @@ module Pseudonymity
end
def anonymize(results)
results.collect! do | r |
count = 0
results.each do | r |
break if count > 0
count += 1
puts r.inspect
puts @anon_fields
new_hash = r.each_with_object({}) do | (k, v), h |
if @anon_fields.include? k and !v.nil?
h[k] = Digest::SHA2.new(256).hexdigest v
......@@ -23,33 +28,50 @@ module Pseudonymity
end
class Table
class << self
def table_to_csv(table, whitelist_columns, pseudonymity_columns)
sql = "SELECT #{whitelist_columns.join(",")} from #{table}"
results = ActiveRecord::Base.connection.exec_query(sql)
anon = Anon.new(pseudonymity_columns)
results = anon.anonymize results
write_to_csv_file table, results
config = {}
def initialize
parse_config
end
def tables_to_csv
tables = @config["tables"]
tables.map do | k, v |
table_to_csv(k, v["whitelist"], v["anon"])
end
end
def write_to_csv_file(title, contents)
file_path = "/tmp/#{title}.csv"
if contents.empty?
File.open(file_path, "w") {}
return file_path
end
column_names = contents.first.keys
contents = CSV.generate do | csv |
csv << column_names
contents.each do |x|
csv << x.values
end
end
File.open(file_path, 'w') { |file| file.write(contents) }
def table_to_csv(table, whitelist_columns, pseudonymity_columns)
sql = "SELECT #{whitelist_columns.join(",")} from #{table}"
results = ActiveRecord::Base.connection.exec_query(sql)
anon = Anon.new(pseudonymity_columns)
results = anon.anonymize results
write_to_csv_file table, results
end
def parse_config
@config = YAML.load_file('./lib/assets/pseudonymity_dump.yml')
end
def write_to_csv_file(title, contents)
file_path = "/tmp/#{title}.csv"
if contents.empty?
File.open(file_path, "w") {}
return file_path
end
private :write_to_csv_file
column_names = contents.first.keys
contents = CSV.generate do | csv |
csv << column_names
contents.each do |x|
csv << x.values
end
end
File.open(file_path, 'w') { |file| file.write(contents) }
return file_path
end
private :write_to_csv_file
end
end
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment