<commit_before>require 'rubygems'
require 'sinatra'
require 'sinatra/activerecord'
require 'socket'

set :database, 'postgres://ip:ip@localhost/ips'

def hostname(ip)
	begin
		address_parts = ip.split(/\./).map(&:to_i)
		hostname = Socket.gethostbyaddr(address_parts.pack('CCCC')).first
	rescue SocketError
		hostname = nil
	end
	hostname
end

class IP < ActiveRecord::Base
	attr_accessible :name, :ip
end

get '/' do
	@ip = env['REMOTE_ADDR']
	@hostname = hostname(@ip) if @ip
	@forwarded = env['HTTP_X_FORWARDED_FOR']
	@forwarded_host = hostname(@forwarded) if @forwarded

	erb :'index.html'
end

get '/:host' do |host|
	@host = host
	record = IP.find_by_host(host)
	if record
		@ip = record.ip
		erb :'show.html'
	else
		erb :'unknown.html'
	end
end<commit_msg>Add capability to create/update record using POST<commit_after>require 'rubygems'
require 'sinatra'
require 'sinatra/activerecord'
require 'socket'

set :database, 'postgres://ip:ip@localhost/ips'

def hostname(ip)
	begin
		address_parts = ip.split(/\./).map(&:to_i)
		hostname = Socket.gethostbyaddr(address_parts.pack('CCCC')).first
	rescue SocketError
		hostname = nil
	end
	hostname
end

class IP < ActiveRecord::Base
	attr_accessible :name, :ip
end

get '/' do
	@ip = env['REMOTE_ADDR']
	@hostname = hostname(@ip) if @ip
	@forwarded = env['HTTP_X_FORWARDED_FOR']
	@forwarded_host = hostname(@forwarded) if @forwarded

	erb :'index.html'
end

get '/:host' do |host|
	@host = host
	record = IP.find_by_host(host)
	if record
		@ip = record.ip
		erb :'show.html'
	else
		erb :'unknown.html'
	end
end

post '/:host' do |host|
	record = IP.find_by_host(host)
	unless record
		record = IP.new
		record.host = host
	end

	record.ip = env['REMOTE_ADDR']

	record.save!

	redirect "/#{host}"
end
{'fraction_non_alphanumeric': 0.13968072976054732, 'fraction_numerical': 0.0, 'mean_word_length': 4.55379746835443, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1572831', 'n_tokens_mistral': 708, 'n_tokens_neox': 685, 'n_words': 160}