From 9ee0672fdcf4bfe16c152ddad2b59a685c6f890b Mon Sep 17 00:00:00 2001 From: Jeff Finley Date: Sat, 26 Sep 2015 11:50:40 -0700 Subject: [PATCH 1/8] Added rate limit endpoint and tests --- lib/Github/Api/RateLimit.php | 46 +++++++++++++++++++ lib/Github/Client.php | 6 +++ test/Github/Tests/Api/RateLimitTest.php | 41 +++++++++++++++++ .../Github/Tests/Functional/RateLimitTest.php | 22 +++++++++ 4 files changed, 115 insertions(+) create mode 100644 lib/Github/Api/RateLimit.php create mode 100644 test/Github/Tests/Api/RateLimitTest.php create mode 100644 test/Github/Tests/Functional/RateLimitTest.php diff --git a/lib/Github/Api/RateLimit.php b/lib/Github/Api/RateLimit.php new file mode 100644 index 00000000000..13f3a156725 --- /dev/null +++ b/lib/Github/Api/RateLimit.php @@ -0,0 +1,46 @@ + + */ +class RateLimit extends AbstractApi +{ + + /** + * Get rate limits + * + * @return \Guzzle\Http\EntityBodyInterface|mixed|string + */ + 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..f3b2b4dba5b --- /dev/null +++ b/test/Github/Tests/Api/RateLimitTest.php @@ -0,0 +1,41 @@ + [ + 'core' => [ + 'limit' => 5000, + 'remaining' => 4999, + 'reset' => 1372700873 + ], + 'search' => [ + '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'; + } +} \ No newline at end of file diff --git a/test/Github/Tests/Functional/RateLimitTest.php b/test/Github/Tests/Functional/RateLimitTest.php new file mode 100644 index 00000000000..3447db3ff8d --- /dev/null +++ b/test/Github/Tests/Functional/RateLimitTest.php @@ -0,0 +1,22 @@ +client->api('rate_limit')->getRateLimits(); + + $this->assertArrayHasKey('resources', $response); + $this->assertArraySubset(['resources' => ['core' => ['limit' => 60]]], $response); + + } + +} From cf0a6d4aa84118bc847fd2a1f8d0b94594b6f6c4 Mon Sep 17 00:00:00 2001 From: Jeff Finley Date: Sat, 26 Sep 2015 12:03:14 -0700 Subject: [PATCH 2/8] added documentation --- doc/rate_limits.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 doc/rate_limits.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'); +``` From 8733443f02d3375340eb14cb3f22cb344c4e9492 Mon Sep 17 00:00:00 2001 From: Jeff Finley Date: Sat, 26 Sep 2015 12:10:37 -0700 Subject: [PATCH 3/8] style updates from styleci check --- lib/Github/Api/RateLimit.php | 2 -- test/Github/Tests/Api/RateLimitTest.php | 16 ++++++++-------- test/Github/Tests/Functional/RateLimitTest.php | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/Github/Api/RateLimit.php b/lib/Github/Api/RateLimit.php index 13f3a156725..e66724788ca 100644 --- a/lib/Github/Api/RateLimit.php +++ b/lib/Github/Api/RateLimit.php @@ -10,7 +10,6 @@ */ class RateLimit extends AbstractApi { - /** * Get rate limits * @@ -42,5 +41,4 @@ public function getSearchLimit() $response = $this->getRateLimits(); return $response['resources']['search']['limit']; } - } diff --git a/test/Github/Tests/Api/RateLimitTest.php b/test/Github/Tests/Api/RateLimitTest.php index f3b2b4dba5b..d5632d35c65 100644 --- a/test/Github/Tests/Api/RateLimitTest.php +++ b/test/Github/Tests/Api/RateLimitTest.php @@ -10,20 +10,20 @@ class RateLimitTest extends TestCase */ public function shouldReturnRateLimitArray() { - $expectedArray = [ - 'resources' => [ - 'core' => [ + $expectedArray = array( + 'resources' => array( + 'core' => array( 'limit' => 5000, 'remaining' => 4999, 'reset' => 1372700873 - ], - 'search' => [ + ), + 'search' => array( 'limit' => 30, 'remaining' => 18, 'reset' => 1372697452 - ] - ] - ]; + ) + ) + ); $api = $this->getApiMock(); $api->expects($this->once()) diff --git a/test/Github/Tests/Functional/RateLimitTest.php b/test/Github/Tests/Functional/RateLimitTest.php index 3447db3ff8d..6bc0eb2f053 100644 --- a/test/Github/Tests/Functional/RateLimitTest.php +++ b/test/Github/Tests/Functional/RateLimitTest.php @@ -15,7 +15,7 @@ public function shouldRetrievedRateLimits() $response = $this->client->api('rate_limit')->getRateLimits(); $this->assertArrayHasKey('resources', $response); - $this->assertArraySubset(['resources' => ['core' => ['limit' => 60]]], $response); + $this->assertArraySubset(array('resources' => array('core' => array('limit' => 60))), $response); } From 177e5f9bc09de18939a36e9121f54209eb2ffcaf Mon Sep 17 00:00:00 2001 From: Jeff Finley Date: Sat, 26 Sep 2015 12:10:37 -0700 Subject: [PATCH 4/8] Style updates from Styleci check --- lib/Github/Api/RateLimit.php | 2 -- test/Github/Tests/Api/RateLimitTest.php | 18 +++++++++--------- test/Github/Tests/Functional/RateLimitTest.php | 4 +--- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/Github/Api/RateLimit.php b/lib/Github/Api/RateLimit.php index 13f3a156725..e66724788ca 100644 --- a/lib/Github/Api/RateLimit.php +++ b/lib/Github/Api/RateLimit.php @@ -10,7 +10,6 @@ */ class RateLimit extends AbstractApi { - /** * Get rate limits * @@ -42,5 +41,4 @@ public function getSearchLimit() $response = $this->getRateLimits(); return $response['resources']['search']['limit']; } - } diff --git a/test/Github/Tests/Api/RateLimitTest.php b/test/Github/Tests/Api/RateLimitTest.php index f3b2b4dba5b..bd917c651b6 100644 --- a/test/Github/Tests/Api/RateLimitTest.php +++ b/test/Github/Tests/Api/RateLimitTest.php @@ -4,26 +4,25 @@ class RateLimitTest extends TestCase { - /** * @test */ public function shouldReturnRateLimitArray() { - $expectedArray = [ - 'resources' => [ - 'core' => [ + $expectedArray = array( + 'resources' => array( + 'core' => array( 'limit' => 5000, 'remaining' => 4999, 'reset' => 1372700873 - ], - 'search' => [ + ), + 'search' => array( 'limit' => 30, 'remaining' => 18, 'reset' => 1372697452 - ] - ] - ]; + ) + ) + ); $api = $this->getApiMock(); $api->expects($this->once()) @@ -38,4 +37,5 @@ protected function getApiClass() { return 'Github\Api\RateLimit'; } + } \ No newline at end of file diff --git a/test/Github/Tests/Functional/RateLimitTest.php b/test/Github/Tests/Functional/RateLimitTest.php index 3447db3ff8d..b0b63704d63 100644 --- a/test/Github/Tests/Functional/RateLimitTest.php +++ b/test/Github/Tests/Functional/RateLimitTest.php @@ -15,8 +15,6 @@ public function shouldRetrievedRateLimits() $response = $this->client->api('rate_limit')->getRateLimits(); $this->assertArrayHasKey('resources', $response); - $this->assertArraySubset(['resources' => ['core' => ['limit' => 60]]], $response); - + $this->assertArraySubset(array('resources' => array('core' => array('limit' => 60))), $response); } - } From fd76633e96dd8a150dc87a9683a67ad22f4c902f Mon Sep 17 00:00:00 2001 From: Jeff Finley Date: Sat, 26 Sep 2015 12:27:26 -0700 Subject: [PATCH 5/8] updated docblock --- lib/Github/Api/RateLimit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Github/Api/RateLimit.php b/lib/Github/Api/RateLimit.php index e66724788ca..08ff9256701 100644 --- a/lib/Github/Api/RateLimit.php +++ b/lib/Github/Api/RateLimit.php @@ -13,7 +13,7 @@ class RateLimit extends AbstractApi /** * Get rate limits * - * @return \Guzzle\Http\EntityBodyInterface|mixed|string + * @return array */ public function getRateLimits() { From a07bce5ff160febaeed043bd551bb15e1f280fde Mon Sep 17 00:00:00 2001 From: Jeff Finley Date: Sat, 26 Sep 2015 12:29:15 -0700 Subject: [PATCH 6/8] remove extra line --- test/Github/Tests/Api/RateLimitTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/Github/Tests/Api/RateLimitTest.php b/test/Github/Tests/Api/RateLimitTest.php index bd917c651b6..4f1b8c467d3 100644 --- a/test/Github/Tests/Api/RateLimitTest.php +++ b/test/Github/Tests/Api/RateLimitTest.php @@ -37,5 +37,4 @@ protected function getApiClass() { return 'Github\Api\RateLimit'; } - -} \ No newline at end of file +} From 8775cb21c91aef2ffe350682c216c8e69c54321a Mon Sep 17 00:00:00 2001 From: Jeff Finley Date: Wed, 7 Oct 2015 08:07:32 -0700 Subject: [PATCH 7/8] Added link to rate_limits.md --- doc/README.md | 1 + 1 file changed, 1 insertion(+) 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) From eca9e8c3481972ba3b992f732cccd4e56c94d260 Mon Sep 17 00:00:00 2001 From: Jeff Finley Date: Thu, 8 Oct 2015 13:48:18 -0700 Subject: [PATCH 8/8] Add blank line before return statements. --- lib/Github/Api/RateLimit.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/Github/Api/RateLimit.php b/lib/Github/Api/RateLimit.php index 08ff9256701..a07eb781a6e 100644 --- a/lib/Github/Api/RateLimit.php +++ b/lib/Github/Api/RateLimit.php @@ -28,6 +28,7 @@ public function getRateLimits() public function getCoreLimit() { $response = $this->getRateLimits(); + return $response['resources']['core']['limit']; } @@ -39,6 +40,7 @@ public function getCoreLimit() public function getSearchLimit() { $response = $this->getRateLimits(); + return $response['resources']['search']['limit']; } }