Skip to content

Instantly share code, notes, and snippets.

@johana-star
Created December 10, 2011 02:36
Show Gist options
  • Save johana-star/1454351 to your computer and use it in GitHub Desktop.
Save johana-star/1454351 to your computer and use it in GitHub Desktop.
Using the Twitter gem — looking to fix a truncating url issue.
=begin
I'm using the home_timeline method to get a collection of tweets, and it's giving me problems with tweets which have been retweeted. A retweet will often have a truncated URL in it's text like "http://t.co/V" in "RT @noirinp: The @adainitiative are open for donations! Donate @ http://t.co/T3obm0iQ or see cool stuff they're planning @ http://t.co/V ..."
by the way, you can pull this up in IRB with the following:
require "twitter"
tweet = Twitter.status(144927777588658177)
tweet.text
I'm using the code below to
=end
@urls = Array.new
@tweets_with_urls.each_index do |i|
link = @tweets_with_urls[i].text.scan(/http\S*/)
#TODO test multiple links in one tweet
if link.last.length == 20
@urls << link
else
# I want to use an alternate method to ensure I get the untruncated url. Something like:
# @urls << @tweets_with_urls[i].method_which_gets_untruncated_url
end
end
=begin
Below is an example of output from IRB which shows the data available to me from a tweet which has a truncated URL.
ruby-1.9.2-p290 :001 > require "twitter"
=> true
ruby-1.9.2-p290 :002 > tweet = Twitter.status(144927777588658177)
=> #<Twitter::Status:0x007f9f517054e8 @attrs={"contributors"=>nil, "coordinates"=>nil, "id_str"=>"144927777588658177", "retweeted_status"=>{"contributors"=>nil, "coordinates"=>nil, "id_str"=>"144927726653018112", "retweet_count"=>2, "in_reply_to_user_id"=>nil, "favorited"=>false, "created_at"=>"Thu Dec 08 23:53:57 +0000 2011", "possibly_sensitive"=>false, "user"=>{"time_zone"=>"Vienna", "profile_text_color"=>"333333", "protected"=>false, "id_str"=>"14096201", "contributors_enabled"=>false, "following"=>false, "profile_background_image_url"=>"http://a0.twimg.com/images/themes/theme1/bg.png", "followers_count"=>856, "profile_image_url"=>"http://a0.twimg.com/profile_images/1362027848/NoirinbyTomCoates_normal.jpg", "name"=>"Noirin Plunkett", "default_profile"=>true, "profile_link_color"=>"0084B4", "profile_image_url_https"=>"https://si0.twimg.com/profile_images/1362027848/NoirinbyTomCoates_normal.jpg", "listed_count"=>79, "utc_offset"=>3600, "created_at"=>"Fri Mar 07 17:51:43 +0000 2008", "description"=>"Geek - English Translator", "notifications"=>false, "profile_background_color"=>"C0DEED", "statuses_count"=>2371, "profile_background_tile"=>false, "profile_sidebar_fill_color"=>"DDEEF6", "default_profile_image"=>false, "follow_request_sent"=>false, "lang"=>"en", "geo_enabled"=>false, "friends_count"=>244, "profile_sidebar_border_color"=>"C0DEED", "location"=>"Post-geographic!", "is_translator"=>false, "show_all_inline_media"=>true, "screen_name"=>"noirinp", "id"=>14096201, "verified"=>false, "profile_use_background_image"=>true, "profile_background_image_url_https"=>"https://si0.twimg.com/images/themes/theme1/bg.png", "favourites_count"=>59, "url"=>"http://blog.nerdchic.net/"}, "in_reply_to_screen_name"=>nil, "in_reply_to_status_id"=>nil, "in_reply_to_status_id_str"=>nil, "retweeted"=>false, "truncated"=>false, "in_reply_to_user_id_str"=>nil, "place"=>nil, "source"=>"web", "id"=>144927726653018112, "geo"=>nil, "text"=>"The @adainitiative are open for donations! Donate @ http://t.co/T3obm0iQ or see cool stuff they're planning @ http://t.co/VqXFSKxQ :-)"}, "retweet_count"=>2, "in_reply_to_user_id"=>nil, "favorited"=>false, "created_at"=>"Thu Dec 08 23:54:09 +0000 2011", "possibly_sensitive"=>false, "user"=>{"time_zone"=>"Pacific Time (US & Canada)", "profile_text_color"=>"000000", "protected"=>false, "id_str"=>"8263212", "contributors_enabled"=>false, "following"=>true, "profile_background_image_url"=>"http://a1.twimg.com/profile_background_images/2458594/It_s_A_Waffle_Window_Day_.jpg", "followers_count"=>1029, "profile_image_url"=>"http://a3.twimg.com/profile_images/1351310559/MSCHWERN_HAS_A_PAUSEID_normal.jpg", "name"=>"Schwern", "default_profile"=>false, "profile_link_color"=>"0000ff", "profile_image_url_https"=>"https://si0.twimg.com/profile_images/1351310559/MSCHWERN_HAS_A_PAUSEID_normal.jpg", "listed_count"=>138, "utc_offset"=>-28800, "created_at"=>"Sat Aug 18 09:16:37 +0000 2007", "description"=>"Bystanders were amazed at the volume of blood", "notifications"=>true, "profile_background_color"=>"F36A28", "statuses_count"=>4959, "profile_background_tile"=>false, "profile_sidebar_fill_color"=>"A8FF82", "default_profile_image"=>false, "follow_request_sent"=>false, "lang"=>"en", "geo_enabled"=>false, "friends_count"=>209, "profile_sidebar_border_color"=>"41E62D", "location"=>"Portland, OR, USA", "is_translator"=>false, "show_all_inline_media"=>false, "screen_name"=>"schwern", "id"=>8263212, "verified"=>false, "profile_use_background_image"=>true, "profile_background_image_url_https"=>"https://si0.twimg.com/profile_background_images/2458594/It_s_A_Waffle_Window_Day_.jpg", "favourites_count"=>0, "url"=>"http://schwern.net"}, "in_reply_to_screen_name"=>nil, "in_reply_to_status_id"=>nil, "in_reply_to_status_id_str"=>nil, "retweeted"=>false, "truncated"=>true, "in_reply_to_user_id_str"=>nil, "place"=>nil, "source"=>"<a href=\"http://itunes.apple.com/us/app/twitter/id409789998?mt=12\" rel=\"nofollow\">Twitter for Mac</a>", "id"=>144927777588658177, "geo"=>nil, "text"=>"RT @noirinp: The @adainitiative are open for donations! Donate @ http://t.co/T3obm0iQ or see cool stuff they're planning @ http://t.co/V ..."}>
ruby-1.9.2-p290 :003 > tweet.text
=> "RT @noirinp: The @adainitiative are open for donations! Donate @ http://t.co/T3obm0iQ or see cool stuff they're planning @ http://t.co/V ..."
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment