Skip to content

Instantly share code, notes, and snippets.

@afeld
Created August 15, 2012 23:47
Show Gist options
  • Save afeld/3364792 to your computer and use it in GitHub Desktop.
Save afeld/3364792 to your computer and use it in GitHub Desktop.
Jux: clean up garbage Twitter usernames
# using Mongoid
# clean up leading/trailing whitespace
User.where(twitter_username: /(\A\s+[A-Za-z0-9_]+\s*\z)|(\A\s*[A-Za-z0-9_]+\s+\z)/).each do |user|
user.twitter_username = user.twitter_username.strip
user.save!
end
# clean up twitter usernames of the format @... or twitter.com/...
link_rx = /(?:\A@|\A\/|twitter\.com\/(?:#!\/)?)([A-Za-z0-9_]+)\s*\z/i
User.where(twitter_username: link_rx).each do |user|
if user.twitter_username =~ link_rx
new_tu = $1
if new_tu =~ /\A[A-Za-z0-9_]+\z/
# valid
user.twitter_username = new_tu
user.save!
# puts "old: #{user.twitter_username}\t\tnew: #{new_tu}"
else
puts "failed: #{new_tu}"
end
end
end
# finally, get rid of all remaining invalid ones
User.where(twitter_username: /[^A-Za-z0-9_]/).update_all(twitter_username: nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment