Skip to content

Commit

Permalink
get country code
Browse files Browse the repository at this point in the history
  • Loading branch information
cmthakur committed Dec 9, 2014
1 parent 04161bb commit abe0db9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*** Get country codes ***
http://localhost:3001/country_codes?ip=github.com

*** Get Details ***
http://localhost:3001?ip=github.com
12 changes: 7 additions & 5 deletions app.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
3 changes: 0 additions & 3 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
require 'logger'
use Rack::CommonLogger, logger

require File.join(File.dirname(__FILE__), 'app.rb')

run Rack::URLMap.new('/' => LocationTracker::App )
Binary file added lib/GeoLiteCity.dat
Binary file not shown.
12 changes: 11 additions & 1 deletion lib/tracker.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit abe0db9

Please sign in to comment.