Skip to content
This repository has been archived by the owner on Oct 31, 2020. It is now read-only.

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ripienaar committed Feb 2, 2012
0 parents commit d0e145e
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
pkg
31 changes: 31 additions & 0 deletions 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 changes: 30 additions & 0 deletions 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 change: 1 addition & 0 deletions lib/oldskool-ipinfodb.rb
@@ -0,0 +1 @@
require 'oldskool/ipinfodb_handler'
31 changes: 31 additions & 0 deletions 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 changes: 15 additions & 0 deletions 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.