Skip to content

Commit

Permalink
Use count parameter with all queries
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Nov 20, 2012
1 parent 7ea3f5d commit 902b143
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 44 deletions.
18 changes: 3 additions & 15 deletions lib/t/collectable.rb
Expand Up @@ -6,10 +6,6 @@ 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 = retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
yield(cursor)
Expand All @@ -27,12 +23,12 @@ def collect_with_max_id(collection=[], max_id=nil, &block)
tweets.empty? ? collection.flatten : collect_with_max_id(collection, tweets.last.id - 1, &block)
end

def collect_with_number(number, key, &block)
def collect_with_count(number, &block)
opts = {}
opts[key] = MAX_NUM_RESULTS
opts[:count] = MAX_NUM_RESULTS
collect_with_max_id do |max_id|
opts[:max_id] = max_id unless max_id.nil?
opts[key] = number unless number >= MAX_NUM_RESULTS
opts[:count] = number unless number >= MAX_NUM_RESULTS
if number > 0
tweets = yield opts
number -= tweets.length
Expand All @@ -41,14 +37,6 @@ def collect_with_number(number, key, &block)
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

def collect_with_page(collection=[], page=1, &block)
tweets = retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
yield page
Expand Down
4 changes: 2 additions & 2 deletions lib/t/list.rb
Expand Up @@ -120,8 +120,8 @@ def remove(list, user, *users)
method_option "reverse", :aliases => "-r", :type => :boolean, :default => false, :desc => "Reverse the order of the sort."
def timeline(list)
owner, list = extract_owner(list, options)
per_page = options['number'] || DEFAULT_NUM_RESULTS
tweets = collect_with_per_page(per_page) do |opts|
count = options['number'] || DEFAULT_NUM_RESULTS
tweets = collect_with_count(count) do |opts|
client.list_timeline(owner, list, opts)
end
print_tweets(tweets)
Expand Down
4 changes: 2 additions & 2 deletions lib/t/search.rb
Expand Up @@ -34,8 +34,8 @@ def initialize(*)
method_option "decode_urls", :aliases => "-d", :type => :boolean, :default => false, :desc => "Decodes t.co URLs into their original form."
method_option "number", :aliases => "-n", :type => :numeric, :default => DEFAULT_NUM_RESULTS
def all(query)
rpp = options['number'] || DEFAULT_NUM_RESULTS
tweets = collect_with_rpp(rpp) do |opts|
count = options['number'] || DEFAULT_NUM_RESULTS
tweets = collect_with_count(count) do |opts|
opts[:include_entities] = 1 if options['decode_urls']
client.search(query, opts).results
end
Expand Down
22 changes: 11 additions & 11 deletions spec/list_spec.rb
Expand Up @@ -311,11 +311,11 @@

describe "#timeline" do
before do
stub_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :per_page => "20", :slug => "presidents"}).to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :count => "20", :slug => "presidents"}).to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@list.timeline("presidents")
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :per_page => "20", :slug => "presidents"})).to have_been_made
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :count => "20", :slug => "presidents"})).to have_been_made
end
it "has the correct output" do
@list.timeline("presidents")
Expand Down Expand Up @@ -492,35 +492,35 @@
end
context "--number" do
before do
stub_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :per_page => "1", :slug => "presidents"}).to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :per_page => "200", :slug => "presidents"}).to_return(:body => fixture("200_statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :per_page => "1", :max_id => "265500541700956160", :slug => "presidents"}).to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :count => "1", :slug => "presidents"}).to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :count => "200", :slug => "presidents"}).to_return(:body => fixture("200_statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :count => "1", :max_id => "265500541700956160", :slug => "presidents"}).to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "limits the number of results to 1" do
@list.options = @list.options.merge("number" => 1)
@list.timeline("presidents")
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :per_page => "1", :slug => "presidents"})).to have_been_made
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :count => "1", :slug => "presidents"})).to have_been_made
end
it "limits the number of results to 201" do
@list.options = @list.options.merge("number" => 201)
@list.timeline("presidents")
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :per_page => "200", :slug => "presidents"})).to have_been_made
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :per_page => "1", :max_id => "265500541700956160", :slug => "presidents"})).to have_been_made
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :count => "200", :slug => "presidents"})).to have_been_made
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :count => "1", :max_id => "265500541700956160", :slug => "presidents"})).to have_been_made
end
end
context "with a user passed" do
it "requests the correct resource" do
@list.timeline("testcli/presidents")
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :per_page => "20", :slug => "presidents"})).to have_been_made
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_screen_name => "testcli", :count => "20", :slug => "presidents"})).to have_been_made
end
context "--id" do
before do
@list.options = @list.options.merge("id" => true)
stub_get("/1.1/lists/statuses.json").with(:query => {:owner_id => "7505382", :per_page => "20", :slug => "presidents"}).to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/lists/statuses.json").with(:query => {:owner_id => "7505382", :count => "20", :slug => "presidents"}).to_return(:body => fixture("statuses.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@list.timeline("7505382/presidents")
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_id => "7505382", :per_page => "20", :slug => "presidents"})).to have_been_made
expect(a_get("/1.1/lists/statuses.json").with(:query => {:owner_id => "7505382", :count => "20", :slug => "presidents"})).to have_been_made
end
end
end
Expand Down
28 changes: 14 additions & 14 deletions spec/search_spec.rb
Expand Up @@ -30,11 +30,11 @@

describe "#all" do
before do
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => "20"}).to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => "20"}).to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "requests the correct resource" do
@search.all("twitter")
expect(a_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => "20"})).to have_been_made
expect(a_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => "20"})).to have_been_made
end
it "has the correct output" do
@search.all("twitter")
Expand Down Expand Up @@ -656,30 +656,30 @@
end
context "--number" do
before do
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => "1"}).to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => "200"}).to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => "103", :max_id => "267024711169503231"}).to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => "5", :max_id => "267024711169503231"}).to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => "1"}).to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => "200"}).to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => "103", :max_id => "267024711169503231"}).to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => "5", :max_id => "267024711169503231"}).to_return(:body => fixture("search.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "limits the number of results to 1" do
@search.options = @search.options.merge("number" => 1)
results = @search.all("twitter")
expect(a_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => "1"})).to have_been_made
expect(a_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => "1"})).to have_been_made
end
it "limits the number of results to 201" do
@search.options = @search.options.merge("number" => 201)
@search.all("twitter")
expect(a_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => "200"})).to have_been_made
expect(a_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => "103", :max_id => "267024711169503231"})).to have_been_made
expect(a_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => "5", :max_id => "267024711169503231"})).to have_been_made
expect(a_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => "200"})).to have_been_made
expect(a_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => "103", :max_id => "267024711169503231"})).to have_been_made
expect(a_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => "5", :max_id => "267024711169503231"})).to have_been_made
end
end
context "--decode_urls" do
before(:each) do
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => 20}).to_return(:body => fixture("search_with_entities.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :rpp => 5, :max_id => 264784855672442882}).to_return(:body => fixture("search_with_entities.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :include_entities => 1, :rpp => 20}).to_return(:body => fixture("search_with_entities.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :include_entities => 1, :rpp => 5, :max_id => 264784855672442882}).to_return(:body => fixture("search_with_entities.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => 20}).to_return(:body => fixture("search_with_entities.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :count => 5, :max_id => 264784855672442882}).to_return(:body => fixture("search_with_entities.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :include_entities => 1, :count => 20}).to_return(:body => fixture("search_with_entities.json"), :headers => {:content_type => "application/json; charset=utf-8"})
stub_get("/1.1/search/tweets.json").with(:query => {:q => "twitter", :include_entities => 1, :count => 5, :max_id => 264784855672442882}).to_return(:body => fixture("search_with_entities.json"), :headers => {:content_type => "application/json; charset=utf-8"})
end
it "does not decode urls without given the explicit option" do
@search.all("twitter")
Expand Down

0 comments on commit 902b143

Please sign in to comment.