diff --git a/README.md b/README.md index e69de29..7672bba 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,5 @@ +*** Get country codes *** +http://localhost:3001/country_codes?ip=github.com + +*** Get Details *** +http://localhost:3001?ip=github.com \ No newline at end of file diff --git a/app.rb b/app.rb index f618067..1c34081 100644 --- a/app.rb +++ b/app.rb @@ -1,13 +1,11 @@ require 'rubygems' require 'sinatra' -require 'redis' require 'json' require 'logger' +require 'geoip' require 'colorize' Dir[File.dirname(__FILE__) + '/lib/*.rb'].sort.each {|file| require file} -I18n.enforce_available_locales = false -I18n.locale = :en module LocationTracker class App < Sinatra::Base @@ -17,10 +15,14 @@ class App < Sinatra::Base get '/' do + ip = params['ip'] + Tracker.get_details(ip).to_json + end + get '/country_codes' do + ip = params['ip'] + Tracker.country_codes(ip) end end - - end diff --git a/config.ru b/config.ru index d6a1b9a..28a0ae1 100644 --- a/config.ru +++ b/config.ru @@ -1,6 +1,3 @@ -require 'logger' -use Rack::CommonLogger, logger - require File.join(File.dirname(__FILE__), 'app.rb') run Rack::URLMap.new('/' => LocationTracker::App ) diff --git a/lib/GeoLiteCity.dat b/lib/GeoLiteCity.dat new file mode 100644 index 0000000..5aa5e28 Binary files /dev/null and b/lib/GeoLiteCity.dat differ diff --git a/lib/tracker.rb b/lib/tracker.rb index afbca78..195d0fe 100644 --- a/lib/tracker.rb +++ b/lib/tracker.rb @@ -1,6 +1,16 @@ module Tracker - def self.get_location(ip) + def self.get_details(ip) + geoip.city(ip).to_hash + end + + def self.country_codes(ip) + details = geoip.city(ip) + [details[:country_code2], details[:country_code3]] + end + + def self.geoip + GeoIP.new('./lib/GeoLiteCity.dat') end end \ No newline at end of file