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 'digest'
require 'csv' require 'csv'
require 'yaml'
module Pseudonymity module Pseudonymity
class Anon class Anon
...@@ -8,8 +9,12 @@ module Pseudonymity ...@@ -8,8 +9,12 @@ module Pseudonymity
end end
def anonymize(results) def anonymize(results)
count = 0
results.collect! do | r | 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 | new_hash = r.each_with_object({}) do | (k, v), h |
if @anon_fields.include? k and !v.nil? if @anon_fields.include? k and !v.nil?
h[k] = Digest::SHA2.new(256).hexdigest v h[k] = Digest::SHA2.new(256).hexdigest v
...@@ -23,33 +28,50 @@ module Pseudonymity ...@@ -23,33 +28,50 @@ module Pseudonymity
end end
class Table class Table
class << self
def table_to_csv(table, whitelist_columns, pseudonymity_columns) config = {}
sql = "SELECT #{whitelist_columns.join(",")} from #{table}"
results = ActiveRecord::Base.connection.exec_query(sql) def initialize
anon = Anon.new(pseudonymity_columns) parse_config
results = anon.anonymize results end
write_to_csv_file table, results
def tables_to_csv
tables = @config["tables"]
tables.map do | k, v |
table_to_csv(k, v["whitelist"], v["anon"])
end end
end
def write_to_csv_file(title, contents) def table_to_csv(table, whitelist_columns, pseudonymity_columns)
file_path = "/tmp/#{title}.csv" sql = "SELECT #{whitelist_columns.join(",")} from #{table}"
if contents.empty? results = ActiveRecord::Base.connection.exec_query(sql)
File.open(file_path, "w") {} anon = Anon.new(pseudonymity_columns)
return file_path results = anon.anonymize results
end write_to_csv_file table, results
column_names = contents.first.keys end
contents = CSV.generate do | csv |
csv << column_names def parse_config
contents.each do |x| @config = YAML.load_file('./lib/assets/pseudonymity_dump.yml')
csv << x.values end
end
end def write_to_csv_file(title, contents)
File.open(file_path, 'w') { |file| file.write(contents) } file_path = "/tmp/#{title}.csv"
if contents.empty?
File.open(file_path, "w") {}
return file_path return file_path
end end
column_names = contents.first.keys
private :write_to_csv_file 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 end
private :write_to_csv_file
end end
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