-
-
Notifications
You must be signed in to change notification settings - Fork 598
Gists API #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gists API #5
Changes from 14 commits
826fb3f
c53e1ba
8de6339
5245ad5
4baecb7
5acc889
690a969
1d3d647
58bc417
b8c1502
36701a5
2adb045
2dc6c87
a617177
4812621
49bb59e
870ab57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Github\Api; | ||
|
||
/** | ||
* Creating, editing, deleting and listing gists | ||
* | ||
* @link http://developer.github.com/v3/gists/ | ||
* @author Edoardo Rivello <edoardo.rivello at gmail dot com> | ||
* @license MIT License | ||
*/ | ||
class Gist extends Api | ||
{ | ||
/** | ||
* List gists by username | ||
* @link http://developer.github.com/v3/gists/ | ||
* | ||
* @param string $username the username | ||
* @return array list of gist found | ||
*/ | ||
public function getList($username) | ||
{ | ||
return $this->get('users/'.urlencode($username).'/gists'); | ||
} | ||
|
||
/** | ||
* Show a specific gist | ||
* @link http://developer.github.com/v3/gists/ | ||
* | ||
* @param string $id the gist id | ||
* @return array data from gist | ||
*/ | ||
public function getGist($id) | ||
{ | ||
return $this->get('/gists/'.urlencode($id)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove first |
||
} | ||
|
||
/** | ||
* Create a new gist. | ||
* @link http://developer.github.com/v3/gists/ | ||
* | ||
* @param string $description gist description | ||
* @param bool $public 1 for public, 0 for private | ||
* @param string $filename gist filename | ||
* @param string $content gist file contents | ||
* @return array returns gist data | ||
*/ | ||
public function create($filename, $content, $description = '', $public = false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be: |
||
{ | ||
$input = array( | ||
'description' => $description, | ||
'public' => $public, | ||
'files' => array( | ||
$filename => array( | ||
'content' => $content | ||
) | ||
) | ||
); | ||
|
||
return $this->post('gists', $input); | ||
} | ||
|
||
/** | ||
* Edit a gist | ||
* @link http://developer.github.com/v3/gists/ | ||
* | ||
* @param string $$id the gist id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicated |
||
* @param array $values the key => value pairs to post | ||
* @return array informations about the gist | ||
*/ | ||
public function update($id, $values) | ||
{ | ||
return $this->patch('gists/'.urlencode($id), $values); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be changed as |
||
} | ||
|
||
/** | ||
* Remove a gist by id | ||
* @link http://developer.github.com/v3/gists/ | ||
* | ||
* @param int $id the gist id | ||
* @return null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is wrong. |
||
*/ | ||
public function remove($id) | ||
{ | ||
return $this->delete('gists/'.urlencode($id)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Github\Tests\Api; | ||
|
||
use Github\Tests\ApiTestCase; | ||
|
||
class GistTest extends ApiTestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function shouldCreateGist() | ||
{ | ||
$api = $this->getApiMock(); | ||
|
||
$input = array( | ||
'description' => '', | ||
'public' => false, | ||
'files' => array( | ||
'filename.txt' => array( | ||
'content' => 'content' | ||
) | ||
) | ||
); | ||
|
||
$filename = 'filename.txt'; | ||
$content = 'content'; | ||
|
||
$api->expects($this->once()) | ||
->method('post') | ||
->with('gists', $input); | ||
|
||
$gist = $api->create($filename, $content); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldUpdateGist() | ||
{ | ||
$api = $this->getApiMock(); | ||
|
||
$input = array( | ||
'description' => '', | ||
'files' => array( | ||
'filename.txt' => array( | ||
'filename' => 'new_name.txt', | ||
'content' => 'content' | ||
), | ||
'filename_new.txt' => array( | ||
'content' => 'content new' | ||
) | ||
) | ||
); | ||
|
||
$api->expects($this->once()) | ||
->method('patch') | ||
->with('gists/5', $input); | ||
|
||
$gist = $api->update(5, $input); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldDeleteGist() | ||
{ | ||
$api = $this->getApiMock(); | ||
|
||
$api->expects($this->once()) | ||
->method('delete') | ||
->with('gists/5'); | ||
|
||
$api->remove(5); | ||
} | ||
|
||
protected function getApiClass() | ||
{ | ||
return 'Github\Api\Gist'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Github\Tests\Functional; | ||
|
||
use Github\Client; | ||
|
||
class GistTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function shouldRetrieveGistsListByUser() | ||
{ | ||
$username = 'KnpLabs'; | ||
|
||
$github = new Client(); | ||
$gists = $github->getGistApi()->getList($username); | ||
$gist = array_pop($gists); | ||
|
||
$this->assertArrayHasKey('url', $gist); | ||
$this->assertArrayHasKey('files', $gist); | ||
$this->assertArrayHasKey('comments', $gist); | ||
$this->assertArrayHasKey('created_at', $gist); | ||
$this->assertArrayHasKey('updated_at', $gist); | ||
$this->assertArrayHasKey('user', $gist); | ||
$this->assertEquals('KnpLabs', $gist['user']['login']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldRetrieveGistById() | ||
{ | ||
$id = 1; | ||
|
||
$github = new Client(); | ||
$gist = $github->getGistApi()->getGist($id); | ||
|
||
$this->assertArrayHasKey('url', $gist); | ||
$this->assertArrayHasKey('files', $gist); | ||
$this->assertArrayHasKey('comments', $gist); | ||
$this->assertArrayHasKey('created_at', $gist); | ||
$this->assertArrayHasKey('updated_at', $gist); | ||
$this->assertArrayHasKey('user', $gist); | ||
$this->assertArrayHasKey('gistfile1.txt', $gist['files']); | ||
$this->assertEquals('schacon', $gist['user']['login']); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add functionality for types listings (all, public, starred) for authenticated user ?