diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2007-09-13 21:47:50 +0000 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2007-09-13 21:47:50 +0000 |
commit | 1002684a7491dd8c160f81c00c4b5a4dcb5ac260 (patch) | |
tree | fcc9d2014279f3764f2bd5536e648368ceac71b7 /data/rbot | |
parent | 033504340d0c9b28fabd2564b0122edaac2aef2e (diff) |
shortenurls plugin: handle failing services by trying other services, and make return values more uniform
Diffstat (limited to 'data/rbot')
-rw-r--r-- | data/rbot/plugins/shortenurls.rb | 28 |
1 files changed, 21 insertions, 7 deletions
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 |