I've been struggling with an adjustment to a rss parsesr (a freelancer built the feature, I'm a newbie working on my own app). The parser needs a conditional which means it will only parse new posts in a feed if they have a date_published which is later than the time at which the Source (the applications model naming convention for a feed URL) was created at, i.e. date_published later than source.created_at. [1] is the original script, [2] is my failed attempt (but maybe it gives you an idea of what I've been trying to do);
[1]
if feed feed.items.each do |item| item_date = DateTime.parse(item.date_published.to_s).strftime("%Y-%m-%d %H:%M:%S")
if NewsItem.find(:first, :conditions => {:published_at => item_date, :source_id => source.id}) RAILS_DEFAULT_LOGGER.info("RssReader: Already scraped #{item.url}") else news_item = NewsItem.new( :title => item.title, :url => item.url, :excerpt => item.content.....
[2]
if feed feed.items.each do |item| item_date = DateTime.parse(item.date_published.to_s).strftime("%Y-%m-%d %H:%M:%S")
# the new conditional if item_date < source.created_at RAILS_DEFAULT_LOGGER.info("RssReader: No new NewsItems since the Source was created") elsif NewsItem.find(:first, :conditions => {:published_at => item_date, :source_id => source.id}) RAILS_DEFAULT_LOGGER.info("RssReader: Already scraped #{item.url}") else news_item = NewsItem.new( :title => item.title, :url => item.url, :excerpt => item.content....
Any ideas? I'm presuming the above doesn't work because it's a comparison of two strings. However, a friend helped me to try many different types of DateTime.parse and Time.parse on the source.created_at in the conditional, to get a fair comparison of DateTime formats, but none of them worked. We also followed some trial & error by grabbing the output of different DateTime.parse methods in the log, but we couldn't find one that works (to_i didn't seem to do it).
Is there a Rails 'later_than' help or a guide to successful DateTime comparisons out there? Or does anyone know the correct approach for this?