module Thoth::Plugin::Twitter

Twitter plugin for Thoth.

Public Class Methods

parse_tweet(tweet) click to toggle source

Parses tweet text and converts it into HTML. Explicit URLs and @username or hashtag references will be turned into links.

# File lib/thoth/plugin/thoth_twitter.rb, line 71
def parse_tweet(tweet)
  index     = 0
  html      = tweet.dup
  protocols = ['ftp', 'ftps', 'git', 'http', 'https', 'mailto', 'scp',
               'sftp', 'ssh', 'telnet']
  urls      = []

  # Extract URLs and replace them with placeholders for later.
  URI.extract(html.dup, protocols) do |url|
    html.sub!(url, "__URL#{index}__")
    urls << url
    index += 1
  end

  # Replace URL placeholders with links.
  urls.each_with_index do |url, index|
    html.sub!("__URL#{index}__", "<a href=\"#{url}\">" <<
        "#{url.length > 26 ? url[0..26] + '...' : url}</a>")
  end

  # Turn @username into a link to the specified user's Twitter profile.
  html.gsub!(/@([a-zA-Z0-9_]{1,16})([^a-zA-Z0-9_])?/,
      '@<a href="http://twitter.com/\1">\1</a>\2')

  # Turn #hashtags into links.
  html.gsub!(/#([a-zA-Z0-9_]{1,32})([^a-zA-Z0-9_])?/,
      '<a href="http://search.twitter.com/search?q=%23\1">#\1</a>\2')

  return html
end
recent_tweets(user, options = {}) click to toggle source

Gets a Hash containing recent tweets for the specified user. The only valid option currently is :count, which specifies the maximum number of tweets that should be returned.

# File lib/thoth/plugin/thoth_twitter.rb, line 105
def recent_tweets(user, options = {})
  if @skip_until
    return [] if @skip_until > Time.now
    @skip_until = nil
  end

  cache   = Ramaze::Cache.plugin
  options = {:count => 5}.merge(options)
  count   = options[:count].to_i

  count += 10 unless Config.twitter['include_replies']
  count = 200 if count > 200

  url = "http://twitter.com/statuses/user_timeline/#{user}.json?count=" <<
      count.to_s

  if value = cache[url]
    return value
  end

  tweets = []

  Timeout.timeout(Config.twitter['request_timeout'], StandardError) do
    tweets = JSON.parse(open(url).read)
  end

  # Weed out replies if necessary.
  unless Config.twitter['include_replies']
    tweets.delete_if do |tweet|
      !tweet['in_reply_to_status_id'].nil? ||
          !tweet['in_reply_to_user_id'].nil?
    end

    tweets = tweets.slice(0, options[:count].to_i)
  end

  # Parse the tweets into an easier-to-use format.
  tweets.map! do |tweet|
    {
      :created_at => Time.parse(tweet['created_at']),
      :html       => parse_tweet(tweet['text']),
      :id         => tweet['id'],
      :source     => tweet['source'],
      :text       => tweet['text'],
      :truncated  => tweet['truncated'],
      :url        => "http://twitter.com/#{user}/statuses/#{tweet['id']}"
    }
  end

  @failures = 0

  return cache.store(url, tweets, :ttl => Config.twitter['cache_ttl'])

rescue => e
  Ramaze::Log.error "Thoth::Plugin::Twitter: #{e.message}"

  @failures ||= 0
  @failures += 1

  if @failures >= Config.twitter['failure_threshold']
    @skip_until = Time.now + Config.twitter['failure_timeout']
    Ramaze::Log.error "Thoth::Plugin::Twitter: Twitter failed to respond #{@failures} times. Will retry after #{@skip_until}."
  end

  return []
end