Created
June 13, 2014 01:28
-
-
Save amacgregor/b9440ae5409fd7b9d41a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require Record | |
defrecord :xmlElement, Record.extract(:xmlElement, from_lib: "xmerl/include/xmerl.hrl") | |
defrecord :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl") | |
defrecord :xmlAttribute, Record.extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl") | |
defmodule RssParserTest do | |
use ExUnit.Case | |
def sample_xml do | |
""" | |
<rss> | |
<channel> | |
<title>Example Title</title> | |
<description>Custom Description</description> | |
<pubDate>Wed, 11 Jun 2014 18:00:41 +0200</pubDate> | |
<generator>PHP RSSGen 1.0.0 - Seld.be</generator> | |
<docs>http://www.rssboard.org/rss-2-0-10</docs> | |
<ttl>60</ttl> | |
<language>en</language> | |
<lastBuildDate>Tue, 27 May 2014 15:07:03 +0200</lastBuildDate> | |
<category>PHP</category> | |
<item> | |
<title>Item 1</title> | |
<description>Test 1</description> | |
<link>http://feeds.seld.be/~r/php-blog-seld/~3/JIWnIXuFQBU/upcoming-conferences-2013</link> | |
<pubDate>Tue, 19 Nov 2013 17:36:52 +0100</pubDate> | |
<guid isPermaLink="false">http://seld.be/notes/upcoming-conferences-2013</guid> | |
<category>News1</category> | |
<category>PHP2</category> | |
</item> | |
<item> | |
<title>Item 2</title> | |
<description>Test 2</description> | |
<link>http://feeds.seld.be/~r/php-blog-seld/~3/JIWnIXuFQBU/upcoming-conferences-2013</link> | |
<pubDate>Tue, 19 Nov 2013 17:36:52 +0100</pubDate> | |
<guid isPermaLink="false">http://seld.be/notes/upcoming-conferences-2013</guid> | |
<category>News</category> | |
<category>PHP</category> | |
</item> | |
</channel> | |
</rss> | |
""" | |
end | |
test "parsing the title out of the feed" do | |
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml)) | |
[title_element] = :xmerl_xpath.string('/rss/channel/title', xml) | |
[title_text] = title_element.content | |
title = title_text.value | |
assert title == 'Example Title' | |
end | |
test "parsing the description out of the feed" do | |
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml)) | |
[description_element] = :xmerl_xpath.string('/rss/channel/description',xml) | |
[description_text] = description_element.content | |
description = description_text.value | |
assert description == 'Custom Description' | |
end | |
test "parsing the title out of the items in the feed (alternate)" do | |
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml)) | |
items = :xmerl_xpath.string('/rss/channel/item', xml) | |
item_titles = items |> Enum.map(fn(x) -> | |
[title_element] = :xmerl_xpath.string('/item/title', x) | |
[title_text] = title_element.content | |
title_text.value | |
end) | |
assert item_titles == ['Item 1', 'Item 2'] | |
end | |
test "parsing the title out of the items in the feed" do | |
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml)) | |
items = :xmerl_xpath.string('/rss/channel/item/title/text()', xml) | |
item_titles = items |> Enum.map(fn(x) -> x.value end) | |
assert item_titles == ['Item 1', 'Item 2'] | |
end | |
test "parsing the description out of the items in the feed" do | |
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml)) | |
items = :xmerl_xpath.string('/rss/channel/item/description/text()', xml) | |
item_descriptions = items |> Enum.map(fn(x) -> x.value end) | |
assert item_descriptions = ['Test 1', 'Test 2'] | |
end | |
test "parsing the categories out of the items in the feed" do | |
{xml, _rest} = :xmerl_scan.string(bitstring_to_list(sample_xml)) | |
items = :xmerl_xpath.string('/rss/channel/item/category/text()', xml) | |
item_categories = items |> Enum.map(fn(x) -> x.value end) | |
assert item_categories = ['1', 'Test 2'] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment