<commit_before>require 'sinatra'
require 'json'
require 'config/environment'

class Overheard
  include DataMapper::Resource

  property :id, Serial
  property :body, Text, { :required => true }
  property :created_at, DateTime
end

DataMapper.finalize
DataMapper.auto_upgrade!

helpers do
  def render_json(hash)
    content_type "application/json"
    JSON.dump(hash)
  end

  def request_data
    return @request_data if @request_data
    if request.form_data?
      @request_data = params
    elsif request.content_type == "application/json"
      request.body.rewind
      @request_data = JSON.parse(request.body.read)
    end
  end
end

get '/' do
  @overheards = Overheard.all
  if request.accept?("text/html")
    erb :home
  elsif request.accept?("application/json")
    render_json({ :overheards => @overheards })
  end
end

get '/overheards/new' do
  @overheard = Overheard.new
  erb :new_overheard
end

post '/overheards' do
  @overheard = Overheard.create({ "body" => request_data["overheard"]["body"] })
  status @overheard.saved? ? 200 : 400

  if request.accept?("text/html")
    if @overheard.saved?
      redirect "/"
    else
      erb :new_overheard
    end
  elsif request.accept?("application/json")
    response_json = { "overheard" => @overheard.attributes }
    response_json["overheard"]["errors"] = @overheard.errors.to_h
    render_json(response_json)
  end
end
<commit_msg>Force ordering by created at<commit_after>require 'sinatra'
require 'json'
require 'config/environment'

class Overheard
  include DataMapper::Resource

  property :id, Serial
  property :body, Text, { :required => true }
  property :created_at, DateTime
end

DataMapper.finalize
DataMapper.auto_upgrade!

helpers do
  def render_json(hash)
    content_type "application/json"
    JSON.dump(hash)
  end

  def request_data
    return @request_data if @request_data
    if request.form_data?
      @request_data = params
    elsif request.content_type == "application/json"
      request.body.rewind
      @request_data = JSON.parse(request.body.read)
    end
  end
end

get '/' do
  @overheards = Overheard.all(:order => [:created_at.asc])
  if request.accept?("text/html")
    erb :home
  elsif request.accept?("application/json")
    render_json({ :overheards => @overheards })
  end
end

get '/overheards/new' do
  @overheard = Overheard.new
  erb :new_overheard
end

post '/overheards' do
  @overheard = Overheard.create({ "body" => request_data["overheard"]["body"] })
  status @overheard.saved? ? 200 : 400

  if request.accept?("text/html")
    if @overheard.saved?
      redirect "/"
    else
      erb :new_overheard
    end
  elsif request.accept?("application/json")
    response_json = { "overheard" => @overheard.attributes }
    response_json["overheard"]["errors"] = @overheard.errors.to_h
    render_json(response_json)
  end
end

{'fraction_non_alphanumeric': 0.11832865168539326, 'fraction_numerical': 0.004213483146067416, 'mean_word_length': 3.7962962962962963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '1244207', 'n_tokens_mistral': 982, 'n_tokens_neox': 941, 'n_words': 213}