Created
November 6, 2008 02:18
-
-
Save r38y/22504 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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