Created
October 28, 2011 05:48
-
-
Save trvsdnn/1321710 to your computer and use it in GitHub Desktop.
twitter_link
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
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