Skip to content

Commit

Permalink
Merge pull request #314 from quickliketurtle/add-rate-limit-endpoint
Browse files Browse the repository at this point in the history
Add rate limit endpoint
  • Loading branch information
cursedcoder committed Oct 11, 2015
2 parents 7b50381 + eca9e8c commit 832b7be
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ APIs:
* [Teams](organization/teams.md)
* [Pull Requests](pull_requests.md)
* [Comments](pull_request/comments.md)
* [Rate Limits](rate_limits.md)
* [Repositories](repos.md)
* [Contents](repo/contents.md)
* [Releases](repo/releases.md)
Expand Down
23 changes: 23 additions & 0 deletions doc/rate_limits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Rate Limit API
[Back to the navigation](README.md)

Get Rate Limit
Wraps [GitHub Rate Limit API](http://developer.github.com/v3/rate_limit/).

#### Get All Rate Limits.

```php
$rateLimits = $github->api('rate_limit')->getRateLimits();
```

#### Get Core Rate Limit

```php
$coreLimit = $github->api('rate_limit')->getCoreLimit();
```

#### Get Search Rate Limit

```php
$searchLimit = $github->api('rate_limit)->getSearchLimit');
```
46 changes: 46 additions & 0 deletions lib/Github/Api/RateLimit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Github\Api;

/**
* Get rate limits
*
* @link https://developer.github.com/v3/rate_limit/
* @author Jeff Finley <[email protected]>
*/
class RateLimit extends AbstractApi
{
/**
* Get rate limits
*
* @return array
*/
public function getRateLimits()
{
return $this->get('rate_limit');
}

/**
* Get core rate limit
*
* @return integer
*/
public function getCoreLimit()
{
$response = $this->getRateLimits();

return $response['resources']['core']['limit'];
}

/**
* Get search rate limit
*
* @return integer
*/
public function getSearchLimit()
{
$response = $this->getRateLimits();

return $response['resources']['search']['limit'];
}
}
6 changes: 6 additions & 0 deletions lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @method Api\PullRequest pr()
* @method Api\PullRequest pullRequest()
* @method Api\PullRequest pullRequests()
* @method Api\RateLimit ratelimit()
* @method Api\Repo repo()
* @method Api\Repo repos()
* @method Api\Repo repository()
Expand Down Expand Up @@ -168,6 +169,11 @@ public function api($name)
$api = new Api\PullRequest($this);
break;

case 'rateLimit':
case 'rate_limit':
$api = new Api\RateLimit($this);
break;

case 'repo':
case 'repos':
case 'repository':
Expand Down
40 changes: 40 additions & 0 deletions test/Github/Tests/Api/RateLimitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Github\Tests\Api;

class RateLimitTest extends TestCase
{
/**
* @test
*/
public function shouldReturnRateLimitArray()
{
$expectedArray = array(
'resources' => array(
'core' => array(
'limit' => 5000,
'remaining' => 4999,
'reset' => 1372700873
),
'search' => array(
'limit' => 30,
'remaining' => 18,
'reset' => 1372697452
)
)
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('rate_limit')
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->getRateLimits());
}

protected function getApiClass()
{
return 'Github\Api\RateLimit';
}
}
20 changes: 20 additions & 0 deletions test/Github/Tests/Functional/RateLimitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Github\Tests\Functional;

/**
* @group functional
*/
class RateLimitTest extends TestCase
{
/**
* @test
*/
public function shouldRetrievedRateLimits()
{
$response = $this->client->api('rate_limit')->getRateLimits();

$this->assertArrayHasKey('resources', $response);
$this->assertArraySubset(array('resources' => array('core' => array('limit' => 60))), $response);
}
}

0 comments on commit 832b7be

Please sign in to comment.