lib/govfeed.rb in govfeed-0.0.1 vs lib/govfeed.rb in govfeed-0.0.2
- old
+ new
@@ -62,58 +62,104 @@
:ca_pe => "http://www.gov.pe.ca/index.php3?number=rss",
:ca_sk => "http://www.gov.sk.ca/Common/PageTemplates/rss.aspx"
}
end
+ def self.getItemTitle(item)
+
+ title = String.new
+
+ if (item/"title") != nil
+ title = "<a href=\"" + (item/"link").inner_text + "\"><h2 class=\"govfeedHeading\">" + (item/"title").inner_text + "</h2></a>\n"
+ end
+
+ title
+
+ end
+
+ def self.getItemPubDate(item)
+
+ pubDate = String.new
+
+ if (item/"pubDate") != nil
+ pubDate = "<div class=\"govfeedPubDate\">" + (item/"pubDate").inner_text + "</div>\n"
+ end
+
+ pubDate
+
+ end
+
+ def self.getItemDescription(item)
+
+ description = String.new
+
+ if (item/"description") != nil
+ description = "<div class=\"govfeedDescription\">" + self.truncate_words((item/"description").inner_text, 40) + "</div>\n"
+ end
+
+ description
+
+ end
def self.getFeed(feed, numberOfStories = 0)
+ begin
+ counter = 0
+
+ # This hash contains the Feed URLs
+ # The format goes Federal feeds, State\Province feeds, Think Tank\Institution feeds
+ # Some states\provinces\territories do not yet have RSS feeds or feeds that work (Idaho).
+ # They will be added to this hash when they are created or fixed.
+ feed_url = self.getFeedList
- # This hash contains the Feed URLs
- # The format goes Federal feeds, State\Province feeds, Think Tank\Institution feeds
- # Some states\provinces\territories do not yet have RSS feeds or feeds that work (Idaho).
- # They will be added to this hash when they are created or fixed.
- feed_url = self.getFeedList
-
- # Curb-Fu will be fetching the feed.
- feed = CurbFu.get(feed_url[feed])
+ # Curb-Fu will be fetching the feed.
+ feed = CurbFu.get(feed_url[feed])
- # the hpricot gem will be used to parse it as we build the HTML
- doc = Hpricot(feed.body.to_s)
+ # the hpricot gem will be used to parse it as we build the HTML
+ doc = Hpricot.XML(feed.body.to_s)
- # RSS feed heading
- rss = "<h1 id=\"govfeedTitle\">" + (doc/"title")[0].inner_text + "</h1>\n"
+ # RSS feed title
+ rss = "<h1 class=\"govfeedTitle\">" + (doc/"title")[0].inner_text + "</h1>\n"
- # Make sure that the feed description exists before attempting to parse it
- if (((doc/"description")[0] != nil) || ((doc/"description")[0] != ""))
- rss += "<div class=\"govfeedDiv\">" + (doc/"description")[0].inner_text + "</div>\n"
- end
+ # Make sure that the feed description exists before attempting to parse it
+ if (((doc/"description")[0] != nil) || ((doc/"description")[0] != ""))
+ rss += "<div class=\"govfeedDescription\">" + (doc/"description")[0].inner_text + "</div>\n"
+ end
- # Make sure that the feed image exists before attempting to parse it
- if (doc/"image")[0] != nil
- rss += "<div class=\"govfeedImage\"><img src=\"" + (doc/"image"/"url")[0].inner_text + "\" alt=\"\" /></div>\n"
- end
+ # Make sure that the feed image exists before attempting to parse it
+ if (doc/"image")[0] != nil
+ rss += "<div class=\"govfeedImage\"><img src=\"" + (doc/"image"/"url")[0].inner_text + "\" alt=\"" + (doc/"image"/"title")[0].inner_text + "\" /></div>\n"
+ end
- # individual RSS feed items
- # If the number of stories parameter was not passed in, fetch all
- if numberOfStories == 0
+ # individual RSS feed items
+ # If the number of stories parameter was not passed in, fetch all
(doc/"item").each do |item|
- rss += "<a href=\"" + (item/"link").inner_text + "\"><h2 class=\"govfeedHeading\">" + (item/"title").inner_text + "</h2></a>\n"
- rss += "<div class=\"govfeedDiv\">" + (item/"pubDate").inner_text + "<br />" + self.truncate_words((item/"description").inner_text, 40) + "</div>\n"
- end
- else
- counter = 0
- (doc/"item").each do |item|
- rss += item.inner_text
- rss += "<a href=\"" + (item/"link").inner_text + "\"><h2 class=\"govfeedHeading\">" + (item/"title").inner_text + "</h2></a>\n"
- rss += "<div class=\"govfeedDiv\">" + (item/"pubDate").inner_text + "<br />" + self.truncate_words((item/"description").inner_text, 40) + "</div>\n"
- counter = counter + 1
- if counter == numberOfStories
- break
+
+ rss += self.getItemTitle(item)
+
+ rss += self.getItemPubDate(item)
+
+ rss += self.getItemDescription(item)
+
+ # If the number of stories is not equal to 0, that means it was limited
+ if numberOfStories != 0
+
+ counter = counter + 1
+
+ if counter == numberOfStories
+ break
+ end
+
end
+
end
- end
- rss
+ rss
+
+ rescue
+
+ "Could not parse RSS feed."
+
+ end
end
end