Skip to content

Instantly share code, notes, and snippets.

@r38y
Created November 6, 2008 02:18
Show Gist options
  • Save r38y/22504 to your computer and use it in GitHub Desktop.
Save r38y/22504 to your computer and use it in GitHub Desktop.
require 'timeout'
class Twitter
def initialize(username, password)
@username = username
@password = password
end
def url
@url ||= URI.parse("http://twitter.com/statuses/update.xml")
end
def tweet_post(post)
status = "is feasting on #{post.title}: #{post_url(post)}"
tweet(status)
end
def post_url(post)
longurl = "http://#{post.user.subdomain}.#{APP_CONFIG[:domain]}/posts/#{post.permalink}"
begin
Timeout::timeout(3) do
Net::HTTP.get "is.gd", "/api.php?longurl=#{longurl}"
end
rescue Timeout::Error
longurl
end
end
def tweet(status)
req = Net::HTTP::Post.new(url.path)
req.basic_auth(@username, @password)
req.set_form_data('status' => status)
Net::HTTP.new(url.host, url.port).start { |http| http.request(req) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment