Methods

Files

Thoth::Plugin::Twitter

Twitter plugin for Thoth.

Public Class Methods

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

  cache   = Ramaze::Cache.value_cache
  options = {:count => 5}.merge(options)

  url = "http://twitter.com/statuses/user_timeline/#{user}.json?count=" <<
      options[: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

  # Parse the tweets into an easier-to-use format.
  tweets.map! do |tweet|
    {
      :created_at => Time.parse(tweet['created_at']),
      :html       => parse_tweet(tweet),
      :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
  @failures ||= 0
  @failures += 1

  if @failures >= Config.twitter.failure_threshold
    @skip_until = Time.now + Config.twitter.failure_timeout
    Ramaze::Log.error("Twitter failed to respond #{@failures} times. " <<
        "Will retry after #{@skip_until}.")
  end

  return []
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.