-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from tricknotes/support-account-info-changed
Add task to update user info
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require "#{Rails.root}/config/environment" | ||
|
||
desc 'Update account info with github.com' | ||
task :update_account_info do | ||
User.all.each do |user| | ||
puts "Fetch user info about \033[36m%s\033[39m..." % user.username | ||
begin | ||
user_info = user.github_client.user | ||
rescue => e | ||
# TODO Notify error to admin | ||
$stderr.puts ["#{e.class} #{e.message}:", *e.backtrace.map {|m| ' '+m }].join("\n") | ||
next | ||
end | ||
|
||
{ | ||
username: user_info['login'], | ||
name: user_info['name'], | ||
avatar_url: user_info['avatar_url'] | ||
}.each {|attribute, value| user.send("#{attribute}=", value) } | ||
|
||
if user.changed? | ||
change_detail = user.changes.map {|attribute, (before, after)| '%s: %s -> %s' % [attribute, before, after] }.join(', ') | ||
puts "User \033[36m%s\033[39m changed (%s)." % [user.username, change_detail] | ||
else | ||
puts "User \033[36m%s\033[39m not changed." % [user.username] | ||
end | ||
|
||
user.save! | ||
end | ||
end |