Ruby Twitter CLI


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



Copy this code and paste it in your HTML
  1. #!/usr/bin/env ruby
  2.  
  3. require 'twitter'
  4. require 'optparse'
  5.  
  6. # UI Application
  7.  
  8. $options = {}
  9.  
  10. class App
  11. def initialize(args)
  12. begin
  13. parse_options(args)
  14. rescue ArgumentError, OptionParser::ParseError => msg
  15. $stderr.print "Error: #{msg}\n"
  16. exit
  17. end
  18. end
  19.  
  20. def parse_options(args)
  21. commands = [:update]
  22.  
  23. OptionParser.new do |opts|
  24. opts.banner = "Usage: twitter [options]"
  25.  
  26. opts.on("-c", "--command COMMAND", commands) do |v|
  27. $options[:command] = v
  28. end
  29.  
  30. opts.on("-m", "--message MESSAGE") do |v|
  31. $options[:message] = v
  32. end
  33.  
  34. opts.on("-u", "--username USER") do |v|
  35. $options[:username] = v
  36. end
  37.  
  38. opts.on("-p", "--password PASSWORD") do |v|
  39. $options[:password] = v
  40. end
  41.  
  42. opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
  43. $options[:verbose] = v
  44. end
  45.  
  46. opts.on_tail("-h", "--help", "Show this message") do
  47. puts opts
  48. exit
  49. end
  50. end.parse!(args)
  51.  
  52. case $options[:command]
  53. when :update
  54. raise ArgumentError, "Update needs a message" if not $options[:message]
  55. end
  56. end
  57.  
  58. def run()
  59. case $options[:command]
  60. when :update
  61. print "update: %s\n" % [$options[:message]]
  62. conn = Twitter::Connection.new($options[:username], $options[:password])
  63. conn.status.update($options[:message])
  64. end
  65. end
  66. end
  67.  
  68. app = App.new(ARGV)
  69. app.run()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.