Skip to content
Browse files

Added username and password optional args to add and now attempting t…

…o import .twitter details on install and in any method that uses current_account
  • Loading branch information...
1 parent 8cfec0f commit 013b48229786c1080ee79a490e731f4b1811a7e4 @jnunemaker jnunemaker committed
Showing with 59 additions and 14 deletions.
  1. +2 −1 History.txt
  2. +0 −1 TODO.txt
  3. +29 −11 lib/twitter/cli.rb
  4. +20 −1 lib/twitter/cli/helpers.rb
  5. +8 −0 lib/twitter/cli/models/account.rb
View
3 History.txt
@@ -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
View
1 TODO.txt
@@ -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
View
40 lib/twitter/cli.rb
@@ -26,6 +26,7 @@ def run
description 'Creates the sqlite3 database and runs the migrations.'
def run
migrate
+ attempt_import
say 'Twitter installed.'
end
end
@@ -33,30 +34,48 @@ def run
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
@@ -91,7 +110,6 @@ def run
rescue ActiveRecord::RecordNotFound
say "ERROR: Account could not be found. Try again. \n"
end
-
end
end
end
View
21 lib/twitter/cli/helpers.rb
@@ -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
@@ -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
View
8 lib/twitter/cli/models/account.rb
@@ -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

0 comments on commit 013b482

Please sign in to comment.
Something went wrong with that request. Please try again.