Skip to content

Commit

Permalink
Adding support for the new rate_limit_status method.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmorrison committed Jun 29, 2008
1 parent 27745af commit 2b5325b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
require 'twitter/base'
require 'twitter/user'
require 'twitter/status'
require 'twitter/direct_message'
require 'twitter/direct_message'
require 'twitter/rate_limit_status'
4 changes: 4 additions & 0 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def destroy(id)
call("destroy/#{id}")
end

def rate_limit_status
RateLimitStatus.new_from_xml request("account/rate_limit_status.xml", :auth => true)
end

# waiting for twitter to correctly implement this in the api as it is documented
def featured
users(call(:featured))
Expand Down
19 changes: 19 additions & 0 deletions lib/twitter/rate_limit_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Twitter
class RateLimitStatus
include EasyClassMaker

attributes :reset_time_in_seconds, :reset_time, :remaining_hits, :hourly_limit

class << self
# Creates a new rate limi status from a piece of xml
def new_from_xml(xml)
RateLimitStatus.new do |s|
s.reset_time_in_seconds = xml.at('reset-time-in-seconds').inner_html.to_i
s.reset_time = Time.parse xml.at('reset-time').inner_html
s.remaining_hits = xml.at('remaining-hits').inner_html.to_i
s.hourly_limit = xml.at('hourly-limit').inner_html.to_i
end
end
end
end
end
28 changes: 28 additions & 0 deletions spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,32 @@
@base.should_receive(:request).and_return(Hpricot::XML(data))
@base.user('4243').name.should == 'John Nunemaker'
end

describe "rate limit status" do
before do
@data = open(File.dirname(__FILE__) + '/fixtures/rate_limit_status.xml').read
@base.stub!(:request).and_return(Hpricot::XML(@data))
end

it "should request the status" do
@base.should_receive(:request).and_return(Hpricot::XML(@data))
@base.rate_limit_status
end

it "should have an hourly limit" do
@base.rate_limit_status.hourly_limit.should == 20
end

it "should have a reset time in seconds" do
@base.rate_limit_status.reset_time_in_seconds.should == 1214757610
end

it "should have a reset time" do
@base.rate_limit_status.reset_time.should == Time.parse('2008-06-29T16:40:10+00:00')
end

it "should have remaining hits" do
@base.rate_limit_status.remaining_hits.should == 5
end
end
end
7 changes: 7 additions & 0 deletions spec/fixtures/rate_limit_status.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<hourly-limit type="integer">20</hourly-limit>
<reset-time-in-seconds type="integer">1214757610</reset-time-in-seconds>
<reset-time type="datetime">2008-06-29T16:40:10+00:00</reset-time>
<remaining-hits type="integer">5</remaining-hits>
</hash>

0 comments on commit 2b5325b

Please sign in to comment.