Return to Snippet

Revision: 2633
at March 18, 2007 11:11 by gtcaz


Updated Code
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"

TW_USER = 'yourusername'
TW_PASS = 'yourpassword'
TW_URL  = 'http://twitter.com/statuses/update.xml'
MAX_LEN = 140

message = STDIN.read.chomp
if message.length > MAX_LEN
  puts "Sorry, your message was #{message.length} characters long; the limit is #{MAX_LEN}."
  TextMate.exit_show_tool_tip
elsif message.empty?
  puts "No message text selected!"
  TextMate.exit_show_tool_tip
end

begin
  url = URI.parse(TW_URL)
  req = Net::HTTP::Post.new(url.path)
  req.basic_auth TW_USER, TW_PASS
  req.set_form_data({'status' => message})
  begin
    res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
    case res
      when Net::HTTPSuccess, Net::HTTPRedirection
        if res.body.empty?
          puts "Twitter is not responding properly"
          TextMate.exit_show_tool_tip
        else
          puts 'Twitter update succeeded'
          TextMate.exit_show_tool_tip
        end
      else
        puts 'Twitter update failed for an unknown reason'
        # res.error!
        TextMate.exit_show_tool_tip
      end
  rescue
    puts $!
    #puts "Twitter update failed - check username/password"
    TextMate.exit_show_tool_tip
  end
rescue SocketError
  puts "Twitter is currently unavailable"
  TextMate.exit_show_tool_tip
end

Revision: 2632
at March 18, 2007 10:39 by gtcaz


Initial Code
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"

TW_USER = 'yourusername'
TW_PASS = 'yourpassword'
TW_URL  = 'http://twitter.com/statuses/update.xml'
MAX_LEN = 140

message = STDIN.read.chomp
if message.length > MAX_LEN
  puts "Sorry, your message was #{message.length} characters long; the limit is #{MAX_LEN}."
  TextMate.exit_show_tool_tip
elsif message.empty?
  puts "No message text selected!"
  TextMate.exit_show_tool_tip
end

begin
  url = URI.parse(TW_URL)
  req = Net::HTTP::Post.new(url.path)
  req.basic_auth TW_USER, TW_PASS
  req.set_form_data({'status' => message})
  begin
    res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
    case res
      when Net::HTTPSuccess, Net::HTTPRedirection
        if res.body.empty?
          puts "Twitter is not responding properly"
          TextMate.exit_show_tool_tip
        else
          puts 'Twitter update succeeded'
          TextMate.exit_show_tool_tip
        end
      else
        puts 'Twitter update failed for an unknown reason'
        # res.error!
        TextMate.exit_show_tool_tip
      end
  rescue
    puts $!
    #puts "Twitter update failed - check username/password"
    TextMate.exit_show_tool_tip
  end
rescue SocketError
  puts "Twitter is currently unavailable"
  TextMate.exit_show_tool_tip
end

Initial URL


Initial Description
Adapted from http://journal.mychores.co.uk/articles/2007/01/21/updating-twitter-from-ruby-rails

Initial Title
Post to Twitter from e/TextMate

Initial Tags
textmate, twitter

Initial Language
Ruby