From a4f819412afae636eee101aa3e627d5fc9fa30b5 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Thu, 22 Jan 2009 17:12:54 +0100 Subject: [PATCH] rss plugin: refactor select_nonempty The plugin was using two distinct, rather horrible methods to pick one among many fields of a given item. We replace both methods with a new RSS::Element method that eval()s its (String) arguments to return the first nonempty value. --- data/rbot/plugins/rss.rb | 54 ++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/data/rbot/plugins/rss.rb b/data/rbot/plugins/rss.rb index 86ec0727..404272bf 100644 --- a/data/rbot/plugins/rss.rb +++ b/data/rbot/plugins/rss.rb @@ -150,6 +150,19 @@ module ::RSS SlashModel::ELEMENTS.collect! {|name| "#{SLASH_PREFIX}_#{name}"} end + + class Element + # This method eval()s its (String) argument(s) looking for one that + # evaluates to a non-empty String + def select_nonempty(*ar) + # debug ar + ar.flatten.each do |i| + v = eval(i) rescue nil + return v unless v.nil_or_empty? + end + return nil + end + end end @@ -254,31 +267,30 @@ class RSSFeedsPlugin < Plugin :default => true, :desc => "Whether to display links from the text of a feed item.") - # Make an 'unique' ID for a given item, based on appropriate bot options + # Make a 'unique' ID for a given item, based on appropriate bot options # Currently only suppored is bot.config['rss.show_updated']: when false, # only the guid/link is accounted for. - - def block_rescue(df = nil, &block) - v = block.call rescue nil - (String === v && '' != v) ? v : nil - end - def make_uid(item) uid = [ - (block_rescue do item.guid.content end || - block_rescue do item.guid end || - block_rescue do item.link.href end || - block_rescue do item.link end - ) + item.select_nonempty(%w{ + guid.content + guid + link.href + link + }) ] if @bot.config['rss.show_updated'] uid.push( - block_rescue do item.content.content end || - block_rescue do item.description end + item.select_nonempty(%w{ + content.content + description + }) ) uid.unshift( - block_rescue do item.title.content end || - block_rescue do item.title end + item.select_nonempty(%w{ + title.content + title + }) ) end # debug "taking hash of #{uid.inspect}" @@ -955,12 +967,6 @@ class RSSFeedsPlugin < Plugin return seconds end - def select_nonempty(*ar) - # debug ar - ar.each { |i| return i unless i.nil_or_empty? } - return nil - end - def printFormattedRss(feed, item, opts=nil) # debug item places = feed.watchers @@ -1041,9 +1047,9 @@ class RSSFeedsPlugin < Plugin link = item.link.href rescue item.link rescue nil link.strip! if link - category = select_nonempty((item.category.content rescue nil), (item.dc_subject rescue nil)) + category = item.select_nonempty(%w{category.content dc_subject}) category.strip! if category - author = select_nonempty((item.author.name.content rescue nil), (item.dc_creator rescue nil), (item.author rescue nil)) + author = item.select_nonempty(%w{author.name.content dc_creator author}) author.strip! if author line1 = nil -- 2.32.0.93.g670b81a890