-
Notifications
You must be signed in to change notification settings - Fork 0
/
twit.rb
executable file
·89 lines (72 loc) · 2.08 KB
/
twit.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/ruby
require 'rubygems'
require 'twitter'
require 'optparse'
# UI Application
$options = {}
class App
def initialize(args)
begin
parse_options(args)
rescue ArgumentError, OptionParser::ParseError => msg
$stderr.print "Error: #{msg}\n"
exit
end
end
def parse_options(args)
commands = [:update, :user_timeline, :list_friends, :list_followers, :make_friend]
OptionParser.new do |opts|
opts.banner = "Usage: twitter [options]"
opts.on("-c", "--command COMMAND", commands) do |v|
$options[:command] = v
end
opts.on("-m", "--message MESSAGE") do |v|
$options[:message] = v
end
opts.on("-u", "--username USER") do |v|
$options[:username] = v
end
opts.on("-p", "--password PASSWORD") do |v|
$options[:password] = v
end
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
$options[:verbose] = v
end
opts.on("-i", "--id", "User Id") do |v|
$options[:userid] = v
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!(args)
case $options[:command]
when :update
raise ArgumentError, "Update needs a message" if not $options[:message]
when :make_friend
raise ArgumentError, "Make Friend needs a user id" if not $options[:userid]
end
end
def run()
auth = Twitter::HTTPAuth.new($options[:username], $options[:password])
twitter = Twitter::Base.new(auth)
case $options[:command]
when :update
print "update: %s\n" % [$options[:message]] if $options[:verbose]
twitter.update($options[:message])
when :user_timeline
twitter.user_timeline.each do |mash|
mash.each do |var|
end
end
when :list_followers
puts twitter.follower_ids
when :list_friends
puts twitter.friend_ids
when :make_friend
twitter.friendship_create($options[:userid])
end
end
end
app = App.new(ARGV)
app.run()