Skip to content

Commit

Permalink
Added post from current account and remove account.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Jul 22, 2008
1 parent 35eddef commit 215b2ca
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
73 changes: 69 additions & 4 deletions lib/twitter/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def run
mode 'uninstall' do
def run
FileUtils.rm(Twitter::CLI::Config[:database])
say 'Twitter setup uninstalled.'
say 'Twitter gem uninstalled.'
end
end

Expand All @@ -59,12 +59,48 @@ def run
end
end

mode 'remove' do
argument( 'username' ) {
optional
description 'username of account you would like to remove'
}

def run
do_work do
if params['username'].given?
account = Account.find_by_username(params['username'].value)
else
Account.find(:all, :order => 'username').each do |a|
say "#{a.id}. #{a}"
end
account_id = ask 'Account to remove (enter number): ' do |q|
q.validate = /\d+/
end
end

begin
account = account_id ? Account.find(account_id) : account
account_name = account.username
account.destroy
say "#{account_name} has been removed.\n"
rescue ActiveRecord::RecordNotFound
say "ERROR: Account could not be found. Try again. \n"
end

end
end
end

mode 'list' do
def run
say 'Account List'
do_work do
Account.find(:all, :order => 'username').each do |a|
say a
if Account.count == 0
say 'No accounts have been added.'
else
say 'Account List'
Account.find(:all, :order => 'username').each do |a|
say a
end
end
end
end
Expand Down Expand Up @@ -99,4 +135,33 @@ def run
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"
else
say 'You do not have a current account set.'
end
end
end
end
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddAccountIdToTweets < ActiveRecord::Migration
def self.up
add_column :tweets, :account_id, :integer
end

def self.down
remove_column :tweets, :account_id
end
end
6 changes: 6 additions & 0 deletions lib/twitter/cli/models/account.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
class Account < ActiveRecord::Base
named_scope :current, :conditions => {:current => true}

has_many :tweets, :dependent => :destroy

def self.active
current.first
end

def self.set_current(account_or_id)
account = account_or_id.is_a?(Account) ? account_or_id : find(account_or_id)
account.update_attribute :current, true
Expand Down
1 change: 1 addition & 0 deletions lib/twitter/cli/models/tweet.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Tweet < ActiveRecord::Base
belongs_to :account
end

0 comments on commit 215b2ca

Please sign in to comment.