<commit_before>require 'rubygems'
require 'bundler/setup'
Bundler.require :default, (ENV['RACK_ENV'] || :development)

require './update'

configure do
  MONGO = Mongo::Connection.
    from_uri(ENV['MONGO_URI'] || 'mongodb://localhost:27017').
    db(ENV['MONGO_DB'] || 'hnd')['hnd']

  # This is kind of hacky, but lets us do everything in a free Heroku dyno
  UPDATE_QUEUE = GirlFriday::WorkQueue.new(:update, :size => 1) do |msg|
    sleep 60
    Update.update! MONGO
    UPDATE_QUEUE << {}
  end

  UPDATE_QUEUE << {}
end

get '/' do
  redirect 'http://mfairley.com/hnd', 301
end

get '/:hash' do
  result = MONGO.find(:hash => params[:hash]).to_a
  result.empty? ? 404 : result.map{|i| i['_id'].to_i.to_s}.join(',')
end<commit_msg>Add json/jsonp service to enable cross-site usage.<commit_after>require 'rubygems'
require 'bundler/setup'
Bundler.require :default, (ENV['RACK_ENV'] || :development)

require './update'

configure do
  MONGO = Mongo::Connection.
    from_uri(ENV['MONGO_URI'] || 'mongodb://localhost:27017').
    db(ENV['MONGO_DB'] || 'hnd')['hnd']

  # This is kind of hacky, but lets us do everything in a free Heroku dyno
  UPDATE_QUEUE = GirlFriday::WorkQueue.new(:update, :size => 1) do |msg|
    sleep 60
    Update.update! MONGO
    UPDATE_QUEUE << {}
  end

  UPDATE_QUEUE << {}
end

get '/' do
  redirect 'http://mfairley.com/hnd', 301
end

get '/:hash' do
  result = MONGO.find(:hash => params[:hash]).to_a
  result.empty? ? 404 : result.map{|i| i['_id'].to_i.to_s}.join(',')
end

get '/json/:hash' do
  result = MONGO.find(:hash => params[:hash]).to_a
  result.empty? ? 404 : result.map{|i| i['_id'].to_i.to_s}.to_a

  # JSONP code adapted from https://gist.github.com/446278
  callback = params['callback']
  json = { :result => result }.to_json

  content_type(callback ? :js : :json)
  response = callback ? "#{callback}(#{json})" : json

  response
end
{'fraction_non_alphanumeric': 0.17541070482246954, 'fraction_numerical': 0.0196078431372549, 'mean_word_length': 4.007957559681698, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 1, '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': '2150990', 'n_tokens_mistral': 758, 'n_tokens_neox': 687, 'n_words': 163}