diff --git a/doc/README.md b/doc/README.md index 890ac85e7f2..a1cb1291ef6 100644 --- a/doc/README.md +++ b/doc/README.md @@ -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) diff --git a/doc/rate_limits.md b/doc/rate_limits.md new file mode 100644 index 00000000000..123e403abf8 --- /dev/null +++ b/doc/rate_limits.md @@ -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'); +``` diff --git a/lib/Github/Api/RateLimit.php b/lib/Github/Api/RateLimit.php new file mode 100644 index 00000000000..a07eb781a6e --- /dev/null +++ b/lib/Github/Api/RateLimit.php @@ -0,0 +1,46 @@ + + */ +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']; + } +} diff --git a/lib/Github/Client.php b/lib/Github/Client.php index fd3be2e088e..c1a17169dc6 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -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() @@ -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': diff --git a/test/Github/Tests/Api/RateLimitTest.php b/test/Github/Tests/Api/RateLimitTest.php new file mode 100644 index 00000000000..4f1b8c467d3 --- /dev/null +++ b/test/Github/Tests/Api/RateLimitTest.php @@ -0,0 +1,40 @@ + 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'; + } +} diff --git a/test/Github/Tests/Functional/RateLimitTest.php b/test/Github/Tests/Functional/RateLimitTest.php new file mode 100644 index 00000000000..b0b63704d63 --- /dev/null +++ b/test/Github/Tests/Functional/RateLimitTest.php @@ -0,0 +1,20 @@ +client->api('rate_limit')->getRateLimits(); + + $this->assertArrayHasKey('resources', $response); + $this->assertArraySubset(array('resources' => array('core' => array('limit' => 60))), $response); + } +}