Skip to content

Commit

Permalink
Merge pull request #44 from j0k3r/hn-api
Browse files Browse the repository at this point in the history
HH api
  • Loading branch information
j0k3r committed Aug 19, 2015
2 parents 3d60216 + 389a8e0 commit d3f1509
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 212 deletions.
74 changes: 74 additions & 0 deletions src/FeedBundle/Extractor/HackerNews.php
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>';
}
}
7 changes: 7 additions & 0 deletions src/FeedBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ services:
tags:
- { name: feed.extractor, alias: spotify }

feed.extractor.hackernews:
class: Api43\FeedBundle\Extractor\HackerNews
arguments:
- @guzzle.client
tags:
- { name: feed.extractor, alias: hackernews }

# improver
feed.improver.chain:
class: Api43\FeedBundle\Improver\ImproverChain
Expand Down
16 changes: 0 additions & 16 deletions src/FeedBundle/Tests/Extractor/CamplusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,6 @@ public function testMatch($url, $expected)
->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));

$camplus = new Camplus($guzzle);
$this->assertEquals($expected, $camplus->match($url));
}
Expand Down
16 changes: 0 additions & 16 deletions src/FeedBundle/Tests/Extractor/DailymotionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,6 @@ public function testMatch($url, $expected)
->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));

$dailymotion = new Dailymotion($guzzle);
$this->assertEquals($expected, $dailymotion->match($url));
}
Expand Down
16 changes: 0 additions & 16 deletions src/FeedBundle/Tests/Extractor/DeviantartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,6 @@ public function testMatch($url, $expected)
->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));

$deviantart = new Deviantart($guzzle);
$this->assertEquals($expected, $deviantart->match($url));
}
Expand Down
20 changes: 0 additions & 20 deletions src/FeedBundle/Tests/Extractor/FlickrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,6 @@ public function testMatch($url, $expected)
->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('getHeader')
->will($this->returnValue('test'));

$flickr = new Flickr($guzzle, 'apikey');
$this->assertEquals($expected, $flickr->match($url));
}
Expand Down
16 changes: 0 additions & 16 deletions src/FeedBundle/Tests/Extractor/GfycatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,6 @@ public function testMatch($url, $expected)
->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));

$gfycat = new Gfycat($guzzle);
$this->assertEquals($expected, $gfycat->match($url));
}
Expand Down
16 changes: 0 additions & 16 deletions src/FeedBundle/Tests/Extractor/GithubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,6 @@ public function testMatch($url, $expected)
->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));

$github = new Github($guzzle);
$this->assertEquals($expected, $github->match($url));
}
Expand Down
121 changes: 121 additions & 0 deletions src/FeedBundle/Tests/Extractor/HackerNewsTest.php
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());
}
}
16 changes: 0 additions & 16 deletions src/FeedBundle/Tests/Extractor/InstagramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,6 @@ public function testMatch($url, $expected)
->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));

$instagram = new Instagram($guzzle);
$this->assertEquals($expected, $instagram->match($url));
}
Expand Down
16 changes: 0 additions & 16 deletions src/FeedBundle/Tests/Extractor/SoundcloudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,6 @@ public function testMatch($url, $expected)
->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));

$soundCloud = new Soundcloud($guzzle);
$this->assertEquals($expected, $soundCloud->match($url));
}
Expand Down
Loading

0 comments on commit d3f1509

Please sign in to comment.