Skip to content
Browse files

Use count parameter with all queries

  • Loading branch information...
1 parent 7ea3f5d commit 902b14321b54f2eed86aaf31fe3c2775067393c7 @sferik committed
Showing with 32 additions and 44 deletions.
  1. +3 −15 lib/t/collectable.rb
  2. +2 −2 lib/t/list.rb
  3. +2 −2 lib/t/search.rb
  4. +11 −11 spec/list_spec.rb
  5. +14 −14 spec/search_spec.rb
View
18 lib/t/collectable.rb
@@ -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)
@@ -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
@@ -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
View
4 lib/t/list.rb
@@ -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)
View
4 lib/t/search.rb
@@ -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
View
22 spec/list_spec.rb
@@ -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")
@@ -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
View
28 spec/search_spec.rb
@@ -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")
@@ -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")

0 comments on commit 902b143

Please sign in to comment.
Something went wrong with that request. Please try again.