Class: Twitter::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/needish/command.rb

Constant Summary collapse

@@commands =
[:post, :timeline, :friends, :friend, :followers, :follower, :featured, :important, :follow, :leave, :d]
@@template =
<<EOF
# .twitter
# 
# Please fill in fields like this:
#
#  email: [email protected]
#  password: secret
#
email: 
password: 
EOF

Class Method Summary collapse

Class Method Details

.commandsObject



33
34
35
# File 'lib/needish/command.rb', line 33

def commands
  @@commands
end

.dObject

Posts a direct message to twitter



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/needish/command.rb', line 215

def d
  config = create_or_find_config
  if ARGV.size != 2
    puts %(\n  You didn't do it right.\n\n  Usage: twitter d jnunemaker "You're fabulous message"\n)
    exit(0)
  end

  user = ARGV.shift
  post = ARGV.shift

  status = Twitter::Base.new(config['email'], config['password']).d(user, post)
  puts "\nDirect message sent to #{user}.\n"
end


146
147
148
149
150
151
152
153
154
155
156
# File 'lib/needish/command.rb', line 146

def featured
  puts
  puts 'This is all implemented, just waiting for twitter to get the api call working'
  config = create_or_find_config
  
  puts
  Twitter::Base.new(config['email'], config['password']).featured.each do |u|
    puts "#{u.name} last updated #{u.status.created_at}\n-- #{u.status.text}"
    puts
  end
end

.followObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/needish/command.rb', line 174

def follow
  config = create_or_find_config
  
  if ARGV.size == 0
    puts %(\n  You forgot to enter a screen name or id to follow.\n\n  Usage: twitter follow jnunemaker\n)
    exit(0)
  end
  
  screen_name = ARGV.shift
  
  puts
  found = false
  begin
    Twitter::Base.new(config['email'], config['password']).follow(screen_name)
    puts "You are now following notifications for #{screen_name}."
  rescue
    puts "FAIL: Somethin went wrong. Sorry."
  end
end

.followerObject

Shows last updated status and time for a follower Needs a screen name



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/needish/command.rb', line 123

def follower
  config = create_or_find_config
  
  if ARGV.size == 0
    puts %(\n  You forgot to enter a screen name.\n\n  Usage: twitter follower jnunemaker\n)
    exit(0)
  end
  
  screen_name = ARGV.shift
  
  puts
  found = false
  Twitter::Base.new(config['email'], config['password']).followers.each do |u|
    if u.screen_name == screen_name
      puts "#{u.name} (#{u.screen_name})"
      puts "#{u.status.text} at #{u.status.created_at}" unless u.status.nil?
      found = true
    end
  end
  
  puts "Sorry couldn't find a follower of yours with #{screen_name} as a screen name" unless found
end

.followersObject

Shows all followers and their last updated status



111
112
113
114
115
116
117
118
119
# File 'lib/needish/command.rb', line 111

def followers
  config = create_or_find_config
  
  puts
  Twitter::Base.new(config['email'], config['password']).followers.each do |u|
    puts "#{u.name} (#{u.screen_name})"
    puts "#{u.status.text} at #{u.status.created_at}" unless u.status.nil?
  end
end

.friendObject

Shows last updated status and time for a friend Needs a screen name



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/needish/command.rb', line 87

def friend
  config = create_or_find_config
  
  if ARGV.size == 0
    puts %(\n  You forgot to enter a screen name.\n\n  Usage: twitter friend jnunemaker\n)
    exit(0)
  end
  
  screen_name = ARGV.shift
  
  puts
  found = false
  Twitter::Base.new(config['email'], config['password']).friends.each do |u|
    if u.screen_name == screen_name
      puts "#{u.name} #{u.screen_name}"
      puts "#{u.status.text} at #{u.status.created_at}" unless u.status.nil?
      found = true
    end
  end
  
  puts "Sorry couldn't find a friend of yours with #{screen_name} as a screen name" unless found
end

.friendsObject



75
76
77
78
79
80
81
82
83
# File 'lib/needish/command.rb', line 75

def friends
  config = create_or_find_config
  
  puts
  Twitter::Base.new(config['email'], config['password']).friends.each do |u|
    puts "#{u.name} (#{u.screen_name})"
    puts "#{u.status.text} at #{u.status.created_at}" unless u.status.nil?
  end
end

.importantObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/needish/command.rb', line 158

def important
  config = create_or_find_config
  
  puts
  if config['important'].nil?
    puts "You have not listed your most important twitter buddies in your config file.\nYou can add important twitterers by adding the following to your config file:\nimportant:\n- jnunemaker\n- frankfurter"
  else
    Twitter::Base.new(config['email'], config['password']).timeline(:friends).each do |s|          
      if config['important'].include?(s.user.screen_name)
        puts "#{s.text}\n-- #{s.user.name} at #{s.created_at}"
        puts
      end
    end
  end
end

.leaveObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/needish/command.rb', line 194

def leave
  config = create_or_find_config
  
  if ARGV.size == 0
    puts %(\n  You forgot to enter a screen name or id to leave.\n\n  Usage: twitter leave jnunemaker\n)
    exit(0)
  end
  
  screen_name = ARGV.shift
  
  puts
  found = false
  begin
    Twitter::Base.new(config['email'], config['password']).leave(screen_name)
    puts "You are no longer following notifications for #{screen_name}."
  rescue
    puts "FAIL: Somethin went wrong. Sorry."
  end
end

.postObject

Posts an updated status to twitter



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/needish/command.rb', line 38

def post
  config = create_or_find_config
  
  if ARGV.size == 0
    puts %(\n  You didn't enter a message to post.\n\n  Usage: twitter post "You're fabulous message"\n)
    exit(0)
  end
  
  post = ARGV.shift
  print "\nSending needish update"
  finished = false
  status = nil
  progress_thread = Thread.new { until finished; print "."; $stdout.flush; sleep 0.5; end; }
  post_thread = Thread.new(binding()) { |b|
    status = Needish::Base.new(config['email'], config['password']).post(post)
    finished = true
  }
  post_thread.join
  progress_thread.join
  puts " OK!"
  puts "Got it! New needish created at: #{status.created_at}\n"
end

.process!Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/needish/command.rb', line 20

def process!
  command = ARGV.shift
  
  if !command.nil? && @@commands.include?(command.intern)
    send(command)
  else
    puts "\nUsage: needish <command> [options]\n\nAvailable Commands:"
    Needish::Command.commands.each do |c|
      puts "    - #{c}"
    end
  end
end

.timelineObject

Shows status, time and user for the specified timeline



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/needish/command.rb', line 62

def timeline
  config = create_or_find_config
  
  timeline = :friends
  timeline = ARGV.shift.intern if ARGV.size > 0 && Twitter::Base.timelines.include?(ARGV[0].intern)
  
  puts
  Twitter::Base.new(config['email'], config['password']).timeline(timeline).each do |s|
    puts "#{s.text}\n-- #{s.user.name} at #{s.created_at}"
    puts
  end
end