Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: mikeyhu/RSS-Homepage
base: 33316d3ae3
...
head fork: mikeyhu/RSS-Homepage
compare: d666e9b7e8
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Mar 17, 2013
Michael Hughes Now parses RSS and Atom titles... not entries/items though. 8c8be51
Commits on Mar 18, 2013
Michael Hughes Process atom and rss entries d666e9b
View
30 src/server/RSSCollector.coffee
@@ -1,9 +1,10 @@
xml2js = require 'xml2js'
http = require 'http'
+_ = require 'underscore'
exports.createRSSCollector = ->
- requestRSS:(connectionString,fun)->
+ requestFeed:(connectionString,fun)->
data=""
http.get connectionString, (res) ->
res.setEncoding('utf8')
@@ -15,8 +16,27 @@ exports.createRSSCollector = ->
console.log("Got error: " + e.message)
fun(e.message,null)
- parseRSS:(data,fun) ->
+ parseFeed:(data,fun) ->
parser = new xml2js.Parser({explicitArray:false})
- parser.parseString data, (err,result)->
- set = result?.rss.channel
- fun(err,set)
+ parser.parseString data, (err,result)=>
+ if err
+ fun(err,null)
+ else if result.rss
+ fun(null,@parseRSS result.rss.channel)
+ else if result.feed
+ fun(null,@parseAtom result.feed)
+ else
+ fun("Unable to parse feed",null)
+
+ parseAtom:(data)->
+ {
+ title:data.title
+ entry:_.flatten([data.entry])
+ }
+
+ parseRSS:(data)->
+ {
+ title:data.title
+ entry:_.flatten([data.item])
+ }
+
View
35 test/server/unit/RSSCollector_test.coffee
@@ -32,12 +32,41 @@ describe 'A RSS collector', ->
</channel>
</rss>
"""
- @collector.parseRSS data,(err,result)->
+ @collector.parseFeed data,(err,result)->
expect(err).to.be.null
expect(result.title,"title").to.equal("BBC News - Home")
+ expect(result.entry[0].title).to.equal("Cameron halts press regulation talks")
done()
- it 'should be able to retrieve and parse some XML', (done)->
- @collector.requestRSS FakeRSS,(err,result)->
+ it 'should be able to retrieve and parse some RSS XML', (done)->
+ @collector.requestFeed FakeRSS,(err,result)->
expect(err,err).to.be.null
expect(result,"result").to.exist
+ done()
+ it 'should be able to parse some Atom XML', (done)->
+ data =
+ """
+ <?xml version="1.0" encoding="utf-8"?>
+ <feed xmlns="http://www.w3.org/2005/Atom">
+ <title>Example Feed</title>
+ <subtitle>Insert witty or insightful remark here</subtitle>
+ <link href="http://example.org/"/>
+ <updated>2003-12-13T18:30:02Z</updated>
+ <author>
+ <name>John Doe</name>
+ <email>johndoe@example.com</email>
+ </author>
+ <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
+ <entry>
+ <title>Atom-Powered Robots Run Amok</title>
+ <link href="http://example.org/2003/12/13/atom03"/>
+ <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
+ <updated>2003-12-13T18:30:02Z</updated>
+ <summary>Some text.</summary>
+ </entry>
+ </feed>
+ """
+ @collector.parseFeed data,(err,result)->
+ expect(err).to.be.null
+ expect(result.title,"title").to.equal("Example Feed")
+ expect(result.entry[0].title).to.equal("Atom-Powered Robots Run Amok")
done()

No commit comments for this range

Something went wrong with that request. Please try again.