Class: Oldskool::IpinfodbHandler
- Inherits:
-
Object
- Object
- Oldskool::IpinfodbHandler
- Defined in:
- lib/oldskool/ipinfodb_handler.rb
Instance Method Summary collapse
- #handle_request(keyword, query) ⇒ Object
-
#initialize(params, keyword, config) ⇒ IpinfodbHandler
constructor
A new instance of IpinfodbHandler.
- #plugin_template(template) ⇒ Object
Constructor Details
#initialize(params, keyword, config) ⇒ IpinfodbHandler
Returns a new instance of IpinfodbHandler.
3 4 5 6 7 8 9 10 11 |
# File 'lib/oldskool/ipinfodb_handler.rb', line 3 def initialize(params, keyword, config) require 'geo_ip' require 'ipaddr' @params = params @keyword = keyword @config = config self end |
Instance Method Details
#handle_request(keyword, query) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/oldskool/ipinfodb_handler.rb', line 17 def handle_request(keyword, query) raise "Please set :ipinfodb_apikey in the config" unless @config[:ipinfodb_apikey] (query == "") ? ip = @params[:http_request].ip : ip = query ipaddr = IPAddr.new(ip) return({:template => :error, :error => "Invalid IP #{ip}, only IPv4 addresses are supported"}) unless ipaddr.ipv4? GeoIp.api_key = @config[:ipinfodb_apikey] geoip = GeoIp.geolocation(ip) {:template => plugin_template(:ipinfo), :ipinfo => geoip} end |
#plugin_template(template) ⇒ Object
13 14 15 |
# File 'lib/oldskool/ipinfodb_handler.rb', line 13 def plugin_template(template) File.read(File.("../../../views/#{template}.erb", __FILE__)) end |