public
Last active

Code from blog post on anonymizing sensitive user data

  • Download Gist
anonymizer_source.rake
Ruby
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 35 36 37 38 39 40 41 42 43 44 45 46 47
task :env_checker do
unless Rails.env.development?
puts "Not in development environment, exiting!"
exit 1
end
end
 
namespace :app_name do
desc 'Anonymize user, company, and location information'
task :anonymize => [:environment, :env_checker] do
User.all.each do |user|
genders = ['male', 'female']
user.update_attributes!(
:born_on => rand(50*365).days.ago
:email => (rand(1000) + 100).to_s + Faker::Internet.email,
:first_name => Faker::Name.first_name,
:gender => genders.rand,
:last_name => Faker::Name.last_name)
end
 
Company.all.each do |company|
company.update_attributes!(
:description_html => Faker::Company.catch_phrase,
:name => Faker::Company.name,
:twitter_username => Faker::Internet.user_name,
:url => 'http://' + Faker::Internet.domain_name)
end
 
location_array = Location.all.inject([]) do |result, location|
result << [location.lat, location.lng]
end.shuffle
 
Location.all.each do |location|
lat, lng = location_array.pop
location.update_attributes(
:city => Faker::Address.city,
:extended_address => Faker::Address.secondary_address,
:lat => lat,
:lng => lng,
:phone => Faker::PhoneNumber.phone_number,
:postal_code => Faker::Address.zip_code,
:state => Faker::Address.state_abbr,
:street_address => Faker::Address.street_address
)
end
end
end

To generate your own phone format, you can use Faker.numerify("###-###-####")

See more at https://github.com/yyyc514/faker/blob/master/lib/faker/phone_number.rb#L5

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.