diff --git a/src/FeedBundle/Extractor/Giphy.php b/src/FeedBundle/Extractor/Giphy.php new file mode 100644 index 00000000..621042c3 --- /dev/null +++ b/src/FeedBundle/Extractor/Giphy.php @@ -0,0 +1,65 @@ +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 '

'.$data['title'].'

'; + } +} diff --git a/src/FeedBundle/Resources/config/extractors.yml b/src/FeedBundle/Resources/config/extractors.yml index e62c2070..51f19f88 100644 --- a/src/FeedBundle/Resources/config/extractors.yml +++ b/src/FeedBundle/Resources/config/extractors.yml @@ -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 } diff --git a/tests/FeedBundle/Extractor/GiphyTest.php b/tests/FeedBundle/Extractor/GiphyTest.php new file mode 100644 index 00000000..7af7538f --- /dev/null +++ b/tests/FeedBundle/Extractor/GiphyTest.php @@ -0,0 +1,70 @@ +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('

my title

', $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'); + } +}