Last active
October 15, 2021 13:06
-
-
Save steviee/3b0c17db50dd43200ba2c29c546a27da to your computer and use it in GitHub Desktop.
Pseudonomize user data in production DB (a copy, hopefully!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'progress_bar' | |
require 'json' | |
require 'open-uri' | |
namespace :pseudo do | |
desc 'Pseudonymize user data' | |
task :perform => :environment do | |
count = User.count # get count of users currently in DB | |
bar = ProgressBar.new(count) # prepare some nice output | |
options = "nat=de,us,dk,ch,fr&inc=gender,name,picture&noinfo" | |
url = "https://randomuser.me/api/?results=#{count}&#{options}" | |
puts "Fetching #{count} fake user profiles from https://randomuser.me" | |
@data = JSON.load(open(url)) # fetch and parse | |
puts "OK - Got the dataset" | |
puts "This will take some time. Go grab a coffee!" | |
User.all.each_with_index do |u,i| | |
begin | |
fake = @data["results"][i] | |
u.name = "#{fake["name"]["first"]} #{fake["name"]["last"]}" | |
u.picture_from_url(fake["picture"]["large"]) | |
u.email = "#{u.username}@xing.com" | |
u.save | |
rescue StandardError => e | |
bar.puts "ERROR: #{e}" | |
end | |
bar.increment! | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment