-
-
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 #100 from j0k3r/extractor-giphy
Add Giphy extractor
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 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,65 @@ | ||
<?php | ||
|
||
namespace Api43\FeedBundle\Extractor; | ||
|
||
use GuzzleHttp\Exception\RequestException; | ||
|
||
class Giphy extends AbstractExtractor | ||
{ | ||
protected $giphyUrl = null; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function match($url) | ||
{ | ||
$host = parse_url($url, PHP_URL_HOST); | ||
$path = parse_url($url, PHP_URL_PATH); | ||
|
||
if (false === $host || false === $path) { | ||
return false; | ||
} | ||
|
||
if (!in_array($host, array('www.giphy.com', 'giphy.com'))) { | ||
return false; | ||
} | ||
|
||
preg_match('/\/gifs\//', $path, $matches); | ||
|
||
if (!isset($matches[0])) { | ||
return false; | ||
} | ||
|
||
$this->giphyUrl = $url; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getContent() | ||
{ | ||
if (!$this->giphyUrl) { | ||
return ''; | ||
} | ||
|
||
try { | ||
$data = $this->client | ||
->get('http://giphy.com/services/oembed/?url='.$this->giphyUrl) | ||
->json(); | ||
} catch (RequestException $e) { | ||
$this->logger->warning('Giphy extract failed for: '.$this->giphyUrl, [ | ||
'exception' => $e, | ||
]); | ||
|
||
return ''; | ||
} | ||
|
||
if (!is_array($data) || empty($data)) { | ||
return ''; | ||
} | ||
|
||
return '<div><h2>'.$data['title'].'</h2><p><img src="'.$data['image'].'"></p></div>'; | ||
} | ||
} |
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,70 @@ | ||
<?php | ||
|
||
namespace Tests\FeedBundle\Extractor; | ||
|
||
use Api43\FeedBundle\Extractor\Giphy; | ||
use Monolog\Logger; | ||
use Monolog\Handler\TestHandler; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Subscriber\Mock; | ||
use GuzzleHttp\Message\Response; | ||
use GuzzleHttp\Stream\Stream; | ||
|
||
class GiphyTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function dataMatch() | ||
{ | ||
return array( | ||
array('http://giphy.com/gifs/linarf-l2SpOiTglzlu7yI3S', true), | ||
array('http://www.giphy.com/gifs/linarf-l2SpOiTglzlu7yI3S', true), | ||
array('https://giphy.com/gifs/linarf-l2SpOiTglzlu7yI3S', true), | ||
array('https://giphy.com/gifs/mlb-baseball-nlds-l2Sq2Ri3w1rmrOTHq', true), | ||
array('https://giphy.com/search/hello-kitty-stickers/', false), | ||
array('https://goog.co', false), | ||
array('http://user@:80', false), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider dataMatch | ||
*/ | ||
public function testMatch($url, $expected) | ||
{ | ||
$giphy = new Giphy(); | ||
$this->assertEquals($expected, $giphy->match($url)); | ||
} | ||
|
||
public function testContent() | ||
{ | ||
$client = new Client(); | ||
|
||
$mock = new Mock([ | ||
new Response(200, [], Stream::factory(json_encode(array('title' => 'my title', 'image' => 'http://0.0.0.0/img.jpg')))), | ||
new Response(200, [], Stream::factory('')), | ||
new Response(400, [], Stream::factory('oops')), | ||
]); | ||
|
||
$client->getEmitter()->attach($mock); | ||
|
||
$giphy = new Giphy(); | ||
$giphy->setClient($client); | ||
|
||
$logHandler = new TestHandler(); | ||
$logger = new Logger('test', array($logHandler)); | ||
$giphy->setLogger($logger); | ||
|
||
// first test fail because we didn't match an url, so GiphyUrl isn't defined | ||
$this->assertEmpty($giphy->getContent()); | ||
|
||
$giphy->match('https://giphy.com/gifs/linarf-l2SpOiTglzlu7yI3S'); | ||
|
||
// consecutive calls | ||
$this->assertEquals('<div><h2>my title</h2><p><img src="http://0.0.0.0/img.jpg"></p></div>', $giphy->getContent()); | ||
// this one will got an empty array | ||
$this->assertEmpty($giphy->getContent()); | ||
// this one will catch an exception | ||
$this->assertEmpty($giphy->getContent()); | ||
|
||
$this->assertTrue($logHandler->hasWarning('Giphy extract failed for: https://giphy.com/gifs/linarf-l2SpOiTglzlu7yI3S'), 'Warning message matched'); | ||
} | ||
} |