Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit d0e145e1236993c404a38cfcf98a1fcc02119700 @ripienaar committed Feb 2, 2012
Showing with 109 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +31 −0 README.md
  3. +30 −0 Rakefile
  4. +1 −0 lib/oldskool-ipinfodb.rb
  5. +31 −0 lib/oldskool/ipinfodb_handler.rb
  6. +15 −0 views/ipinfo.erb
1 .gitignore
@@ -0,0 +1 @@
+pkg
31 README.md
@@ -0,0 +1,31 @@
+What?
+=====
+
+A plugin for Oldskool displays IP information from ipinfodb.com
+
+Configuration?
+--------------
+
+You should sign up for an account at ipinfodb.com, once you have an
+API key fill it into your config
+
+In your _oldskool.yaml_ add the following:
+
+ - :ipinfodb_apikey: YOUR_API_KEY
+
+You can then create a keyword:
+
+ :keywords:
+ - :type: :ipinfodb
+ :keywords:
+ - ip
+
+This creates a keyword _ip_ that you can call without any parameters
+to get info for your browser IP, otherwise supply an IP as an argument.
+
+Only IPv4 is supported at the moment
+
+Contact?
+--------
+
+R.I.Pienaar / rip@devco.net / @ripienaar / http://devco.net/
30 Rakefile
@@ -0,0 +1,30 @@
+require 'rubygems'
+require 'rake/gempackagetask'
+
+spec = Gem::Specification::new do |spec|
+ spec.name = "oldskool-ipinfodb"
+ spec.version = "0.0.1"
+ spec.platform = Gem::Platform::RUBY
+ spec.summary = "oldskool-ipinfodb"
+ spec.description = "description: Display IP information from ipinfodb.com"
+
+ spec.files = FileList["lib/**/*.rb", "views/*.erb"]
+ spec.executables = []
+
+ spec.require_path = "lib"
+
+ spec.has_rdoc = false
+ spec.test_files = nil
+ spec.add_dependency 'geo_ip'
+
+ spec.extensions.push(*[])
+
+ spec.author = "R.I.Pienaar"
+ spec.email = "rip@devco.net"
+ spec.homepage = "http://devco.net/"
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.need_zip = false
+ pkg.need_tar = false
+end
1 lib/oldskool-ipinfodb.rb
@@ -0,0 +1 @@
+require 'oldskool/ipinfodb_handler'
31 lib/oldskool/ipinfodb_handler.rb
@@ -0,0 +1,31 @@
+module Oldskool
+ class IpinfodbHandler
+ def initialize(params, keyword, config)
+ require 'geo_ip'
+ require 'ipaddr'
+
+ @params = params
+ @keyword = keyword
+ @config = config
+ self
+ end
+
+ def plugin_template(template)
+ File.read(File.expand_path("../../../views/#{template}.erb", __FILE__))
+ end
+
+ 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
+ end
+end
15 views/ipinfo.erb
@@ -0,0 +1,15 @@
+<% unless @error %>
+ <% unless @result[:ipinfo][:status_message] == "" %>
+ Failed to retrieve IP Info: <%= @result[:ipinfo][:status_message] %>
+ <% else %>
+ <table>
+ <tr><td><strong>IP Address:</strong></td><td><%= @result[:ipinfo][:ip] %></td></tr>
+ <tr><td><strong>City:</strong></td><td><%= @result[:ipinfo][:city].capitalize %></td></tr>
+ <tr><td><strong>Country:</strong></td><td><%= @result[:ipinfo][:country_name].capitalize %></td></tr>
+ <tr><td><strong>Country Code:</strong></td><td><%= @result[:ipinfo][:country_code] %></td></tr>
+ <tr><td><strong>Region:</strong></td><td><%= @result[:ipinfo][:region_name].capitalize %></td></tr>
+ <tr><td><strong>ZIP Code:</strong></td><td><%= @result[:ipinfo][:zip_code] %></td></tr>
+ <tr><td><strong>Coordinates:</strong></td><td>lon <%= @result[:ipinfo][:longitude] %> lat <%= @result[:ipinfo][:latitude] %></td></tr>
+ </table>
+ <% end %>
+<% end %>

0 comments on commit d0e145e

Please sign in to comment.