Skip to content

Instantly share code, notes, and snippets.

@trvsdnn
Created October 28, 2011 05:48
Show Gist options
  • Save trvsdnn/1321710 to your computer and use it in GitHub Desktop.
Save trvsdnn/1321710 to your computer and use it in GitHub Desktop.
twitter_link
def twitter_link(handle)
avatars_path = 'public/img/avatars/'
avatar = handle + '.jpg'
avatar_path = avatars_path + avatar
img_tag = "<img src='#{avatar_path.sub(/^public/, '')}' />"
# Check and see if there's already an avatar.
unless File.exists?(avatar_path)
# We don't have it. Let's try to get it from Twitter.
begin
img_url = Twitter.profile_image(handle, :size => 'mini')
open(avatar_path, 'wb') do |file|
file << open(img_url).read
end
rescue Exception => e
img_tag = nil
end
end
if img_tag
"<a href='http://twitter.com'/#{handle}>#{img_tag}</a>"
else
# whatever you want to return if the image tag isn't there
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment