Module: Twitter::CLI::Helpers

Defined in:
lib/twitter/cli/helpers.rb

Defined Under Namespace

Classes: NoAccounts, NoActiveAccount

Instance Method Summary collapse

Instance Method Details

#attempt_import(&block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/twitter/cli/helpers.rb', line 54

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'].nil? && !config['password'].nil?
      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

#base(username = current_account.username, password = current_account.password) ⇒ Object



44
45
46
# File 'lib/twitter/cli/helpers.rb', line 44

def base(username=.username, password=.password)
  @base ||= Twitter::Base.new(username, password)
end

#connectObject



90
91
92
93
94
# File 'lib/twitter/cli/helpers.rb', line 90

def connect
  ActiveRecord::Base.logger = Logger.new('/tmp/twitter_ar_logger.log')
  ActiveRecord::Base.establish_connection(Twitter::CLI::Config)
  ActiveRecord::Base.connection
end

#connect_and_migrateObject



101
102
103
104
105
106
# File 'lib/twitter/cli/helpers.rb', line 101

def connect_and_migrate
  say('Attempting to establish connection...')
  connect
  say('Connection established...migrating database...')
  migrate
end

#current_accountObject



48
49
50
51
52
# File 'lib/twitter/cli/helpers.rb', line 48

def 
  @current_account ||= Account.active        
  raise Account.count == 0 ? NoAccounts : NoActiveAccount if @current_account.nil?
  @current_account
end

#do_work(&block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/twitter/cli/helpers.rb', line 71

def do_work(&block)
  connect
  begin
    block.call
  rescue Twitter::RateExceeded
    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

#migrateObject



96
97
98
99
# File 'lib/twitter/cli/helpers.rb', line 96

def migrate
  connect
  ActiveRecord::Migrator.migrate("#{CLI_ROOT}/migrations/")
end

#output_tweets(collection, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/twitter/cli/helpers.rb', line 7

def output_tweets(collection, options={})
  options = {
    :cache        => false,
    :since_prefix => '',
    :empty_msg    => 'Nothing new since your last check.',
    :reverse      => false
  }.merge(options)
  
  if collection.size > 0
    justify   = collection.collect { |s| s.user.screen_name }.max { |a,b| a.length <=> b.length }.length rescue 0
    indention = ' ' * (justify + 3)
    say("\n#{indention}#{collection.size} new tweet(s) found.\n\n")
    collection.reverse! if options[:reverse]
    collection.each do |s|
      Tweet.create_from_tweet(, s) if options[:cache]
      
      occurred_at    = Time.parse(s.created_at).strftime('On %b %d at %l:%M%P')
      formatted_time = '-' * occurred_at.length + "\n#{indention}#{occurred_at}"
      formatted_name = s.user.screen_name.rjust(justify + 1)
      formatted_msg  = ''
      
      s.text.split(' ').each_with_index do |word, idx|
        formatted_msg += "#{word} "
        
        sixth_word = idx != 0 && idx % 6 == 0
        formatted_msg += "\n#{indention}" if sixth_word
      end
      
      say "#{CGI::unescapeHTML(formatted_name)}: #{CGI::unescapeHTML(formatted_msg)}\n#{indention}#{formatted_time}\n\n"
    end
    
    Configuration["#{options[:since_prefix]}_since_id"] = options[:reverse] ? collection.last.id : collection.first.id
  else
    say(options[:empty_msg])
  end
end