Skip to content
Browse files

Add rate_limit command

  • Loading branch information...
1 parent 87f14f4 commit b177299b1d6f4d261e7e9fdb9364df1c45d873f9 @sferik sferik committed May 7, 2012
Showing with 57 additions and 8 deletions.
  1. +22 −6 lib/t/cli.rb
  2. +1 −1 lib/t/list.rb
  3. +1 −1 lib/t/search.rb
  4. +32 −0 spec/cli_spec.rb
  5. +1 −0 spec/fixtures/rate_limit_status.json
View
28 lib/t/cli.rb
@@ -135,7 +135,7 @@ def direct_messages
if options['csv']
say DIRECT_MESSAGE_HEADINGS.to_csv unless direct_messages.empty?
direct_messages.each do |direct_message|
- say [direct_message.id, direct_message.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"), direct_message.sender.screen_name, direct_message.text].to_csv
+ say [direct_message.id, csv_formatted_time(direct_message), direct_message.sender.screen_name, direct_message.text].to_csv
end
elsif options['long']
array = direct_messages.map do |direct_message|
@@ -164,7 +164,7 @@ def direct_messages_sent
if options['csv']
say DIRECT_MESSAGE_HEADINGS.to_csv unless direct_messages.empty?
direct_messages.each do |direct_message|
- say [direct_message.id, direct_message.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"), direct_message.recipient.screen_name, direct_message.text].to_csv
+ say [direct_message.id, csv_formatted_time(direct_message), direct_message.recipient.screen_name, direct_message.text].to_csv
end
elsif options['long']
array = direct_messages.map do |direct_message|
@@ -535,6 +535,23 @@ def open(user)
end
end
+ desc "rate_limit", "Returns information related to Twitter API rate limiting."
+ method_option "csv", :aliases => "-c", :type => :boolean, :default => false, :desc => "Output in CSV format."
+ def rate_limit
+ rate_limit_status = client.rate_limit_status
+ if options['csv']
+ say ["Hourly limit", "Remaining hits", "Reset time"].to_csv
+ say [rate_limit_status.hourly_limit, rate_limit_status.remaining_hits, csv_formatted_time(rate_limit_status, :reset_time)].to_csv
+ else
+ array = []
+ array << ["Hourly limit", number_with_delimiter(rate_limit_status.hourly_limit)]
+ array << ["Remaining hits", number_with_delimiter(rate_limit_status.remaining_hits)]
+ array << ["Reset time", ls_formatted_time(rate_limit_status, :reset_time)]
+ print_table(array)
+ end
+ end
+ map %w(ratelimit rl) => :rate_limit
+
desc "reply STATUS_ID MESSAGE", "Post your Tweet as a reply directed at another person."
method_option "all", :aliases => "-a", :type => "boolean", :default => false, :desc => "Reply to all users mentioned in the Tweet."
method_option "location", :aliases => "-l", :type => :boolean, :default => false
@@ -645,7 +662,7 @@ def status(status_id)
end
if options['csv']
say ["ID", "Text", "Screen name", "Posted at", "Location", "Retweets", "Source", "URL"].to_csv
- say [status.id, HTMLEntities.new.decode(status.text), status.from_user, status.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"), location, status.retweet_count, strip_tags(status.source), "https://twitter.com/#{status.from_user}/status/#{status.id}"].to_csv
+ say [status.id, HTMLEntities.new.decode(status.text), status.from_user, csv_formatted_time(status), location, status.retweet_count, strip_tags(status.source), "https://twitter.com/#{status.from_user}/status/#{status.id}"].to_csv
else
array = []
array << ["ID", status.id.to_s]
@@ -821,12 +838,11 @@ def whois(user)
user = client.user(user)
if options['csv']
say ["ID", "Verified", "Name", "Screen name", "Bio", "Location", "Following", "Last update", "Lasted updated at", "Since", "Tweets", "Favorites", "Listed", "Following", "Followers", "URL"].to_csv
- say [user.id, user.verified?, user.name, user.screen_name, user.description, user.location, user.following?, HTMLEntities.new.decode(user.status.text), user.status.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"), user.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"), user.statuses_count, user.favourites_count, user.listed_count, user.friends_count, user.followers_count, user.url].to_csv
+ say [user.id, user.verified?, user.name, user.screen_name, user.description, user.location, user.following?, HTMLEntities.new.decode(user.status.text), csv_formatted_time(user.status), csv_formatted_time(user), user.statuses_count, user.favourites_count, user.listed_count, user.friends_count, user.followers_count, user.url].to_csv
else
array = []
- name_label = user.verified ? "Name (Verified)" : "Name"
array << ["ID", user.id.to_s]
- array << [name_label, user.name] unless user.name.nil?
+ array << [user.verified ? "Name (Verified)" : "Name", user.name] unless user.name.nil?
array << ["Bio", user.description.gsub(/\n+/, ' ')] unless user.description.nil?
array << ["Location", user.location] unless user.location.nil?
array << ["Status", user.following ? "Following" : "Not following"]
View
2 lib/t/list.rb
@@ -81,7 +81,7 @@ def information(list)
list = client.list(owner, list)
if options['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, list.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"), 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, "https://twitter.com#{list.uri}"].to_csv
else
array = []
array << ["ID", list.id.to_s]
View
2 lib/t/search.rb
@@ -39,7 +39,7 @@ def all(query)
if options['csv']
say STATUS_HEADINGS.to_csv unless statuses.empty?
statuses.each do |status|
- say [status.id, status.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"), status.from_user, HTMLEntities.new.decode(status.text)].to_csv
+ say [status.id, csv_formatted_time(status), status.from_user, HTMLEntities.new.decode(status.text)].to_csv
end
elsif options['long']
array = statuses.map do |status|
View
32 spec/cli_spec.rb
@@ -1911,6 +1911,38 @@
end
end
+ describe "#rate_limit" do
+ before do
+ stub_get("/1/account/rate_limit_status.json").
+ to_return(:body => fixture("rate_limit_status.json"), :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "should request the correct resource" do
+ @cli.rate_limit
+ a_get("/1/account/rate_limit_status.json").
+ should have_been_made
+ end
+ it "should have the correct output" do
+ @cli.rate_limit
+ $stdout.string.should == <<-eos
+Hourly limit 20,000
+Remaining hits 19,993
+Reset time Oct 25 2010
+ eos
+ end
+ context "--csv" do
+ before do
+ @cli.options = @cli.options.merge("csv" => true)
+ end
+ it "should have the correct output" do
+ @cli.rate_limit
+ $stdout.string.should == <<-eos
+Hourly limit,Remaining hits,Reset time
+20000,19993,2010-10-26 02:43:08 +0000
+ eos
+ end
+ end
+ end
+
describe "#reply" do
before do
@cli.options = @cli.options.merge("profile" => fixture_path + "/.trc", "location" => true)
View
1 spec/fixtures/rate_limit_status.json
@@ -0,0 +1 @@
+{"remaining_hits":19993,"hourly_limit":20000,"reset_time_in_seconds":1288060988,"reset_time":"Tue Oct 26 02:43:08 +0000 2010"}

0 comments on commit b177299

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