summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Hecker <mail@apoc.cc>2020-04-14 19:42:37 +0200
committerMatthias Hecker <mail@apoc.cc>2020-04-14 19:42:37 +0200
commitfda847bf583b9cc02eda341730d53887302ffe57 (patch)
treef5ed918626e2b7eb439da30f0c51aa447a18a41b
parent391c2ce19c0954d3ea0f7b6cc17c001d26491ca6 (diff)
refactor: httputil no longer core module see #38
This is intended to make testing/mocking of the http client easier.
-rw-r--r--data/rbot/plugins/geoip.rb20
-rw-r--r--data/rbot/plugins/translator.rb13
-rw-r--r--data/rbot/plugins/weather.rb8
-rw-r--r--lib/rbot/httputil.rb (renamed from lib/rbot/core/utils/httputil.rb)17
-rw-r--r--lib/rbot/ircbot.rb3
5 files changed, 24 insertions, 37 deletions
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:.*?<a href=".*?" target="_blank"> (.*?)</a>}m,
@@ -30,7 +30,7 @@ module ::GeoIP
:lon => %r{Longitude:.*?<td align="left" class="arial_bold">(.*?)</td>}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
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"