Skip to content
Browse files

Process atom and rss entries

  • Loading branch information...
1 parent 8c8be51 commit d666e9b7e845d391d6551282f0bb49890d273044 Michael Hughes committed Mar 18, 2013
Showing with 20 additions and 4 deletions.
  1. +18 −4 src/server/RSSCollector.coffee
  2. +2 −0 test/server/unit/RSSCollector_test.coffee
View
22 src/server/RSSCollector.coffee
@@ -1,5 +1,6 @@
xml2js = require 'xml2js'
http = require 'http'
+_ = require 'underscore'
exports.createRSSCollector = ->
@@ -17,12 +18,25 @@ exports.createRSSCollector = ->
parseFeed:(data,fun) ->
parser = new xml2js.Parser({explicitArray:false})
- parser.parseString data, (err,result)->
- if result.rss
- fun(err,result.rss.channel)
+ 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(err,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
2 test/server/unit/RSSCollector_test.coffee
@@ -35,6 +35,7 @@ describe 'A RSS collector', ->
@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 RSS XML', (done)->
@collector.requestFeed FakeRSS,(err,result)->
@@ -67,4 +68,5 @@ describe 'A RSS collector', ->
@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()

0 comments on commit d666e9b

Please sign in to comment.
Something went wrong with that request. Please try again.