Skip to content

Instantly share code, notes, and snippets.

@hmadison
Created December 1, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmadison/c6ca43702a84b9ab1e7e to your computer and use it in GitHub Desktop.
Save hmadison/c6ca43702a84b9ab1e7e to your computer and use it in GitHub Desktop.
Simple IP Geolocation Serive
require 'bundler/inline'
require 'json'
gemfile do
source 'https://rubygems.org'
gem 'sliver'
gem 'maxminddb'
gem 'rack'
gem 'byebug'
end
class IPLookupAction
include Sliver::Action
DB = MaxMindDB.new('./GeoLite2-City.mmdb')
def call
result = DB.lookup(ip)
response.headers['Content-Type'] = 'application/json'
not_found unless result.found?
response.status = 200
response.body = [JSON.generate(result.to_hash)]
end
private
def not_found
response.status = 404
response.body = ['{}']
end
def ip
self.request.params['ip'] || '255.255.255.255'
end
end
app = Sliver::API.new do |api|
api.connect :get, '/lookup', IPLookupAction
end
run app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment