Skip to content

Commit

Permalink
Added username and password optional args to add and now attempting t…
Browse files Browse the repository at this point in the history
…o import .twitter details on install and in any method that uses current_account
  • Loading branch information
jnunemaker committed Jul 26, 2008
1 parent 8cfec0f commit 013b482
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
3 changes: 2 additions & 1 deletion History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* added the CLI gems as dependencies for now until I separate out the CLI from the API wrapper
* cleaner CLI errors for no active account or no accounts at all
* remove sets a new active account if there are none

* added username and password optional arguments to add
* added import attempt on install and on anything that uses #current_account helper

0.3.1 - July 23, 2008
* added open to CLI twitter open jnunemaker would open default browser to http://twitter.com/jnunemaker
Expand Down
1 change: 0 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
* import from .twitter
* add timeout so it doesn't hang forever like it does now if twitter is down
* add progress indicator for timeline and replies as posting has for more visual indication that work is happening
40 changes: 29 additions & 11 deletions lib/twitter/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,56 @@ def run
description 'Creates the sqlite3 database and runs the migrations.'
def run
migrate
attempt_import
say 'Twitter installed.'
end
end

mode 'uninstall' do
description 'Removes the sqlite3 database. There is no undo for this.'
def run
FileUtils.rm(Twitter::CLI::Config[:database])
FileUtils.rm(Twitter::CLI::Config[:database]) if File.exists?(Twitter::CLI::Config[:database])
say 'Twitter gem uninstalled.'
end
end

mode 'add' do
description 'Adds a new twitter account to the database. Prompts for username and password.'
argument('username', 'u') {
optional
description 'optional username'
}
argument('password', 'p') {
optional
description 'optional password'
}

def run
account = Hash.new
say "Add New Account:"

account[:username] = ask('Username: ') do |q|
q.validate = /\S+/

# allows optional username arg
if params['username'].given?
account[:username] = params['username'].value
else
account[:username] = ask('Username: ') do |q|
q.validate = /\S+/
end
end

account[:password] = ask("Password (won't be displayed): ") do |q|
q.echo = false
q.validate = /\S+/

# allows optional password arg
if params['password'].given?
account[:password] = params['password'].value
else
account[:password] = ask("Password (won't be displayed): ") do |q|
q.echo = false
q.validate = /\S+/
end
end

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

end
end
end
Expand Down
21 changes: 20 additions & 1 deletion lib/twitter/cli/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ def current_account
raise Account.count == 0 ? NoAccounts : NoActiveAccount if @current_account.blank?
@current_account
end

def attempt_import(&block)
tweet_file = File.join(ENV['HOME'], '.twitter')
if File.exists?(tweet_file)
say '.twitter file found, attempting import...'
config = YAML::load(File.read(tweet_file))
if !config['email'].blank? && !config['password'].blank?
Account.add(:username => config['email'], :password => config['password'])
say 'Account imported'
block.call if block_given?
true
else
say "Either your username or password were blank in your .twitter file so I could not import. Use 'twitter add' to add an account."
false
end
end
end

def do_work(&block)
connect
Expand All @@ -52,7 +69,9 @@ def do_work(&block)
rescue Twitter::CLI::Helpers::NoActiveAccount
say("You have not set an active account. Use 'twitter change' to set one now.")
rescue Twitter::CLI::Helpers::NoAccounts
say("You have not created any accounts. Use 'twitter add' to create one now.")
unless attempt_import { block.call }
say("You have not created any accounts. Use 'twitter add' to create one now.")
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions lib/twitter/cli/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ class Account < ActiveRecord::Base

has_many :tweets, :dependent => :destroy

def self.add(hash)
username = hash.delete(:username)
account = find_or_initialize_by_username(username)
account.attributes = hash
account.save
set_current(account) if new_active_needed?
end

def self.active
current.first
end
Expand Down

0 comments on commit 013b482

Please sign in to comment.