Skip to content

Commit

Permalink
Merge pull request #1 from kevinjalbert/ratelimit
Browse files Browse the repository at this point in the history
Improve ratelimit methods (specs and reuse old header).
  • Loading branch information
icco committed May 25, 2012
2 parents 6fb3586 + b7d3d43 commit 1e0395d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/octokit/request.rb
Expand Up @@ -22,13 +22,19 @@ def put(path, options={}, version=api_version, authenticate=true, raw=false, for
request(:put, path, options, version, authenticate, raw, force_urlencoded)
end

def ratelimit
headers = request(:head, "/", {}, api_version, true, true, false)
def ratelimit(force=false)
headers = @last_header
if @last_header == nil || force
headers = request(:head, "/", {}, api_version, true, true, false)
end
return headers["X-RateLimit-Limit"].to_i
end

def ratelimit_remaining
headers = request(:head, "/", {}, api_version, true, true, false)
def ratelimit_remaining(force=false)
headers = @last_header
if @last_header == nil || force
headers = request(:head, "/", {}, api_version, true, true, false)
end
return headers["X-RateLimit-Remaining"].to_i
end

Expand All @@ -53,6 +59,7 @@ def request(method, path, options, version, authenticate, raw, force_urlencoded)
end
end

@last_header = response.headers
if method == :head
response.headers
elsif raw
Expand Down
43 changes: 43 additions & 0 deletions spec/octokit/client_spec.rb
Expand Up @@ -43,4 +43,47 @@

end

describe "ratelimit" do

before(:each) do
stub_request(:get, "https://api.github.com/foo/bar").
to_return(:status => 200, :body => '', :headers =>
{ 'X-RateLimit-Limit' => 5000, 'X-RateLimit-Remaining' => 4999})
stub_request(:get, "https://api.github.com/foo/bar2").
to_return(:status => 200, :body => '', :headers =>
{ 'X-RateLimit-Limit' => 5000, 'X-RateLimit-Remaining' => 4998})
stub_request(:head, "https://api.github.com").
to_return(:status => 200, :body => '', :headers =>
{ 'X-RateLimit-Limit' => 5000, 'X-RateLimit-Remaining' => 4998})
@force = true
@client = Octokit::Client.new()
end

it "should get the ratelimit-limit from the header" do
@client.get("https://api.github.com/foo/bar")
@client.ratelimit.should == 5000
@client.ratelimit(true).should == 5000
end

it "should use the update old header when using new request ratelimit-remaining" do
@client.get("https://api.github.com/foo/bar")
@client.ratelimit_remaining.should == 4999
@client.get("https://api.github.com/foo/bar2")
@client.ratelimit_remaining.should == 4998
end

it "should use the new header when asking for ratelimit-remaining(force)" do
@client.get("https://api.github.com/foo/bar")
@client.ratelimit_remaining.should == 4999
@client.ratelimit_remaining(true).should == 4998
end

it "should use the last header when asking for ratelimit-remaining" do
@client.get("https://api.github.com/foo/bar")
@client.ratelimit_remaining.should == 4999
@client.ratelimit_remaining.should == 4999
end

end

end

0 comments on commit 1e0395d

Please sign in to comment.