Help with using RSS parser in a Rails project?


#1

I’m VERY new at Ruby on Rails. I’m at my first attempt at an application
for myself that will do something useful, thanks in advance for any
advice!

I’ve already written a functioning Ruby script that goes through an RSS
feed to extract and print information I want (headlines and URL for
items published on a certain date). Now I’m trying to turn it into a
Ruby on Rails project (to share with colleagues on an internal server,
and hopefully build on it).

I used Ruby’s built-in rss library to parse the feed, with code like
this:

some of this code adapted from The Ruby Way

i = 0
open(url) do |h|
resp = h.read
result = RSS::Parser.parse(resp,false)
result.items.each do |item|

I’ve made a hash with two keys – the key and the item number

    myhash[["title",i]] = item.title # pulls the headline
    myhash[["pubDate",i]] = item.pubDate # pulls the publish date
    myhash[["link",i]] = item.link # pulls the url

    regex = /articleId=(\d+)/ # uses a regular expression to get the

article ID
contentid = regex.match(item.link)
myhash[[“id”,i]] = contentid[1]
posted = item.pubDate.to_s
regexp = /[A-Z][a-z][a-z],\s(\d+\s[A-Z][a-z]+\s20\d\d)/
match = regexp.match(posted)

    if theday == match[1] # checking to see if the publish date

matches the date I want from elsewhere in the script

         print myhash[["id",i]]
         print "  "
         puts myhash[["title",i]]
         posted_array.push(myhash[["id",i]])
       end

i = i+1
end

end
<<<<<<<<<<<

This all works fine when I run it as a Ruby script from my command line.
But within a Rails project, I get an error message

undefined method `published’ for #RSS::Rss::Channel::Item:0x478c43c

If I comment out the line with the item.published, then I get

undefined method `url’ for #RSS::Rss::Channel::Item:0x453bac0

and so on.

Clearly I’m not using the built-in rss library correctly within Rails.
Suggestions to point this beginner on how to do it correctly would be
greatly appreciated!!!


#2

Oops never mind, I’ve found my own answer, I needed to use the proper
tags from the RSS feed – pubDate instead of published, etc. D’oh. No
idea why that was working correctly in the Ruby version!