Permalink
Browse files

Always set rcfile via flag in initialize

  • Loading branch information...
1 parent c079a65 commit fa83982638be843d3e27a04a0b28b911d121b584 @sferik committed Dec 1, 2012
Showing with 90 additions and 111 deletions.
  1. +19 −18 lib/t/cli.rb
  2. +15 −8 lib/t/delete.rb
  3. +8 −3 lib/t/list.rb
  4. +4 −5 lib/t/requestable.rb
  5. +2 −0 lib/t/search.rb
  6. +19 −11 lib/t/set.rb
  7. +6 −5 lib/t/stream.rb
  8. +6 −19 spec/cli_spec.rb
  9. +2 −7 spec/delete_spec.rb
  10. +0 −20 spec/fixtures/.trc_set
  11. +2 −6 spec/list_spec.rb
  12. +2 −2 spec/search_spec.rb
  13. +3 −3 spec/set_spec.rb
  14. +2 −4 spec/stream_spec.rb
View
@@ -26,6 +26,8 @@ class CLI < Thor
DIRECT_MESSAGE_HEADINGS = ["ID", "Posted at", "Screen name", "Text"]
TREND_HEADINGS = ["WOEID", "Parent ID", "Type", "Name", "Country"]
+ attr_reader :rcfile
+
check_unknown_options!
class_option "host", :aliases => "-H", :type => :string, :default => T::Requestable::DEFAULT_HOST, :desc => "Twitter API server"
@@ -36,24 +38,23 @@ class CLI < Thor
def initialize(*)
@rcfile = T::RCFile.instance
super
+ @rcfile.path = options['profile']
end
desc "accounts", "List accounts"
def accounts
- @rcfile.path = options['profile'] if options['profile']
- @rcfile.profiles.each do |profile|
+ rcfile.profiles.each do |profile|
say profile[0]
profile[1].keys.each do |key|
- say " #{key}#{@rcfile.active_profile[0] == profile[0] && @rcfile.active_profile[1] == key ? " (active)" : nil}"
+ say " #{key}#{rcfile.active_profile[0] == profile[0] && rcfile.active_profile[1] == key ? " (active)" : nil}"
end
end
end
desc "authorize", "Allows an application to request user authorization"
method_option "display-url", :aliases => "-d", :type => :boolean, :default => false, :desc => "Display the authorization URL instead of attempting to open it."
def authorize
- @rcfile.path = options['profile'] if options['profile']
- if @rcfile.empty?
+ if rcfile.empty?
say "Welcome! Before you can use t, you'll first need to register an"
say "application with Twitter. Just follow the steps below:"
say " 1. Sign in to the Twitter Developer site and click"
@@ -99,7 +100,7 @@ def authorize
access_token = request_token.get_access_token(:oauth_verifier => pin.chomp)
oauth_response = access_token.get('/1.1/account/verify_credentials.json?include_entities=false&skip_status=true')
screen_name = oauth_response.body.match(/"screen_name"\s*:\s*"(.*?)"/).captures.first
- @rcfile[screen_name] = {
+ rcfile[screen_name] = {
key => {
'username' => screen_name,
'consumer_key' => key,
@@ -108,7 +109,7 @@ def authorize
'secret' => access_token.secret,
}
}
- @rcfile.active_profile = {'username' => screen_name, 'consumer_key' => key}
+ rcfile.active_profile = {'username' => screen_name, 'consumer_key' => key}
say "Authorization successful."
end
@@ -118,7 +119,7 @@ def block(user, *users)
users, number = fetch_users(users.unshift(user), options) do |users|
client.block(users)
end
- say "@#{@rcfile.active_profile[0]} blocked #{pluralize(number, 'user')}."
+ say "@#{rcfile.active_profile[0]} blocked #{pluralize(number, 'user')}."
say
say "Run `#{File.basename($0)} delete block #{users.map{|user| "@#{user.screen_name}"}.join(' ')}` to unblock."
end
@@ -234,7 +235,7 @@ def dm(user, message)
user.strip_ats
end
direct_message = client.direct_message_create(user, message)
- say "Direct Message sent from @#{@rcfile.active_profile[0]} to @#{direct_message.recipient.screen_name}."
+ say "Direct Message sent from @#{rcfile.active_profile[0]} to @#{direct_message.recipient.screen_name}."
end
map %w(d m) => :dm
@@ -243,7 +244,7 @@ def dm(user, message)
def does_contain(list, user=nil)
owner, list = extract_owner(list, options)
if user.nil?
- user = @rcfile.active_profile[0]
+ user = rcfile.active_profile[0]
else
require 't/core_ext/string'
user = if options['id']
@@ -271,7 +272,7 @@ def does_follow(user1, user2=nil)
user1.strip_ats
end
if user2.nil?
- user2 = @rcfile.active_profile[0]
+ user2 = rcfile.active_profile[0]
else
user2 = if options['id']
client.user(user2.to_i).screen_name
@@ -297,7 +298,7 @@ def favorite(status_id, *status_ids)
client.favorite(status_ids)
end
number = favorites.length
- say "@#{@rcfile.active_profile[0]} favorited #{pluralize(number, 'tweet')}."
+ say "@#{rcfile.active_profile[0]} favorited #{pluralize(number, 'tweet')}."
say
say "Run `#{File.basename($0)} delete favorite #{status_ids.join(' ')}` to unfavorite."
end
@@ -332,7 +333,7 @@ def follow(user, *users)
users, number = fetch_users(users.unshift(user), options) do |users|
client.follow(users)
end
- say "@#{@rcfile.active_profile[0]} is now following #{pluralize(number, 'more user')}."
+ say "@#{rcfile.active_profile[0]} is now following #{pluralize(number, 'more user')}."
say
say "Run `#{File.basename($0)} unfollow #{users.map{|user| "@#{user.screen_name}"}.join(' ')}` to stop."
end
@@ -529,7 +530,7 @@ def reply(status_id, message)
opts = {:in_reply_to_status_id => status.id, :trim_user => true}
opts.merge!(:lat => location.lat, :long => location.lng) if options['location']
reply = client.update("#{users.join(' ')} #{message}", opts)
- say "Reply posted by @#{@rcfile.active_profile[0]} to #{users.join(' ')}."
+ say "Reply posted by @#{rcfile.active_profile[0]} to #{users.join(' ')}."
say
say "Run `#{File.basename($0)} delete status #{reply.id}` to delete."
end
@@ -540,7 +541,7 @@ def report_spam(user, *users)
users, number = fetch_users(users.unshift(user), options) do |users|
client.report_spam(users)
end
- say "@#{@rcfile.active_profile[0]} reported #{pluralize(number, 'user')}."
+ say "@#{rcfile.active_profile[0]} reported #{pluralize(number, 'user')}."
end
map %w(report reportspam spam) => :report_spam
@@ -553,7 +554,7 @@ def retweet(status_id, *status_ids)
client.retweet(status_ids, :trim_user => true)
end
number = retweets.length
- say "@#{@rcfile.active_profile[0]} retweeted #{pluralize(number, 'tweet')}."
+ say "@#{rcfile.active_profile[0]} retweeted #{pluralize(number, 'tweet')}."
say
say "Run `#{File.basename($0)} delete status #{retweets.map{|tweet| tweet.retweeted_status.id}.join(' ')}` to undo."
end
@@ -727,7 +728,7 @@ def unfollow(user, *users)
users, number = fetch_users(users.unshift(user), options) do |users|
client.unfollow(users)
end
- say "@#{@rcfile.active_profile[0]} is no longer following #{pluralize(number, 'user')}."
+ say "@#{rcfile.active_profile[0]} is no longer following #{pluralize(number, 'user')}."
say
say "Run `#{File.basename($0)} follow #{users.map{|user| "@#{user.screen_name}"}.join(' ')}` to follow again."
end
@@ -743,7 +744,7 @@ def update(message)
else
client.update(message, opts)
end
- say "Tweet posted by @#{@rcfile.active_profile[0]}."
+ say "Tweet posted by @#{rcfile.active_profile[0]}."
say
say "Run `#{File.basename($0)} delete status #{status.id}` to delete."
end
View
@@ -9,6 +9,8 @@ class Delete < Thor
include T::Requestable
include T::Utils
+ attr_reader :rcfile
+
check_unknown_options!
class_option "host", :aliases => "-H", :type => :string, :default => T::Requestable::DEFAULT_HOST, :desc => "Twitter API server"
@@ -25,31 +27,33 @@ def initialize(*)
method_option "id", :aliases => "-i", :type => :boolean, :default => false, :desc => "Specify input as Twitter user IDs instead of screen names."
method_option "force", :aliases => "-f", :type => :boolean, :default => false
def block(user, *users)
+ rcfile.path = options['profile']
users, number = fetch_users(users.unshift(user), options) do |users|
client.unblock(users)
end
- say "@#{@rcfile.active_profile[0]} unblocked #{pluralize(number, 'user')}."
+ say "@#{rcfile.active_profile[0]} unblocked #{pluralize(number, 'user')}."
say
say "Run `#{File.basename($0)} block #{users.map{|user| "@#{user.screen_name}"}.join(' ')}` to block."
end
desc "dm [DIRECT_MESSAGE_ID] [DIRECT_MESSAGE_ID...]", "Delete the last Direct Message sent."
method_option "force", :aliases => "-f", :type => :boolean, :default => false
def dm(direct_message_id, *direct_message_ids)
+ rcfile.path = options['profile']
direct_message_ids.unshift(direct_message_id)
require 't/core_ext/string'
direct_message_ids.map!(&:to_i)
if options['force']
direct_messages = client.direct_message_destroy(direct_message_ids)
direct_messages.each do |direct_message|
- say "@#{@rcfile.active_profile[0]} deleted the direct message sent to @#{direct_message.recipient.screen_name}: \"#{direct_message.text}\""
+ say "@#{rcfile.active_profile[0]} deleted the direct message sent to @#{direct_message.recipient.screen_name}: \"#{direct_message.text}\""
end
else
direct_message_ids.each do |direct_message_id|
direct_message = client.direct_message(direct_message_id)
return unless yes? "Are you sure you want to permanently delete the direct message to @#{direct_message.recipient.screen_name}: \"#{direct_message.text}\"? [y/N]"
client.direct_message_destroy(direct_message_id)
- say "@#{@rcfile.active_profile[0]} deleted the direct message sent to @#{direct_message.recipient.screen_name}: \"#{direct_message.text}\""
+ say "@#{rcfile.active_profile[0]} deleted the direct message sent to @#{direct_message.recipient.screen_name}: \"#{direct_message.text}\""
end
end
@@ -59,20 +63,21 @@ def dm(direct_message_id, *direct_message_ids)
desc "favorite TWEET_ID [TWEET_ID...]", "Delete favorites."
method_option "force", :aliases => "-f", :type => :boolean, :default => false
def favorite(status_id, *status_ids)
+ rcfile.path = options['profile']
status_ids.unshift(status_id)
require 't/core_ext/string'
status_ids.map!(&:to_i)
if options['force']
tweets = client.unfavorite(status_ids)
tweets.each do |status|
- say "@#{@rcfile.active_profile[0]} unfavorited @#{status.from_user}'s status: \"#{status.full_text}\""
+ say "@#{rcfile.active_profile[0]} unfavorited @#{status.from_user}'s status: \"#{status.full_text}\""
end
else
status_ids.each do |status_id|
status = client.status(status_id, :include_my_retweet => false, :trim_user => true)
return unless yes? "Are you sure you want to remove @#{status.from_user}'s status: \"#{status.full_text}\" from your favorites? [y/N]"
client.unfavorite(status_id)
- say "@#{@rcfile.active_profile[0]} unfavorited @#{status.from_user}'s status: \"#{status.full_text}\""
+ say "@#{rcfile.active_profile[0]} unfavorited @#{status.from_user}'s status: \"#{status.full_text}\""
end
end
end
@@ -82,6 +87,7 @@ def favorite(status_id, *status_ids)
method_option "force", :aliases => "-f", :type => :boolean, :default => false
method_option "id", :aliases => "-i", :type => :boolean, :default => false, :desc => "Specify list via ID instead of slug."
def list(list)
+ rcfile.path = options['profile']
if options['id']
require 't/core_ext/string'
list = list.to_i
@@ -91,26 +97,27 @@ def list(list)
return unless yes? "Are you sure you want to permanently delete the list \"#{list.name}\"? [y/N]"
end
client.list_destroy(list)
- say "@#{@rcfile.active_profile[0]} deleted the list \"#{list.name}\"."
+ say "@#{rcfile.active_profile[0]} deleted the list \"#{list.name}\"."
end
desc "status TWEET_ID [TWEET_ID...]", "Delete Tweets."
method_option "force", :aliases => "-f", :type => :boolean, :default => false
def status(status_id, *status_ids)
+ rcfile.path = options['profile']
status_ids.unshift(status_id)
require 't/core_ext/string'
status_ids.map!(&:to_i)
if options['force']
tweets = client.status_destroy(status_ids, :trim_user => true)
tweets.each do |status|
- say "@#{@rcfile.active_profile[0]} deleted the Tweet: \"#{status.full_text}\""
+ say "@#{rcfile.active_profile[0]} deleted the Tweet: \"#{status.full_text}\""
end
else
status_ids.each do |status_id|
status = client.status(status_id, :include_my_retweet => false, :trim_user => true)
return unless yes? "Are you sure you want to permanently delete @#{status.from_user}'s status: \"#{status.full_text}\"? [y/N]"
client.status_destroy(status_id, :trim_user => true)
- say "@#{@rcfile.active_profile[0]} deleted the Tweet: \"#{status.full_text}\""
+ say "@#{rcfile.active_profile[0]} deleted the Tweet: \"#{status.full_text}\""
end
end
end
View
@@ -17,6 +17,8 @@ class List < Thor
MAX_USERS_PER_LIST = 500
MAX_USERS_PER_REQUEST = 100
+ attr_reader :rcfile
+
check_unknown_options!
class_option "host", :aliases => "-H", :type => :string, :default => T::Requestable::DEFAULT_HOST, :desc => "Twitter API server"
@@ -32,11 +34,12 @@ def initialize(*)
desc "add LIST USER [USER...]", "Add members to a list."
method_option "id", :aliases => "-i", :type => :boolean, :default => false, :desc => "Specify input as Twitter user IDs instead of screen names."
def add(list, user, *users)
+ rcfile.path = options['profile']
users, number = fetch_users(users.unshift(user), options) do |users|
client.list_add_members(list, users)
users
end
- say "@#{@rcfile.active_profile[0]} added #{pluralize(number, 'member')} to the list \"#{list}\"."
+ say "@#{rcfile.active_profile[0]} added #{pluralize(number, 'member')} to the list \"#{list}\"."
say
if options['id']
say "Run `#{File.basename($0)} list remove --id #{list} #{users.join(' ')}` to undo."
@@ -48,10 +51,11 @@ def add(list, user, *users)
desc "create LIST [DESCRIPTION]", "Create a new list."
method_option "private", :aliases => "-p", :type => :boolean
def create(list, description=nil)
+ rcfile.path = options['profile']
opts = description ? {:description => description} : {}
opts.merge!(:mode => 'private') if options['private']
client.list_create(list, opts)
- say "@#{@rcfile.active_profile[0]} created the list \"#{list}\"."
+ say "@#{rcfile.active_profile[0]} created the list \"#{list}\"."
end
desc "information [USER/]LIST", "Retrieves detailed information about a Twitter list."
@@ -99,11 +103,12 @@ def members(list)
desc "remove LIST USER [USER...]", "Remove members from a list."
method_option "id", :aliases => "-i", :type => :boolean, :default => false, :desc => "Specify input as Twitter user IDs instead of screen names."
def remove(list, user, *users)
+ rcfile.path = options['profile']
users, number = fetch_users(users.unshift(user), options) do |users|
client.list_remove_members(list, users)
users
end
- say "@#{@rcfile.active_profile[0]} removed #{pluralize(number, 'member')} from the list \"#{list}\"."
+ say "@#{rcfile.active_profile[0]} removed #{pluralize(number, 'member')} from the list \"#{list}\"."
say
if options['id']
say "Run `#{File.basename($0)} list add --id #{list} #{users.join(' ')}` to undo."
View
@@ -13,13 +13,12 @@ def base_url
def client
return @client if @client
- @rcfile.path = options['profile'] if options['profile']
@client = Twitter::Client.new(
:endpoint => base_url,
- :consumer_key => @rcfile.active_consumer_key,
- :consumer_secret => @rcfile.active_consumer_secret,
- :oauth_token => @rcfile.active_token,
- :oauth_token_secret => @rcfile.active_secret
+ :consumer_key => rcfile.active_consumer_key,
+ :consumer_secret => rcfile.active_consumer_secret,
+ :oauth_token => rcfile.active_token,
+ :oauth_token_secret => rcfile.active_secret
)
end
View
@@ -16,6 +16,8 @@ class Search < Thor
DEFAULT_NUM_RESULTS = 20
MAX_NUM_RESULTS = 200
+ attr_reader :rcfile
+
check_unknown_options!
class_option "host", :aliases => "-H", :type => :string, :default => T::Requestable::DEFAULT_HOST, :desc => "Twitter API server"
Oops, something went wrong.

0 comments on commit fa83982

Please sign in to comment.