wpart_rss
Overview
The wpart_rss component is in fact a generic RSS feed builder. Since the RSS feeds have well-defined XML structure, it is possible to provide a schema of the feed and inject only the case-specific content to it.
Each RSS channel looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>SOME TITLE</title>
<link>LINK TO THE SITE</link>
<description>
CHANNEL DESCRIPTION
</description>
<language>CHANNEL LANGUAGE</language>
CHANNEL ITEMS
</channel>
</rss>Each channel item is as follows:
<item>
<title>ITEM TITLE</title>
<link>ITEM LINK</link>
<pubDate>ITEM PUBLICATION DATE</pubDate>
<description>ITEM DESCRIPTION</description>
<category>ITEM CATEGORY</category>
</item>Having the skeletons prepared, wpart_rss allows users to provide only the generator of the content and it gives the complete feed in return.
Records
The wpart_rss component defines two new records:
1 -record(rss, {
2 title = "",
3 link = "",
4 description = "",
5 language = "en"
6 }).
7
8 -record(rss_item, {
9 title = "",
10 link = "",
11 publication_date = "",
12 description = "",
13 category = ""
14 }).
In order to include them in your application, you must add the following directive to your module:
1 -include_lib("wpart_rss/include/rss.hrl").
API
create_channel(GeneratorModule, ChannelFun, ItemFun, Items) - function that returns the complete RSS channel. The meaning of the parameters is as follows:
GeneratorModule - the module from where both ChannelFun/0 and ItemFun/1 are exported
ChannelFun - the function within the GeneratorModule that returns filled #rss record
ItemFun - the function within the GeneratorModule that takes an Item (taken from Items) and return filled #rss_item record
Items - an arbitrary Erlang terms that should be used to build an RSS channel. Each of the item will be used as a parameter for the ItemFun
Example
1 -module(rss).
2 -export([dataflow/1, error/2]).
3 -export([rss/1]).
4
5 -export([rss_channel/0, rss_item/1]).
6
7 -include_lib("wpart_rss/include/rss.hrl").
8
9 dataflow(rss) -> [].
10
11 error(_, _) ->
12 ok.
13
14 rss(_) ->
15 {content, html, wpart_rss:create_channel(?MODULE, rss_channel, rss_item,
16 [[{title, "Dog article"}, {resource, "/dog"},
17 {description, "Why do people love dogs"}, {category, "animals"}],
18 [{title, "IE"}, {resource, "/dev/null"},
19 {description, "Why some programs should be banned"}, {category, "crap code"}]])}.
20
21 rss_channel() ->
22 #rss{title = "My service RSS feed",
23 link = "http://" ++ e_conf:host() ++ "/rss",
24 description = "hot and fresh RSS"}.
25
26 rss_item(Item) ->
27 #rss_item{title = proplists:get_value(title, Item),
28 link = "http://" ++ e_conf:host() ++ "/" ++ proplists:get_value(category, Item) ++
29 proplists:get_value(resource, Item),
30 publication_date = wtype_date:get_date("SDAY, DD SMONTH YYYY ", date()) ++
31 wtype_time:format("HH:MM:SS", time()),
32 description = wtype_html:htmlize(proplists:get_value(description, Item),
33 category = proplists:get_value(category, Item)}.
