Skip to content

Instantly share code, notes, and snippets.

@steviee
Last active October 15, 2021 13:06
Show Gist options
  • Save steviee/3b0c17db50dd43200ba2c29c546a27da to your computer and use it in GitHub Desktop.
Save steviee/3b0c17db50dd43200ba2c29c546a27da to your computer and use it in GitHub Desktop.
Pseudonomize user data in production DB (a copy, hopefully!)
# 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