From 1d5f42447b9dafef851eee4a72430e512604278f Mon Sep 17 00:00:00 2001 From: Yaohan Chen Date: Fri, 21 May 2010 17:11:59 -0400 Subject: translator: fix undefined variable Fix an error in commit 027b6965f5abf5d05a95 which causes @lang_list to be undefined but used in BabelFish. --- data/rbot/plugins/translator.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'data/rbot') diff --git a/data/rbot/plugins/translator.rb b/data/rbot/plugins/translator.rb index 9fb74e8a..2e7be919 100644 --- a/data/rbot/plugins/translator.rb +++ b/data/rbot/plugins/translator.rb @@ -215,18 +215,24 @@ class BabelfishTranslator < Translator def initialize(cache) require 'mechanize' - form = WWW::Mechanize.new.get('http://babelfish.altavista.com/babelfish/'). - forms_with(:name => 'frmTrText').first - lang_list = form.fields_with(:name => 'lp').first + (_, lang_list) = parse_page language_pairs = lang_list.options.map {|o| o.value.split('_')}. reject {|p| p.empty?} super(Translator::Direction.pairs(language_pairs), cache) end - def do_translate(text, from, to) - @form ||= WWW::Mechanize.new.get('http://babelfish.altavista.com/babelfish/'). - forms_with(:name => 'frmTrText').first + def parse_page + form = WWW::Mechanize.new.get('http://babelfish.altavista.com/babelfish/'). + forms_with(:name => 'frmTrText').first + lang_list = form.fields_with(:name => 'lp').first + [form, lang_list] + end + def do_translate(text, from, to) + unless @form && @lang_list + @form, @lang_list = parse_page + end + if @form.fields_with(:name => 'trtext').empty? @form.add_field!('trtext', text) else -- cgit v1.2.3