<commit_before>require 'rubygems'
require 'bundler/setup'
require 'rubyXL'
require 'mail'
require 'json'
require 'sinatra/base'


module QuestOn
  class Application < Sinatra::Base
    # Startpage
    get '/' do
      content_type "text/html"
      File.read("web/index.html")
    end

    get '/index.html' do
      content_type "text/html"
      File.read("web/index.html")
    end
    
    get '/*.js' do |name|
      content_type "text/javascript"
      File.read("web/js/#{name}.js")
    end

    # CSS
    get '/*.css' do |name|
      content_type "text/css"
      File.read("web/css/#{name}.css")
    end

    # Bilder
    get '/*.jpg' do |name|
      sleep 0.3
      content_type "image/jpeg"
      File.read("web/img/#{name}.jpg")
    end
    get '/*.png' do |name|
      content_type "image/png"
      File.read("web/img/#{name}.png")
    end

    # mail configuration
    options = { :address              => ENV["SMPT_SERVER"],
                :port                 => 587,
                :domain               => ENV["SMTP_DOMAIN"],
                :user_name            => ENV["SMPT_USERNAME"],
                :password             => ENV["<PASSWORD>"],
                :authentication       => 'plain',
                :enable_starttls_auto => true  }

    Mail.defaults do
      delivery_method :smtp, options
    end


    # store!
    post '/store' do
      results = JSON.parse(params["results"])
  
      temp_file = "tmp/" + (rand*10**40).to_i.to_s(36) + ".xlsx"
  
      # excel
      workbook  = RubyXL::Workbook.new
      worksheet = workbook[0]
      row = 0
      results.each do |result|
        worksheet.add_cell(row, 0, result["id"])
        worksheet.add_cell(row, 1, result["value"])
        row += 1
      end
      worksheet.change_column_bold(0, true)
      workbook.write(temp_file)
  
      # email
      message = Mail.new do 
        from     ENV["SOURCE_EMAIL"]
        to       ENV["DESTINATION_EMAIL"]
        subject  'Neues Umfrageresultat'
        body     'Weitere Informationen in den angehängten Dateien.'
      end
      message.attachments["werte.xlsx"] = File.read(temp_file)
      message.deliver
  
      File.unlink temp_file
  <commit_msg>Unescessary file creation removed.<commit_after>require 'rubygems'
require 'bundler/setup'
require 'rubyXL'
require 'mail'
require 'json'
require 'sinatra/base'


module QuestOn
  class Application < Sinatra::Base
    # Startpage
    get '/' do
      content_type "text/html"
      File.read("web/index.html")
    end

    get '/index.html' do
      content_type "text/html"
      File.read("web/index.html")
    end
    
    get '/*.js' do |name|
      content_type "text/javascript"
      File.read("web/js/#{name}.js")
    end

    # CSS
    get '/*.css' do |name|
      content_type "text/css"
      File.read("web/css/#{name}.css")
    end

    # Bilder
    get '/*.jpg' do |name|
      sleep 0.3
      content_type "image/jpeg"
      File.read("web/img/#{name}.jpg")
    end
    get '/*.png' do |name|
      content_type "image/png"
      File.read("web/img/#{name}.png")
    end

    # mail configuration
    options = { :address              => ENV["SMPT_SERVER"],
                :port                 => 587,
                :domain               => ENV["SMTP_DOMAIN"],
                :user_name            => ENV["SMPT_USERNAME"],
                :password             => ENV["<PASSWORD>"],
                :authentication       => 'plain',
                :enable_starttls_auto => true  }

    Mail.defaults do
      delivery_method :smtp, options
    end


    # store!
    post '/store' do
      results = JSON.parse(params["results"])
      
      # excel
      workbook  = RubyXL::Workbook.new
      worksheet = workbook[0]
      row = 0
      results.each do |result|
        worksheet.add_cell(row, 0, result["id"])
        worksheet.add_cell(row, 1, result["value"])
        row += 1
      end
      worksheet.change_column_bold(0, true)
      
      # email
      message = Mail.new do 
        from     ENV["SOURCE_EMAIL"]
        to       ENV["DESTINATION_EMAIL"]
        subject  'Neues Umfrageresultat'
        body     'Weitere Informationen in den angehängten Dateien.'
      end
      message.attachments["werte.xlsx"] = workbook.stream.read
      message.deliver
    
{'fraction_non_alphanumeric': 0.12861136999068035, 'fraction_numerical': 0.0065237651444548, 'mean_word_length': 1.8206307490144547, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '2836304', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1377, 'n_words': 288}