From: Giuseppe Bilotta Date: Thu, 13 Sep 2007 21:47:50 +0000 (+0000) Subject: shortenurls plugin: handle failing services by trying other services, and make return... X-Git-Url: https://git.netwichtig.de/gitweb/?a=commitdiff_plain;h=1002684a7491dd8c160f81c00c4b5a4dcb5ac260;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git shortenurls plugin: handle failing services by trying other services, and make return values more uniform --- diff --git a/data/rbot/plugins/shortenurls.rb b/data/rbot/plugins/shortenurls.rb index 743cad08..4cb96c35 100644 --- a/data/rbot/plugins/shortenurls.rb +++ b/data/rbot/plugins/shortenurls.rb @@ -35,7 +35,7 @@ class ShortenURLs < Plugin url = params[:url] if url == "help" m.reply help(m.plugin) unless params[:called] - return + return nil end begin to_uri = URI.parse(url) @@ -43,19 +43,33 @@ class ShortenURLs < Plugin raise URI::InvalidURIError if to_uri.class == URI::Generic rescue URI::InvalidURIError m.reply "#{url} doesn't look like an URL to me ..." unless params[:called] - return + return nil end service = params[:service] || m.plugin.to_sym service = :rubyurl if service == :shorturl - short = WWW::ShortURL.shorten(url, service) + tried = [] + short = nil - if params[:called] - return short - else - m.reply "#{url} shortened to #{short}" + begin + tried << service + short = WWW::ShortURL.shorten(url, service) + raise WWW::InvalidService, "#{service} returned an empty string for #{url}" unless short and not short.empty? + rescue WWW::InvalidService + pool = services - tried + if pool.empty? + m.reply "#{service} failed, and I don't know what else to try next" unless params[:called] + return nil + else + service = pool.pick_one + m.reply "#{tried.last} failed, I'll try #{service} instead" unless params[:called] + retry + end end + + m.reply "#{url} shortened to #{short}" unless params[:called] + return short end end