Skip to content

Commit

Permalink
Merge pull request #100 from j0k3r/extractor-giphy
Browse files Browse the repository at this point in the history
Add Giphy extractor
  • Loading branch information
j0k3r authored Oct 8, 2016
2 parents 0e7b26d + 9b42170 commit 30f3405
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/FeedBundle/Extractor/Giphy.php
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>';
}
}
8 changes: 8 additions & 0 deletions src/FeedBundle/Resources/config/extractors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,11 @@ services:
- [ setLogger, [ "@logger" ]]
tags:
- { name: feed.extractor, alias: reddituploads }

feed.extractor.giphy:
class: Api43\FeedBundle\Extractor\Giphy
calls:
- [ setLogger, [ "@logger" ]]
- [ setClient, [ "@guzzle.client" ]]
tags:
- { name: feed.extractor, alias: giphy }
70 changes: 70 additions & 0 deletions tests/FeedBundle/Extractor/GiphyTest.php
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');
}
}

0 comments on commit 30f3405

Please sign in to comment.