Skip to content

Commit

Permalink
Merge pull request #239 from leonghui/jsonfeed-author
Browse files Browse the repository at this point in the history
add support for author in JSON feed
  • Loading branch information
alexdebril authored Nov 13, 2019
2 parents ba54b36 + f44a8b6 commit bba403f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/FeedIo/Parser/JsonParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace FeedIo\Parser;

use FeedIo\Feed\Item;
use FeedIo\Feed\Item\Author;
use FeedIo\FeedInterface;
use FeedIo\ParserAbstract;
use FeedIo\Reader\Document;
Expand Down Expand Up @@ -72,6 +73,15 @@ public function parseItems(iterable $items, FeedInterface $feed) : JsonParser
$contentHtml = $this->readOffset($dataItem, 'content_html');
$item->setDescription($this->readOffset($dataItem, 'content_text', $contentHtml));
$item->setLink($this->readOffset($dataItem, 'url'));

if (array_key_exists('author', $dataItem)) {
$authorItem = $dataItem['author'];
$author = new Author();
$author->setName($this->readOffset($authorItem, 'name'));
$author->setUri($this->readOffset($authorItem, 'url'));
$author->setEmail($this->readOffset($authorItem, 'email'));
$item->setAuthor($author);
}
$feed->add($item);
}

Expand Down
13 changes: 12 additions & 1 deletion tests/FeedIo/Parser/JsonParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,19 @@ public function testParseContent()

$items = $feed->toArray()['items'];

$this->assertCount(2, $items);
$this->assertCount(4, $items);
$this->assertEquals('https://jsonfeed.org/2017/05/17/announcing_json_feed', $items[0]['link']);
$this->assertNull($items[1]['link']);

$this->assertNull($items[0]['author']);
$this->assertNull($items[1]['author']);

$this->assertEquals('Manton Reece', $items[2]['author']->getName());
$this->assertNull($items[2]['author']->getUri());
$this->assertNull($items[2]['author']->getEmail());

$this->assertEquals('Manton Reece', $items[3]['author']->getName());
$this->assertEquals('http://manton.org', $items[3]['author']->getUri());
$this->assertEquals('manton@micro.blog', $items[3]['author']->getEmail());
}
}
20 changes: 20 additions & 0 deletions tests/samples/feed.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@
"title": "Announcing JSON Feed",
"content_html": "<p>We — Manton Reece and Brent Simmons — have noticed that JSON has become the developers’ choice for APIs, and that developers will often go out of their way to avoid XML. JSON is simpler to read and write, and it’s less prone to bugs.</p>\n\n<p>So we developed JSON Feed, a format similar to <a href=\"http://cyber.harvard.edu/rss/rss.html\">RSS</a> and <a href=\"https://tools.ietf.org/html/rfc4287\">Atom</a> but in JSON. It reflects the lessons learned from our years of work reading and publishing feeds.</p>\n\n<p><a href=\"https://jsonfeed.org/version/1\">See the spec</a>. It’s at version 1, which may be the only version ever needed. If future versions are needed, version 1 feeds will still be valid feeds.</p>\n\n<h4>Notes</h4>\n\n<p>We have a <a href=\"https://github.com/manton/jsonfeed-wp\">WordPress plugin</a> and, coming soon, a JSON Feed Parser for Swift. As more code is written, by us and others, we’ll update the <a href=\"https://jsonfeed.org/code\">code</a> page.</p>\n\n<p>See <a href=\"https://jsonfeed.org/mappingrssandatom\">Mapping RSS and Atom to JSON Feed</a> for more on the similarities between the formats.</p>\n\n<p>This website — the Markdown files and supporting resources — <a href=\"https://github.com/brentsimmons/JSONFeed\">is up on GitHub</a>, and you’re welcome to comment there.</p>\n\n<p>This website is also a blog, and you can subscribe to the <a href=\"https://jsonfeed.org/xml/rss.xml\">RSS feed</a> or the <a href=\"https://jsonfeed.org/feed.json\">JSON feed</a> (if your reader supports it).</p>\n\n<p>We worked with a number of people on this over the course of several months. We list them, and thank them, at the bottom of the <a href=\"https://jsonfeed.org/version/1\">spec</a>. But — most importantly — <a href=\"http://furbo.org/\">Craig Hockenberry</a> spent a little time making it look pretty. :)</p>",
"date_published": "2017-05-17T08:02:12-07:00"
},
{
"id": "https://jsonfeed.org/2017/05/17/announcing_json_feed",
"title": "Announcing JSON Feed",
"content_html": "<p>We — Manton Reece and Brent Simmons — have noticed that JSON has become the developers’ choice for APIs, and that developers will often go out of their way to avoid XML. JSON is simpler to read and write, and it’s less prone to bugs.</p>\n\n<p>So we developed JSON Feed, a format similar to <a href=\"http://cyber.harvard.edu/rss/rss.html\">RSS</a> and <a href=\"https://tools.ietf.org/html/rfc4287\">Atom</a> but in JSON. It reflects the lessons learned from our years of work reading and publishing feeds.</p>\n\n<p><a href=\"https://jsonfeed.org/version/1\">See the spec</a>. It’s at version 1, which may be the only version ever needed. If future versions are needed, version 1 feeds will still be valid feeds.</p>\n\n<h4>Notes</h4>\n\n<p>We have a <a href=\"https://github.com/manton/jsonfeed-wp\">WordPress plugin</a> and, coming soon, a JSON Feed Parser for Swift. As more code is written, by us and others, we’ll update the <a href=\"https://jsonfeed.org/code\">code</a> page.</p>\n\n<p>See <a href=\"https://jsonfeed.org/mappingrssandatom\">Mapping RSS and Atom to JSON Feed</a> for more on the similarities between the formats.</p>\n\n<p>This website — the Markdown files and supporting resources — <a href=\"https://github.com/brentsimmons/JSONFeed\">is up on GitHub</a>, and you’re welcome to comment there.</p>\n\n<p>This website is also a blog, and you can subscribe to the <a href=\"https://jsonfeed.org/xml/rss.xml\">RSS feed</a> or the <a href=\"https://jsonfeed.org/feed.json\">JSON feed</a> (if your reader supports it).</p>\n\n<p>We worked with a number of people on this over the course of several months. We list them, and thank them, at the bottom of the <a href=\"https://jsonfeed.org/version/1\">spec</a>. But — most importantly — <a href=\"http://furbo.org/\">Craig Hockenberry</a> spent a little time making it look pretty. :)</p>",
"date_published": "2017-05-17T08:02:12-07:00",
"author": {
"name": "Manton Reece"
}
},
{
"id": "https://jsonfeed.org/2017/05/17/announcing_json_feed",
"title": "Announcing JSON Feed",
"content_html": "<p>We — Manton Reece and Brent Simmons — have noticed that JSON has become the developers’ choice for APIs, and that developers will often go out of their way to avoid XML. JSON is simpler to read and write, and it’s less prone to bugs.</p>\n\n<p>So we developed JSON Feed, a format similar to <a href=\"http://cyber.harvard.edu/rss/rss.html\">RSS</a> and <a href=\"https://tools.ietf.org/html/rfc4287\">Atom</a> but in JSON. It reflects the lessons learned from our years of work reading and publishing feeds.</p>\n\n<p><a href=\"https://jsonfeed.org/version/1\">See the spec</a>. It’s at version 1, which may be the only version ever needed. If future versions are needed, version 1 feeds will still be valid feeds.</p>\n\n<h4>Notes</h4>\n\n<p>We have a <a href=\"https://github.com/manton/jsonfeed-wp\">WordPress plugin</a> and, coming soon, a JSON Feed Parser for Swift. As more code is written, by us and others, we’ll update the <a href=\"https://jsonfeed.org/code\">code</a> page.</p>\n\n<p>See <a href=\"https://jsonfeed.org/mappingrssandatom\">Mapping RSS and Atom to JSON Feed</a> for more on the similarities between the formats.</p>\n\n<p>This website — the Markdown files and supporting resources — <a href=\"https://github.com/brentsimmons/JSONFeed\">is up on GitHub</a>, and you’re welcome to comment there.</p>\n\n<p>This website is also a blog, and you can subscribe to the <a href=\"https://jsonfeed.org/xml/rss.xml\">RSS feed</a> or the <a href=\"https://jsonfeed.org/feed.json\">JSON feed</a> (if your reader supports it).</p>\n\n<p>We worked with a number of people on this over the course of several months. We list them, and thank them, at the bottom of the <a href=\"https://jsonfeed.org/version/1\">spec</a>. But — most importantly — <a href=\"http://furbo.org/\">Craig Hockenberry</a> spent a little time making it look pretty. :)</p>",
"date_published": "2017-05-17T08:02:12-07:00",
"author": {
"name": "Manton Reece",
"url": "http://manton.org",
"email": "manton@micro.blog"
}
}
]
}

0 comments on commit bba403f

Please sign in to comment.