]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/translator.rb
refactor: httputil no longer core module see #38
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / translator.rb
index 338216384ef5ea0af98b7e880eadf4096469bc08..c8983a287293b7cab573675b3f21d092d69e057d 100644 (file)
@@ -34,9 +34,10 @@ class Translator
 
   attr_reader :directions, :cache
 
-  def initialize(directions, cache={})
+  def initialize(directions, cache={}, bot)
     @directions = directions
     @cache = cache
+    @bot = bot
   end
 
   # Many translators use Mechanize, which changed namespace around version 1.0
@@ -121,13 +122,14 @@ class GoogleTranslator < Translator
     ga it ja kn kk km ko lv lt mk ms ml mt mr mn ne no or ps fa pl
     pt_PT pa ro ru sa sr sd si sk sl es sw sv tg ta tl te th bo tr
     uk ur uz ug vi cy yi auto]
-  def initialize(cache={})
+  def initialize(cache={}, bot)
     require 'mechanize'
-    super(Translator::Direction.all_to_all(LANGUAGES), cache)
+    super(Translator::Direction.all_to_all(LANGUAGES), cache, bot)
   end
 
   def do_translate(text, from, to)
     agent = Mechanize.new
+    agent.user_agent_alias = 'Linux Mozilla'
     page = agent.get URL
     form = page.form_with(:id => 'gt-form')
     form.sl = from
@@ -144,14 +146,14 @@ class YandexTranslator < Translator
 
   URL = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key=%s&lang=%s-%s&text=%s'
   KEY = 'trnsl.1.1.20140326T031210Z.1e298c8adb4058ed.d93278fea8d79e0a0ba76b6ab4bfbf6ac43ada72'
-  def initialize(cache)
+  def initialize(cache, bot)
     require 'uri'
     require 'json'
-    super(Translator::Direction.all_to_all(LANGUAGES), cache)
+    super(Translator::Direction.all_to_all(LANGUAGES), cache, bot)
   end
 
   def translate(text, from, to)
-    res = Irc::Utils.bot.httputil.get_response(URL % [KEY, from, to, URI.escape(text)])
+    res = @bot.httputil.get_response(URL % [KEY, from, to, URI.escape(text)])
     res = JSON.parse(res.body)
 
     if res['code'] != 200