Skip to content

Commit

Permalink
Added and listing accounts and changing your current account all work.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Jul 22, 2008
1 parent d94b6bd commit 35eddef
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 33 deletions.
36 changes: 3 additions & 33 deletions bin/twitter
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
#!/usr/bin/env ruby
# = addicted to twitter
#
# ... a sweet little diddy that helps you twitter your life away from the command line
#
# == Install
#
# $ sudo gem install twitter
#
# == Command Line Use
#
# $ twitter
#
# Usage: twitter <command> [options]
#
# Available Commands:
# - post
# - timeline
# - friends
# - friend
# - followers
# - follower
#
# That will show the commands and each command will either run or show you the options it needs to run
#
# $ twitter post "releasing my new twitter gem"
#
# Got it! New twitter created at: Mon Nov 27 00:22:27 UTC 2006
#
# That will post a status update to your twitter
require 'rubygems'
require 'twitter'
require 'twitter/command'

# if we have stdin, let's prepend it to the message
if ARGV[0] && ARGV[0] == 'post' && !STDIN.tty?
ARGV[1] = "#{STDIN.read}#{ARGV[1]}"
end

Twitter::Command.process!
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'twitter'
require 'twitter/cli'
102 changes: 102 additions & 0 deletions lib/twitter/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
require 'rubygems'
gem 'main', '>= 2.8.2'
gem 'highline', '>= 1.4.0'
gem 'activerecord', '>= 2.1.0'
gem 'sqlite3-ruby', '>= 1.2.2'
require 'main'
require 'highline/import'
require 'activerecord'
require 'sqlite3'
HighLine.track_eof = false

CLI_ROOT = File.expand_path(File.join(File.dirname(__FILE__), 'cli'))
require CLI_ROOT + '/config'
require CLI_ROOT + '/helpers'

Dir[CLI_ROOT + '/models/*.rb'].each { |m| require m }

include Twitter::CLI::Helpers

Main {
def run
puts 'This is where the help goes'
end

mode 'install' do
def run
migrate
say 'Twitter setup installed.'
end
end

mode 'uninstall' do
def run
FileUtils.rm(Twitter::CLI::Config[:database])
say 'Twitter setup uninstalled.'
end
end

mode 'add' do
def run
account = Hash.new
say "Add New Account:"

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

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

do_work do
Twitter::Base.new(account[:username], account[:password]).verify_credentials
account[:current] = Account.current.count > 0 ? false : true
Account.create(account)
say 'Account added.'
end
end
end

mode 'list' do
def run
say 'Account List'
do_work do
Account.find(:all, :order => 'username').each do |a|
say a
end
end
end
end

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

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

begin
current = Account.set_current(new_current)
say "#{current} is now the current account.\n"
rescue ActiveRecord::RecordNotFound
say "ERROR: Account could not be found. Try again. \n"
end

end
end
end
}
9 changes: 9 additions & 0 deletions lib/twitter/cli/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Twitter
module CLI
Config = {
:adapter => 'sqlite3',
:database => File.join(ENV['HOME'], '.twitter.db'),
:timeout => 5000
}
end
end
36 changes: 36 additions & 0 deletions lib/twitter/cli/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Twitter
module CLI
module Helpers
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}")
end
end

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

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

def connect_and_migrate
say('Attempting to establish connection...')
connect
say('Connection established...migrating database...')
migrate
end
end
end
end
13 changes: 13 additions & 0 deletions lib/twitter/cli/migrations/20080722194500_create_accounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateAccounts < ActiveRecord::Migration
def self.up
create_table :accounts do |t|
t.string :username, :password
t.boolean :current
t.timestamps
end
end

def self.down
drop_table :accounts
end
end
16 changes: 16 additions & 0 deletions lib/twitter/cli/migrations/20080722194508_create_tweets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateTweets < ActiveRecord::Migration
def self.up
create_table :tweets do |t|
t.datetime :occurred_at
t.boolean :truncated, :favorited, :protected, :default => false
t.integer :twitter_id, :user_id, :in_reply_to_status_id, :in_reply_to_user_id, :user_followers_count
t.text :body
t.string :source, :user_name, :user_screen_name, :user_location, :user_description, :user_profile_image_url, :user_url
t.timestamps
end
end

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

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
Account.update_all "current = 0", "id != #{account.id}"
account
end

def to_s
"#{current? ? '*' : ' '} #{username}"
end
alias to_str to_s
end
2 changes: 2 additions & 0 deletions lib/twitter/cli/models/tweet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Tweet < ActiveRecord::Base
end

0 comments on commit 35eddef

Please sign in to comment.