5bc3ee98b5a5bd0cd936554de2bdc232

This is a script to extract and format tweets from a user for posting to a blog. I'm fairly new to ruby so I'm sure there are idioms I'm missing, or places where I've completely gone in the wrong direction. Any refactoring help much appreciated.

#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'uri'

# User configurable
user = "yourtwitterusernamehere"
# get the XML for the user
geturl = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=#{user}"

doc = Nokogiri::XML(open(geturl))
#doc = Nokogiri::XML(File.new('twitter.xml'))
# TODO - error checking

# grabbed from http://stackoverflow.com/questions/2034580/i-am-creating-a-twitter-clone-in-ruby-on-rails-how-do-i-code-it-so-that-the
def linkup_mentions_and_hashtags(text)
   text.gsub!(/@([\w]+)(\W)?/, '<a href="http://twitter.com/\1">@\1</a>\2')
   text.gsub!(/#([\w]+)(\W)?/, '<a href="http://twitter.com/search?q=%23\1">#\1</a>\2')
   text
end

# deal with various tweet parsing code to link usernames, links, etc
def parsetweet(t)
   urls = URI.extract(t,%w[ http https ftp ])
   urls.each { |url| t.gsub!(url,"<a href=\"#{url}\">#{url}</a>") }

   # auto-link @usernames
   t = linkup_mentions_and_hashtags(t)
   t
end

doc.xpath('//status').each do |status|
   tweetid = status.xpath('.//id').first.content
   datetext = status.xpath('.//created_at').first.content
   tweet = status.xpath('.//text').first.content

   url = "http://twitter.com/thinkinginrails/statuses/#{tweetid}"
   date = Date.parse(datetext)
   #datepretty = "#{date.year}/#{date.month}/#{date.day}"
   tweettext = parsetweet(tweet)
   puts "<li>#{tweettext} <em><a href=\"#{url}\">#</a></em></li>\n"
end

Refactorings

No refactoring yet !

Your refactoring





Format Copy from initial code

or Cancel