-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from j0k3r/hn-api
HH api
- Loading branch information
Showing
16 changed files
with
202 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Api43\FeedBundle\Extractor; | ||
|
||
use Guzzle\Http\Client; | ||
use Guzzle\Http\Exception\RequestException; | ||
|
||
class HackerNews extends AbstractExtractor | ||
{ | ||
protected $guzzle; | ||
protected $text = null; | ||
|
||
/** | ||
* @param Client $guzzle | ||
*/ | ||
public function __construct(Client $guzzle) | ||
{ | ||
$this->guzzle = $guzzle; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function match($url) | ||
{ | ||
$host = parse_url($url, PHP_URL_HOST); | ||
$query = parse_url($url, PHP_URL_QUERY); | ||
|
||
if (false === $host || false === $query) { | ||
return false; | ||
} | ||
|
||
if (0 !== strpos($host, 'news.ycombinator.com')) { | ||
return false; | ||
} | ||
|
||
// match HN id | ||
preg_match('/id\=([0-9]+)/i', $query, $matches); | ||
|
||
if (!isset($matches[1])) { | ||
return false; | ||
} | ||
|
||
try { | ||
$data = $this->guzzle | ||
->get('https://hacker-news.firebaseio.com/v0/item/'.$matches[1].'.json') | ||
->send() | ||
->json(); | ||
} catch (RequestException $e) { | ||
return false; | ||
} | ||
|
||
if (in_array($data['type'], array('comment', 'pollopt')) | ||
|| trim($data['text']) === '') { | ||
return false; | ||
} | ||
|
||
$this->text = $data['text']; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getContent() | ||
{ | ||
if (!$this->text) { | ||
return ''; | ||
} | ||
|
||
return '<p>'.$this->text.'</p>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Api43\FeedBundle\Tests\Extractor; | ||
|
||
use Api43\FeedBundle\Extractor\HackerNews; | ||
use Guzzle\Http\Exception\RequestException; | ||
|
||
class HackerNewsTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function dataMatch() | ||
{ | ||
return array( | ||
array('https://news.ycombinator.com/item?id=10074364', true, array('text' => 'toto', 'type' => 'story')), | ||
array('http://news.ycombinator.com/item?id=10074364', true, array('text' => 'toto', 'type' => 'job')), | ||
// comment | ||
array('http://news.ycombinator.com/item?id=10077812', false, array('text' => 'toto', 'type' => 'comment')), | ||
// pollopt | ||
array('http://news.ycombinator.com/item?id=160705', false, array('text' => 'toto', 'type' => 'pollopt')), | ||
array('https://goog.co', false), | ||
array('http://news.ycombinator.com/item?id=rtyui', false), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider dataMatch | ||
*/ | ||
public function testMatch($url, $expected, $valueReturned = null) | ||
{ | ||
$guzzle = $this->getMockBuilder('Guzzle\Http\Client') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$request = $this->getMockBuilder('Guzzle\Http\Message\Request') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$response = $this->getMockBuilder('Guzzle\Http\Message\Response') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$guzzle->expects($this->any()) | ||
->method('get') | ||
->will($this->returnValue($request)); | ||
|
||
$request->expects($this->any()) | ||
->method('send') | ||
->will($this->returnValue($response)); | ||
|
||
$response->expects($this->any()) | ||
->method('json') | ||
->will($this->returnValue($valueReturned)); | ||
|
||
$hn = new HackerNews($guzzle); | ||
$this->assertEquals($expected, $hn->match($url)); | ||
} | ||
|
||
public function testMatchGuzzleFail() | ||
{ | ||
$guzzle = $this->getMockBuilder('Guzzle\Http\Client') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$request = $this->getMockBuilder('Guzzle\Http\Message\Request') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$response = $this->getMockBuilder('Guzzle\Http\Message\Response') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$guzzle->expects($this->any()) | ||
->method('get') | ||
->will($this->returnValue($request)); | ||
|
||
$request->expects($this->any()) | ||
->method('send') | ||
->will($this->returnValue($response)); | ||
|
||
$response->expects($this->any()) | ||
->method('json') | ||
->will($this->throwException(new RequestException())); | ||
|
||
$hn = new HackerNews($guzzle); | ||
$this->assertEquals(false, $hn->match('http://news.ycombinator.com/item?id=10074364')); | ||
} | ||
|
||
public function testContent() | ||
{ | ||
$guzzle = $this->getMockBuilder('Guzzle\Http\Client') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$request = $this->getMockBuilder('Guzzle\Http\Message\Request') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$response = $this->getMockBuilder('Guzzle\Http\Message\Response') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$guzzle->expects($this->any()) | ||
->method('get') | ||
->will($this->returnValue($request)); | ||
|
||
$request->expects($this->any()) | ||
->method('send') | ||
->will($this->returnValue($response)); | ||
|
||
$response->expects($this->any()) | ||
->method('json') | ||
->will($this->returnValue(array('text' => 'toto', 'type' => 'story'))); | ||
|
||
$hn = new HackerNews($guzzle); | ||
|
||
// first test fail because we didn't match an url, so HackerNewsId isn't defined | ||
$this->assertEmpty($hn->getContent()); | ||
|
||
$hn->match('http://news.ycombinator.com/item?id=10074364'); | ||
$this->assertEquals('<p>toto</p>', $hn->getContent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.