Permalink
Browse files

Refactor d705c79

  • Loading branch information...
1 parent aee9b03 commit aa3b3897b729766df8a7382c1a9b0340de2cfe30 @sferik committed May 6, 2012
Showing with 52 additions and 31 deletions.
  1. +21 −8 lib/t/cli.rb
  2. +21 −17 lib/t/collectable.rb
  3. +4 −2 lib/t/list.rb
  4. +4 −2 lib/t/search.rb
  5. +2 −2 spec/cli_spec.rb
View
@@ -40,7 +40,6 @@ class CLI < Thor
DEFAULT_NUM_RESULTS = 20
MAX_SCREEN_NAME_SIZE = 20
MAX_USERS_PER_REQUEST = 100
- MAX_NUM_RESULTS = 200
check_unknown_options!
@@ -130,7 +129,9 @@ def block(user, *users)
method_option "reverse", :aliases => "-r", :type => :boolean, :default => false, :desc => "Reverse the order of the sort."
def direct_messages
count = options['number'] || DEFAULT_NUM_RESULTS
- direct_messages = collect_with_count(:direct_messages, count)
+ direct_messages = collect_with_count(count) do |opts|
+ client.direct_messages(opts)
+ end
direct_messages.reverse! if options['reverse']
if options['csv']
say ["ID", "Posted at", "Screen name", "Text"].to_csv unless direct_messages.empty?
@@ -164,7 +165,9 @@ def direct_messages
method_option "reverse", :aliases => "-r", :type => :boolean, :default => false, :desc => "Reverse the order of the sort."
def direct_messages_sent
count = options['number'] || DEFAULT_NUM_RESULTS
- direct_messages = collect_with_count(:direct_messages_sent, count)
+ direct_messages = collect_with_count(count) do |opts|
+ client.direct_messages_sent(opts)
+ end
direct_messages.reverse! if options['reverse']
if options['csv']
say ["ID", "Posted at", "Screen name", "Text"].to_csv unless direct_messages.empty?
@@ -328,7 +331,9 @@ def favorites(user=nil)
end
end
count = options['number'] || DEFAULT_NUM_RESULTS
- statuses = collect_with_count(:favorites, count, {:args => [user]})
+ statuses = collect_with_count(count) do |opts|
+ client.favorites(user, opts)
+ end
print_statuses(statuses)
end
map %w(faves favourites) => :favorites
@@ -516,7 +521,9 @@ def lists(user=nil)
method_option "reverse", :aliases => "-r", :type => :boolean, :default => false, :desc => "Reverse the order of the sort."
def mentions
count = options['number'] || DEFAULT_NUM_RESULTS
- statuses = collect_with_count(:mentions, count)
+ statuses = collect_with_count(count) do |opts|
+ client.mentions(opts)
+ end
print_statuses(statuses)
end
map %w(replies) => :mentions
@@ -611,7 +618,9 @@ def retweets(user=nil)
end
end
count = options['number'] || DEFAULT_NUM_RESULTS
- statuses = collect_with_count(:retweeted_by, count, {:args => [user]})
+ statuses = collect_with_count(count) do |opts|
+ client.retweeted_by(user, opts)
+ end
print_statuses(statuses)
end
map %w(rts) => :retweets
@@ -696,9 +705,13 @@ def timeline(user=nil)
else
user.strip_ats
end
- statuses = collect_with_count(:user_timeline, count, {:args => [user]})
+ statuses = collect_with_count(count) do |opts|
+ client.user_timeline(user, opts)
+ end
else
- statuses = collect_with_count(:home_timeline, count)
+ statuses = collect_with_count(count) do |opts|
+ client.home_timeline(opts)
+ end
end
print_statuses(statuses)
end
View
@@ -3,6 +3,10 @@ module Collectable
MAX_NUM_RESULTS = 200
+ def collect_with_count(count, &block)
+ collect_with_number(count, :count, &block)
+ end
+
def collect_with_cursor(collection=[], cursor=-1, &block)
object = yield cursor
collection += object.collection
@@ -16,28 +20,28 @@ def collect_with_max_id(collection=[], max_id=nil, &block)
array.empty? ? collection : collect_with_max_id(collection, array.last.id - 1, &block)
end
- def collect_with_count(method, count, opts = {})
- # Most of the APIs use :count to request a specific count. For the few that don't, this allows
- # to specify a different count parameter (e.g. search uses :rpp and recommendations uses :limit)
- count_key = opts[:count_key].nil? ? :count : opts[:count_key]
- params = {}
- params[count_key] = MAX_NUM_RESULTS
+ def collect_with_number(number, key, &block)
+ opts = {}
+ opts[key] = MAX_NUM_RESULTS
statuses = collect_with_max_id do |max_id|
- params[:max_id] = max_id unless max_id.nil?
- params[count_key] = count unless count >= MAX_NUM_RESULTS
- if count > 0
- count -= MAX_NUM_RESULTS
+ opts[:max_id] = max_id unless max_id.nil?
+ opts[key] = number unless number >= MAX_NUM_RESULTS
+ if number > 0
+ number -= MAX_NUM_RESULTS
retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
- if opts[:args].nil? || !opts[:args].is_a?(Array) || opts[:args].empty?
- client.send(method, params)
- elsif opts[:args].length === 1
- client.send(method, opts[:args][0], params)
- elsif opts[:args].length === 2
- client.send(method, opts[:args][0], opts[:args][1], params)
- end
+ yield opts
end
end
end.flatten.compact
end
+
+ def collect_with_per_page(per_page, &block)
+ collect_with_number(per_page, :per_page, &block)
+ end
+
+ def collect_with_rpp(rpp, &block)
+ collect_with_number(rpp, :rpp, &block)
+ end
+
end
end
View
@@ -173,8 +173,10 @@ def timeline(list)
owner.strip_ats
end
end
- count = options['number'] || DEFAULT_NUM_RESULTS
- statuses = collect_with_count(:list_timeline, count, {:args => [owner, list], :count_key => :per_page})
+ per_page = options['number'] || DEFAULT_NUM_RESULTS
+ statuses = collect_with_per_page(per_page) do |opts|
+ client.list_timeline(owner, list, opts)
+ end
print_statuses(statuses)
end
map %w(tl) => :timeline
View
@@ -33,8 +33,10 @@ def initialize(*)
method_option "long", :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
method_option "number", :aliases => "-n", :type => :numeric, :default => DEFAULT_NUM_RESULTS
def all(query)
- count = options['number'] || DEFAULT_NUM_RESULTS
- statuses = collect_with_count(:search, count, {:args => [query], :count_key => :rpp})
+ rpp = options['number'] || DEFAULT_NUM_RESULTS
+ statuses = collect_with_rpp(rpp) do |opts|
+ client.search(query, opts)
+ end
if options['csv']
say ["ID", "Posted at", "Screen name", "Text"].to_csv unless statuses.empty?
statuses.each do |status|
View
@@ -197,7 +197,7 @@
a_get("/1/direct_messages.json").
with(:query => {:count => "145", :max_id => "1624782205"}).
should have_been_made
- end
+ end
end
context "--reverse" do
before do
@@ -318,7 +318,7 @@
a_get("/1/direct_messages/sent.json").
with(:query => {:count => "145", :max_id => "1624782205"}).
should have_been_made
- end
+ end
end
context "--reverse" do
before do

0 comments on commit aa3b389

Please sign in to comment.