lib/twitter/cli/helpers.rb in jnunemaker-twitter-0.3.1 vs lib/twitter/cli/helpers.rb in jnunemaker-twitter-0.3.4

- old
+ new

@@ -1,8 +1,11 @@ module Twitter module CLI module Helpers + class NoActiveAccount < StandardError; end + class NoAccounts < StandardError; end + def output_tweets(collection, options={}) options.reverse_merge!({ :cache => false, :since_prefix => '', :empty_msg => 'Nothing new since your last check.' @@ -29,14 +32,31 @@ 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 ||= Account.active + 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 begin block.call @@ -44,9 +64,15 @@ say("Twitter says you've been making too many requests. Wait for a bit and try again.") rescue Twitter::Unavailable say("Twitter is unavailable right now. Try again later.") rescue Twitter::CantConnect => msg say("Can't connect to twitter because: #{msg}") + 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 + unless attempt_import { block.call } + say("You have not created any accounts. Use 'twitter add' to create one now.") + end end end def connect ActiveRecord::Base.logger = Logger.new('/tmp/twitter_ar_logger.log') \ No newline at end of file