Skip to content

Commit

Permalink
Update RateLimit class for API v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Sep 7, 2012
1 parent dfc5641 commit 540cbb2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 46 deletions.
17 changes: 4 additions & 13 deletions lib/twitter/rate_limit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,27 @@ def initialize(attrs={})
@attrs = attrs
end

# @return [String]
def class
@attrs.values_at('x-ratelimit-class', 'X-RateLimit-Class').compact.first
end

# @return [Integer]
def limit
limit = @attrs.values_at('x-ratelimit-limit', 'X-RateLimit-Limit').compact.first
limit = @attrs['x-rate-limit-limit']
limit.to_i if limit
end

# @return [Integer]
def remaining
remaining = @attrs.values_at('x-ratelimit-remaining', 'X-RateLimit-Remaining').compact.first
remaining = @attrs['x-rate-limit-remaining']
remaining.to_i if remaining
end

# @return [Time]
def reset_at
reset = @attrs.values_at('x-ratelimit-reset', 'X-RateLimit-Reset').compact.first
reset = @attrs['x-rate-limit-reset']
Time.at(reset.to_i) if reset
end

# @return [Integer]
def reset_in
if retry_after = @attrs.values_at('retry-after', 'Retry-After').compact.first
retry_after.to_i
elsif reset_at
[(reset_at - Time.now).ceil, 0].max
end
[(reset_at - Time.now).ceil, 0].max if reset_at
end
alias retry_after reset_in

Expand Down
45 changes: 12 additions & 33 deletions spec/twitter/rate_limit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,36 @@
end

describe "#limit" do
it "returns an Integer when X-RateLimit-Limit header is set" do
rate_limit = Twitter::RateLimit.new('X-RateLimit-Limit' => "150")
it "returns an Integer when x-rate-limit-limit header is set" do
rate_limit = Twitter::RateLimit.new('x-rate-limit-limit' => "150")
rate_limit.limit.should be_an Integer
rate_limit.limit.should eq 150
end
it "returns nil when X-RateLimit-Limit header is not set" do
it "returns nil when x-rate-limit-limit header is not set" do
rate_limit = Twitter::RateLimit.new
rate_limit.limit.should be_nil
end
end

describe "#class" do
it "returns a String when X-RateLimit-Class header is set" do
rate_limit = Twitter::RateLimit.new('X-RateLimit-Class' => "api")
rate_limit.class.should be_an String
rate_limit.class.should eq "api"
end
it "returns nil when X-RateLimit-Class header is not set" do
rate_limit = Twitter::RateLimit.new
rate_limit.class.should be_nil
end
end

describe "#remaining" do
it "returns an Integer when X-RateLimit-Remaining header is set" do
rate_limit = Twitter::RateLimit.new('X-RateLimit-Remaining' => "149")
it "returns an Integer when x-rate-limit-remaining header is set" do
rate_limit = Twitter::RateLimit.new('x-rate-limit-remaining' => "149")
rate_limit.remaining.should be_an Integer
rate_limit.remaining.should eq 149
end
it "returns nil when X-RateLimit-Remaining header is not set" do
it "returns nil when x-rate-limit-remaining header is not set" do
rate_limit = Twitter::RateLimit.new
rate_limit.remaining.should be_nil
end
end

describe "#reset_at" do
it "returns a Time when X-RateLimit-Reset header is set" do
rate_limit = Twitter::RateLimit.new('X-RateLimit-Reset' => "1339019097")
it "returns a Time when x-rate-limit-reset header is set" do
rate_limit = Twitter::RateLimit.new('x-rate-limit-reset' => "1339019097")
rate_limit.reset_at.should be_a Time
rate_limit.reset_at.should eq Time.at(1339019097)
end
it "returns nil when X-RateLimit-Reset header is not set" do
it "returns nil when x-rate-limit-reset header is not set" do
rate_limit = Twitter::RateLimit.new
rate_limit.reset_at.should be_nil
end
Expand All @@ -64,24 +52,15 @@
after do
Timecop.return
end
it "returns an Integer when X-RateLimit-Reset header is set" do
rate_limit = Twitter::RateLimit.new('X-RateLimit-Reset' => "1339019097")
it "returns an Integer when x-rate-limit-reset header is set" do
rate_limit = Twitter::RateLimit.new('x-rate-limit-reset' => "1339019097")
rate_limit.reset_in.should be_an Integer
rate_limit.reset_in.should eq 15777
end
it "returns nil when X-RateLimit-Reset header is not set" do
it "returns nil when x-rate-limit-reset header is not set" do
rate_limit = Twitter::RateLimit.new
rate_limit.reset_in.should be_nil
end
it "returns an Integer when Retry-After header is set" do
rate_limit = Twitter::RateLimit.new('Retry-After' => "1339019097")
rate_limit.retry_after.should be_an Integer
rate_limit.retry_after.should eq 1339019097
end
it "returns nil when Retry-After header is not set" do
rate_limit = Twitter::RateLimit.new
rate_limit.retry_after.should be_nil
end
end

end

0 comments on commit 540cbb2

Please sign in to comment.