From fda847bf583b9cc02eda341730d53887302ffe57 Mon Sep 17 00:00:00 2001 From: Matthias Hecker Date: Tue, 14 Apr 2020 19:42:37 +0200 Subject: [PATCH] refactor: httputil no longer core module see #38 This is intended to make testing/mocking of the http client easier. --- data/rbot/plugins/geoip.rb | 20 ++++++++++---------- data/rbot/plugins/translator.rb | 13 +++++++------ data/rbot/plugins/weather.rb | 8 ++++---- lib/rbot/{core/utils => }/httputil.rb | 17 ----------------- lib/rbot/ircbot.rb | 3 +++ 5 files changed, 24 insertions(+), 37 deletions(-) rename lib/rbot/{core/utils => }/httputil.rb (98%) diff --git a/data/rbot/plugins/geoip.rb b/data/rbot/plugins/geoip.rb index 8c5e44a4..48391a10 100644 --- a/data/rbot/plugins/geoip.rb +++ b/data/rbot/plugins/geoip.rb @@ -20,7 +20,7 @@ module ::GeoIP hostname =~ Resolv::IPv4::Regex && (hostname.split(".").map { |e| e.to_i }.max <= 255) end - def self.geoiptool(ip) + def self.geoiptool(bot, ip) url = "http://www.geoiptool.com/en/?IP=" regexes = { :country => %r{Country:.*? (.*?)}m, @@ -30,7 +30,7 @@ module ::GeoIP :lon => %r{Longitude:.*?(.*?)}m } res = {} - raw = Irc::Utils.bot.httputil.get_response(url+ip) + raw = bot.httputil.get_response(url+ip) raw = raw.decompress_body(raw.raw_body) regexes.each { |key, regex| res[key] = raw.scan(regex).join('') } @@ -40,8 +40,8 @@ module ::GeoIP IPINFODB_URL = "http://api.ipinfodb.com/v2/ip_query.php?key=%{key}&ip=%{ip}" - def self.ipinfodb(ip) - key = Irc::Utils.bot.config['geoip.ipinfodb_key'] + def self.ipinfodb(bot, ip) + key = bot.config['geoip.ipinfodb_key'] return if not key or key.empty? url = IPINFODB_URL % { :ip => ip, @@ -49,7 +49,7 @@ module ::GeoIP } debug "Requesting #{url}" - xml = Irc::Utils.bot.httputil.get(url) + xml = bot.httputil.get(url) if xml obj = REXML::Document.new(xml) @@ -67,11 +67,11 @@ module ::GeoIP end JUMP_TABLE = { - "ipinfodb" => Proc.new { |ip| ipinfodb(ip) }, - "geoiptool" => Proc.new { |ip| geoiptool(ip) }, + "ipinfodb" => Proc.new { |bot, ip| ipinfodb(bot, ip) }, + "geoiptool" => Proc.new { |bot, ip| geoiptool(bot, ip) }, } - def self.resolve(hostname, api) + def self.resolve(bot, hostname, api) raise InvalidHostError unless valid_host?(hostname) begin @@ -82,7 +82,7 @@ module ::GeoIP raise BadAPIError unless JUMP_TABLE.key?(api) - return JUMP_TABLE[api].call(ip) + return JUMP_TABLE[api].call(bot, ip) end end @@ -179,7 +179,7 @@ class GeoIpPlugin < Plugin begin apis = @bot.config['geoip.sources'] apis.compact.each { |api| - geo = GeoIP::resolve(host, api) + geo = GeoIP::resolve(@bot, host, api) if geo and geo[:country] != "" break end diff --git a/data/rbot/plugins/translator.rb b/data/rbot/plugins/translator.rb index 833701c3..c8983a28 100644 --- a/data/rbot/plugins/translator.rb +++ b/data/rbot/plugins/translator.rb @@ -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,9 +122,9 @@ 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) @@ -145,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 diff --git a/data/rbot/plugins/weather.rb b/data/rbot/plugins/weather.rb index 36a5a88f..7c1336f0 100644 --- a/data/rbot/plugins/weather.rb +++ b/data/rbot/plugins/weather.rb @@ -15,15 +15,15 @@ require 'rexml/document' # Wraps NOAA National Weather Service information class CurrentConditions - @@bot = Irc::Utils.bot - def initialize(station) + def initialize(station, bot) @station = station + @bot = bot @url = "http://www.nws.noaa.gov/data/current_obs/#{URI.encode @station.upcase}.xml" @current_conditions = String.new end def update begin - resp = @@bot.httputil.get_response(@url) + resp = @bot.httputil.get_response(@url) case resp when Net::HTTPSuccess cc_doc = (REXML::Document.new resp.body).root @@ -161,7 +161,7 @@ class WeatherPlugin < Plugin if @nws_cache.has_key?(where) then met = @nws_cache[where] else - met = CurrentConditions.new(where) + met = CurrentConditions.new(where, @bot) end if met begin diff --git a/lib/rbot/core/utils/httputil.rb b/lib/rbot/httputil.rb similarity index 98% rename from lib/rbot/core/utils/httputil.rb rename to lib/rbot/httputil.rb index fb275547..75b5b80b 100644 --- a/lib/rbot/core/utils/httputil.rb +++ b/lib/rbot/httputil.rb @@ -726,20 +726,3 @@ class HttpUtil end end end - -class HttpUtilPlugin < CoreBotModule - def initialize(*a) - super(*a) - debug 'initializing httputil' - @bot.httputil = Irc::Utils::HttpUtil.new(@bot) - end - - def cleanup - debug 'shutting down httputil' - @bot.httputil.cleanup - @bot.httputil = nil - super - end -end - -HttpUtilPlugin.new diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index ddbc6eb6..3c508229 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -50,6 +50,7 @@ require 'rbot/registry' require 'rbot/plugins' require 'rbot/message' require 'rbot/language' +require 'rbot/httputil' module Irc @@ -471,6 +472,7 @@ class Bot @plugins = nil @lang = Language.new(self, @config['core.language']) + @httputil = Utils::HttpUtil.new(self) begin @auth = Auth::manager @@ -1236,6 +1238,7 @@ class Bot debug "\tignoring cleanup error: #{$!}" end end + @httputil.cleanup # debug "\tstopping timers ..." # @timer.stop # debug "Closing registries" -- 2.39.2