Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please separate return statement with 1 blank line

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cursedcoder

You should use symfony preset on your StyleCI config file or at least, add the return fixer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #323.

}

/**
* 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);
}
}