Skip to content

Commit

Permalink
added #base and #current_account helpers. added #follow and #leave.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Jul 22, 2008
1 parent 215b2ca commit ad2da16
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
67 changes: 45 additions & 22 deletions lib/twitter/cli.rb
Expand Up @@ -51,7 +51,7 @@ def run
end

do_work do
Twitter::Base.new(account[:username], account[:password]).verify_credentials
base(account[:username], account[:password]).verify_credentials
account[:current] = Account.current.count > 0 ? false : true
Account.create(account)
say 'Account added.'
Expand Down Expand Up @@ -131,36 +131,59 @@ def run
rescue ActiveRecord::RecordNotFound
say "ERROR: Account could not be found. Try again. \n"
end

end
end
end

mode 'post' do

def run
do_work do
account = Account.active
if account
post = if ARGV.size > 1
ARGV.join " "
else
ARGV.shift
end

say "Sending twitter update"
finished, status = false, nil
progress_thread = Thread.new { until finished; print "."; $stdout.flush; sleep 0.5; end; }
post_thread = Thread.new(binding()) do |b|
status = Twitter::Base.new(account.username, account.password).post(post, :source => Twitter::SourceName)
finished = true
end
post_thread.join
progress_thread.join
say "Got it! New twitter created at: #{status.created_at}\n"
post = if ARGV.size > 1
ARGV.join " "
else
say 'You do not have a current account set.'
ARGV.shift
end

say "Sending twitter update"
finished, status = false, nil
progress_thread = Thread.new { until finished; print "."; $stdout.flush; sleep 0.5; end; }
post_thread = Thread.new(binding()) do |b|
status = base.post(post, :source => Twitter::SourceName)
finished = true
end
post_thread.join
progress_thread.join
say "Got it! New twitter created at: #{status.created_at}\n"
end
end
end

mode 'follow' do
argument('username') {
required
description 'username or id of twitterrer to follow'
}

def run
do_work do
username = params['username'].value
base.follow(username)
say "You are now following notifications from #{username}"
end
end
end

mode 'leave' do
argument('username') {
required
description 'username or id of twitterrer to leave'
}

def run
do_work do
username = params['username'].value
base.leave(username)
say "You are no longer following notifications from #{username}"
end
end
end
Expand Down
12 changes: 11 additions & 1 deletion lib/twitter/cli/helpers.rb
@@ -1,6 +1,16 @@
module Twitter
module CLI
module Helpers
module Helpers
def base(username=current_account.username, password=current_account.password)
@base ||= Twitter::Base.new(username, password)
end

def current_account
@current_account ||= Account.active
exit('No current account.') if @current_account.blank?
@current_account
end

def do_work(&block)
connect
begin
Expand Down

0 comments on commit ad2da16

Please sign in to comment.