Skip to content

Commit

Permalink
Updates for the forthcoming twitter gem v5 release
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jul 28, 2013
1 parent adfed4f commit aee46e6
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 89 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
source 'https://rubygems.org'

gem 'twitter', :git => 'https://github.com/sferik/twitter.git'

gem 'rake'
gem 'jruby-openssl', :platforms => :jruby

Expand Down
54 changes: 18 additions & 36 deletions lib/t/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,10 @@ def groupies(user=nil)
client.verify_credentials.screen_name
end
follower_ids = Thread.new do
collect_with_cursor do |cursor|
client.follower_ids(user, :cursor => cursor)
end
client.follower_ids(user).to_a
end
following_ids = Thread.new do
collect_with_cursor do |cursor|
client.friend_ids(user, :cursor => cursor)
end
client.friend_ids(user).to_a
end
disciple_ids = (follower_ids.value - following_ids.value)
require 'retryable'
Expand Down Expand Up @@ -353,9 +349,7 @@ def followings(user=nil)
user.strip_ats
end
end
following_ids = collect_with_cursor do |cursor|
client.friend_ids(user, :cursor => cursor)
end
following_ids = client.friend_ids(user).to_a
require 'retryable'
users = retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
client.users(following_ids)
Expand All @@ -379,9 +373,7 @@ def followers(user=nil)
user.strip_ats
end
end
follower_ids = collect_with_cursor do |cursor|
client.follower_ids(user, :cursor => cursor)
end
follower_ids = client.follower_ids(user).to_a
require 'retryable'
users = retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
client.users(follower_ids)
Expand All @@ -408,14 +400,10 @@ def friends(user=nil)
client.verify_credentials.screen_name
end
following_ids = Thread.new do
collect_with_cursor do |cursor|
client.friend_ids(user, :cursor => cursor)
end
client.friend_ids(user).to_a
end
follower_ids = Thread.new do
collect_with_cursor do |cursor|
client.follower_ids(user, :cursor => cursor)
end
client.follower_ids(user).to_a
end
friend_ids = (following_ids.value & follower_ids.value)
require 'retryable'
Expand Down Expand Up @@ -444,14 +432,10 @@ def leaders(user=nil)
client.verify_credentials.screen_name
end
following_ids = Thread.new do
collect_with_cursor do |cursor|
client.friend_ids(user, :cursor => cursor)
end
client.friend_ids(user).to_a
end
follower_ids = Thread.new do
collect_with_cursor do |cursor|
client.follower_ids(user, :cursor => cursor)
end
client.follower_ids(user).to_a
end
leader_ids = (following_ids.value - follower_ids.value)
require 'retryable'
Expand Down Expand Up @@ -508,10 +492,10 @@ def open(user)
require 'launchy'
if options['id']
user = client.user(user.to_i)
open_or_print("https://twitter.com/#{user.screen_name}", :dry_run => options['display-url'])
open_or_print(user.url, :dry_run => options['display-url'])
elsif options['status']
status = client.status(user.to_i, :include_my_retweet => false)
open_or_print("https://twitter.com/#{status.from_user}/status/#{status.id}", :dry_run => options['display-url'])
open_or_print(status.url, :dry_run => options['display-url'])
else
require 't/core_ext/string'
open_or_print("https://twitter.com/#{user.strip_ats}", :dry_run => options['display-url'])
Expand All @@ -523,7 +507,7 @@ def open(user)
method_option "location", :aliases => "-l", :type => :string, :default => nil, :desc => "Add location information. If the optional 'latitude,longitude' parameter is not supplied, looks up location by IP address."
def reply(status_id, message)
status = client.status(status_id.to_i, :include_my_retweet => false)
users = Array(status.from_user)
users = Array(status.user.screen_name)
if options['all']
users += extract_mentioned_screen_names(status.full_text)
users.uniq!
Expand Down Expand Up @@ -601,8 +585,7 @@ def ruler
method_option "long", :aliases => "-l", :type => :boolean, :default => false, :desc => "Output in long format."
def status(status_id)
status = client.status(status_id.to_i, :include_my_retweet => false)
status_activity = client.status_activity(status_id.to_i)
location = if status.place
location = if status.place?
if status.place.name && status.place.attributes && status.place.attributes[:street_address] && status.place.attributes[:locality] && status.place.attributes[:region] && status.place.country
[status.place.name, status.place.attributes[:street_address], status.place.attributes[:locality], status.place.attributes[:region], status.place.country].join(", ")
elsif status.place.name && status.place.attributes && status.place.attributes[:locality] && status.place.attributes[:region] && status.place.country
Expand All @@ -616,27 +599,26 @@ def status(status_id)
else
status.place.name
end
elsif status.geo
elsif status.geo?
reverse_geocode(status.geo)
end
status_headings = ["ID", "Posted at", "Screen name", "Text", "Retweets", "Favorites", "Replies", "Source", "Location"]
status_headings = ["ID", "Posted at", "Screen name", "Text", "Retweets", "Favorites", "Source", "Location"]
if options['csv']
require 'csv'
say status_headings.to_csv
say [status.id, csv_formatted_time(status), status.from_user, decode_full_text(status), status.retweet_count, status_activity.favoriters_count, status_activity.repliers_count, strip_tags(status.source), location].to_csv
say [status.id, csv_formatted_time(status), status.user.screen_name, decode_full_text(status), status.retweet_count, status.favorite_count, strip_tags(status.source), location].to_csv
elsif options['long']
array = [status.id, ls_formatted_time(status), "@#{status.from_user}", decode_full_text(status).gsub(/\n+/, ' '), status.retweet_count, status_activity.favoriters_count, status_activity.repliers_count, strip_tags(status.source), location]
array = [status.id, ls_formatted_time(status), "@#{status.user.screen_name}", decode_full_text(status).gsub(/\n+/, ' '), status.retweet_count, status.favorite_count, strip_tags(status.source), location]
format = options['format'] || status_headings.size.times.map{"%s"}
print_table_with_headings([array], status_headings, format)
else
array = []
array << ["ID", status.id.to_s]
array << ["Text", decode_full_text(status).gsub(/\n+/, ' ')]
array << ["Screen name", "@#{status.from_user}"]
array << ["Screen name", "@#{status.user.screen_name}"]
array << ["Posted at", "#{ls_formatted_time(status)} (#{time_ago_in_words(status.created_at)} ago)"]
array << ["Retweets", number_with_delimiter(status.retweet_count)]
array << ["Favorites", number_with_delimiter(status_activity.favoriters_count)]
array << ["Replies", number_with_delimiter(status_activity.repliers_count)]
array << ["Favorites", number_with_delimiter(status.favorite_count)]
array << ["Source", strip_tags(status.source)]
array << ["Location", location] unless location.nil?
print_table(array)
Expand Down
8 changes: 0 additions & 8 deletions lib/t/collectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ module Collectable

MAX_NUM_RESULTS = 200

def collect_with_cursor(collection=[], cursor=-1, &block)
object = retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
yield(cursor)
end
collection += object.collection
object.last? ? collection.flatten : collect_with_cursor(collection, object.next_cursor, &block)
end

def collect_with_max_id(collection=[], max_id=nil, &block)
tweets = retryable(:tries => 3, :on => Twitter::Error::ServerError, :sleep => 0) do
yield(max_id)
Expand Down
8 changes: 4 additions & 4 deletions lib/t/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ def favorite(status_id, *status_ids)
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.user.screen_name}'s status: \"#{status.full_text}\""
end
else
status_ids.each do |status_id|
status = client.status(status_id, :include_my_retweet => false)
return unless yes? "Are you sure you want to remove @#{status.from_user}'s status: \"#{status.full_text}\" from your favorites? [y/N]"
return unless yes? "Are you sure you want to remove @#{status.user.screen_name}'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.user.screen_name}'s status: \"#{status.full_text}\""
end
end
end
Expand Down Expand Up @@ -103,7 +103,7 @@ def status(status_id, *status_ids)
else
status_ids.each do |status_id|
status = client.status(status_id, :include_my_retweet => false)
return unless yes? "Are you sure you want to permanently delete @#{status.from_user}'s status: \"#{status.full_text}\"? [y/N]"
return unless yes? "Are you sure you want to permanently delete @#{status.user.screen_name}'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}\""
end
Expand Down
8 changes: 3 additions & 5 deletions lib/t/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def information(list)
if options['csv']
require 'csv'
say ["ID", "Description", "Slug", "Screen name", "Created at", "Members", "Subscribers", "Following", "Mode", "URL"].to_csv
say [list.id, list.description, list.slug, list.user.screen_name, csv_formatted_time(list), list.member_count, list.subscriber_count, list.following?, list.mode, "https://twitter.com#{list.uri}"].to_csv
say [list.id, list.description, list.slug, list.user.screen_name, csv_formatted_time(list), list.member_count, list.subscriber_count, list.following?, list.mode, list.url].to_csv
else
array = []
array << ["ID", list.id.to_s]
Expand All @@ -69,7 +69,7 @@ def information(list)
array << ["Subscribers", number_with_delimiter(list.subscriber_count)]
array << ["Status", list.following ? "Following" : "Not following"]
array << ["Mode", list.mode]
array << ["URL", "https://twitter.com#{list.uri}"]
array << ["URL", list.url]
print_table(array)
end
end
Expand All @@ -84,9 +84,7 @@ def information(list)
method_option "unsorted", :aliases => "-u", :type => :boolean, :default => false, :desc => "Output is not sorted."
def members(list)
owner, list = extract_owner(list, options)
users = collect_with_cursor do |cursor|
client.list_members(owner, list, :cursor => cursor)
end
users = client.list_members(owner, list).to_a
print_users(users)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/t/printable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def build_long_list(list)
end

def build_long_tweet(tweet)
[tweet.id, ls_formatted_time(tweet), "@#{tweet.from_user}", decode_full_text(tweet, options['decode_urls']).gsub(/\n+/, ' ')]
[tweet.id, ls_formatted_time(tweet), "@#{tweet.user.screen_name}", decode_full_text(tweet, options['decode_urls']).gsub(/\n+/, ' ')]
end

def build_long_user(user)
Expand Down Expand Up @@ -43,7 +43,7 @@ def print_csv_list(list)
def print_csv_tweet(tweet)
require 'csv'
require 'htmlentities'
say [tweet.id, csv_formatted_time(tweet), tweet.from_user, decode_full_text(tweet)].to_csv
say [tweet.id, csv_formatted_time(tweet), tweet.user.screen_name, decode_full_text(tweet)].to_csv
end

def print_csv_user(user)
Expand Down
8 changes: 4 additions & 4 deletions lib/t/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ def all(query)
count = options['number'] || DEFAULT_NUM_RESULTS
tweets = collect_with_count(count) do |count_opts|
count_opts[:include_entities] = 1 if options['decode_urls']
client.search(query, count_opts).results
client.search(query, count_opts).to_a
end
tweets.reverse! if options['reverse']
require 'htmlentities'
if options['csv']
require 'csv'
say TWEET_HEADINGS.to_csv unless tweets.empty?
tweets.each do |tweet|
say [tweet.id, csv_formatted_time(tweet), tweet.from_user, decode_full_text(tweet)].to_csv
say [tweet.id, csv_formatted_time(tweet), tweet.user.screen_name, decode_full_text(tweet)].to_csv
end
elsif options['long']
array = tweets.map do |tweet|
[tweet.id, ls_formatted_time(tweet), "@#{tweet.from_user}", decode_full_text(tweet, options['decode_urls']).gsub(/\n+/, ' ')]
[tweet.id, ls_formatted_time(tweet), "@#{tweet.user.screen_name}", decode_full_text(tweet, options['decode_urls']).gsub(/\n+/, ' ')]
end
format = options['format'] || TWEET_HEADINGS.size.times.map{"%s"}
print_table_with_headings(array, TWEET_HEADINGS, format)
else
say unless tweets.empty?
tweets.each do |tweet|
print_message(tweet.from_user, decode_full_text(tweet, options['decode_urls']))
print_message(tweet.user.screen_name, decode_full_text(tweet, options['decode_urls']))
end
end
end
Expand Down
33 changes: 11 additions & 22 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2376,12 +2376,10 @@
describe "#status" do
before do
stub_get("/1.1/statuses/show/55709764298092545.json").with(:query => {:include_my_retweet => "false"}).to_return(:body => fixture("status.json"))
stub_get("/i/statuses/55709764298092545/activity/summary.json").to_return(:body => fixture("activity_summary.json"))
end
it "requests the correct resources" do
@cli.status("55709764298092545")
expect(a_get("/1.1/statuses/show/55709764298092545.json").with(:query => {:include_my_retweet => "false"})).to have_been_made
expect(a_get("/i/statuses/55709764298092545/activity/summary.json")).to have_been_made
end
it "has the correct output" do
@cli.status("55709764298092545")
Expand All @@ -2391,8 +2389,7 @@
Screen name @sferik
Posted at Apr 6 2011 (8 months ago)
Retweets 320
Favorites 2
Replies 1
Favorites 50
Source Twitter for iPhone
Location Blowfish Sushi To Die For, 2170 Bryant St, San Francisco, California, United States
eos
Expand All @@ -2404,8 +2401,8 @@
it "has the correct output" do
@cli.status("55709764298092545")
expect($stdout.string).to eq <<-eos
ID,Posted at,Screen name,Text,Retweets,Favorites,Replies,Source,Location
55709764298092545,2011-04-06 19:13:37 +0000,sferik,The problem with your code is that it's doing exactly what you told it to do.,320,2,1,Twitter for iPhone,"Blowfish Sushi To Die For, 2170 Bryant St, San Francisco, California, United States"
ID,Posted at,Screen name,Text,Retweets,Favorites,Source,Location
55709764298092545,2011-04-06 19:13:37 +0000,sferik,The problem with your code is that it's doing exactly what you told it to do.,320,50,Twitter for iPhone,"Blowfish Sushi To Die For, 2170 Bryant St, San Francisco, California, United States"
eos
end
end
Expand All @@ -2421,8 +2418,7 @@
Screen name @sferik
Posted at Apr 6 2011 (8 months ago)
Retweets 320
Favorites 2
Replies 1
Favorites 50
Source Twitter for iPhone
Location Blowfish Sushi To Die For, San Francisco, California, United States
eos
Expand All @@ -2440,8 +2436,7 @@
Screen name @sferik
Posted at Apr 6 2011 (8 months ago)
Retweets 320
Favorites 2
Replies 1
Favorites 50
Source Twitter for iPhone
Location Blowfish Sushi To Die For, San Francisco, California, United States
eos
Expand All @@ -2459,8 +2454,7 @@
Screen name @sferik
Posted at Apr 6 2011 (8 months ago)
Retweets 320
Favorites 2
Replies 1
Favorites 50
Source Twitter for iPhone
Location Blowfish Sushi To Die For, San Francisco, United States
eos
Expand All @@ -2478,8 +2472,7 @@
Screen name @sferik
Posted at Apr 6 2011 (8 months ago)
Retweets 320
Favorites 2
Replies 1
Favorites 50
Source Twitter for iPhone
Location Blowfish Sushi To Die For, San Francisco
eos
Expand All @@ -2497,8 +2490,7 @@
Screen name @sferik
Posted at Apr 6 2011 (8 months ago)
Retweets 320
Favorites 2
Replies 1
Favorites 50
Source Twitter for iPhone
Location Blowfish Sushi To Die For
eos
Expand All @@ -2517,8 +2509,7 @@
Screen name @sferik
Posted at Apr 6 2011 (8 months ago)
Retweets 320
Favorites 2
Replies 1
Favorites 50
Source Twitter for iPhone
Location San Francisco, CA, USA
eos
Expand All @@ -2536,8 +2527,7 @@
Screen name @sferik
Posted at Apr 6 2011 (8 months ago)
Retweets 320
Favorites 2
Replies 1
Favorites 50
Source Twitter for iPhone
Location CA, USA
eos
Expand All @@ -2556,8 +2546,7 @@
Screen name @sferik
Posted at Apr 6 2011 (8 months ago)
Retweets 320
Favorites 2
Replies 1
Favorites 50
Source Twitter for iPhone
Location USA
eos
Expand Down
1 change: 0 additions & 1 deletion spec/fixtures/activity_summary.json

This file was deleted.

0 comments on commit aee46e6

Please sign in to comment.