Post to Twitter from e/TextMate


/ Published in: Ruby
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. #!/usr/bin/env ruby
  2. require 'net/http'
  3. require 'uri'
  4. require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"
  5.  
  6. TW_USER = 'yourusername'
  7. TW_PASS = 'yourpassword'
  8. TW_URL = 'http://twitter.com/statuses/update.xml'
  9. MAX_LEN = 140
  10.  
  11. message = STDIN.read.chomp
  12. if message.length > MAX_LEN
  13. puts "Sorry, your message was #{message.length} characters long; the limit is #{MAX_LEN}."
  14. TextMate.exit_show_tool_tip
  15. elsif message.empty?
  16. puts "No message text selected!"
  17. TextMate.exit_show_tool_tip
  18. end
  19.  
  20. begin
  21. url = URI.parse(TW_URL)
  22. req = Net::HTTP::Post.new(url.path)
  23. req.basic_auth TW_USER, TW_PASS
  24. req.set_form_data({'status' => message})
  25. begin
  26. res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
  27. case res
  28. when Net::HTTPSuccess, Net::HTTPRedirection
  29. if res.body.empty?
  30. puts "Twitter is not responding properly"
  31. TextMate.exit_show_tool_tip
  32. else
  33. puts 'Twitter update succeeded'
  34. TextMate.exit_show_tool_tip
  35. end
  36. else
  37. puts 'Twitter update failed for an unknown reason'
  38. # res.error!
  39. TextMate.exit_show_tool_tip
  40. end
  41. rescue
  42. puts $!
  43. #puts "Twitter update failed - check username/password"
  44. TextMate.exit_show_tool_tip
  45. end
  46. rescue SocketError
  47. puts "Twitter is currently unavailable"
  48. TextMate.exit_show_tool_tip
  49. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.