<commit_before>require 'app/controllers/api_controller'
require 'app/models/owner'
require 'app/models/session'

require 'active_support/core_ext/hash/slice'

module Pod
  module TrunkApp
    class SessionsController < APIController
      post '/', :requires_owner => false do
        owner_params = JSON.parse(request.body.read)
        DB.test_safe_transaction do
          owner_email, owner_name = owner_params.values_at('email', 'name')
          owner = Owner.find_or_create_by_email_and_update_name(owner_email, owner_name)
          session = owner.create_session!(url('/sessions/verify/%s'))
          session_attributes = session.public_attributes
          session_attributes['token'] = session.token
          json_message(201, session_attributes)
        end
      end

      get '/verify/:token', :requires_owner => false do
        if session = Session.with_verification_token(params[:token])
          session.verify!
          json_message(200, session)
        else
          json_error(404, 'Session not found.')
        end
      end

      get '/', :requires_owner => true do
        owner_attributes = @owner.public_attributes
        owner_attributes['sessions'] = @owner.sessions.map(&:public_attributes)
        json_message(200, owner_attributes)
      end

      delete '/', :requires_owner => true do
        @owner.sessions.each do |session|
          next if session.id == @session.id
          session.destroy<commit_msg>[Session] Construct verification URL manually to not re-use SCRIPT_NAME.<commit_after>require 'app/controllers/api_controller'
require 'app/models/owner'
require 'app/models/session'

require 'active_support/core_ext/hash/slice'

module Pod
  module TrunkApp
    class SessionsController < APIController
      post '/', :requires_owner => false do
        owner_params = JSON.parse(request.body.read)
        DB.test_safe_transaction do
          owner_email, owner_name = owner_params.values_at('email', 'name')
          owner = Owner.find_or_create_by_email_and_update_name(owner_email, owner_name)
          session = owner.create_session!("#{request.scheme}://#{request.host_with_port}/sessions/verify/%s")
          session_attributes = session.public_attributes
          session_attributes['token'] = session.token
          json_message(201, session_attributes)
        end
      end

      get '/verify/:token', :requires_owner => false do
        if session = Session.with_verification_token(params[:token])
          session.verify!
          json_message(200, session)
        else
          json_error(404, 'Session not found.')
        end
      end

      get '/', :requires_owner => true do
        owner_attributes = @owner.public_attributes
        owner_attributes['sessions'] = @owner.sessions.map(&:public_attributes)
        json_message(200, owner_attributes)
      end

      delete '/', :requires_owner => true do
        @owner.sessions.each do |session|
          next if session.id == @session.id
          session.destroy
{'fraction_non_alphanumeric': 0.10023310023310024, 'fraction_numerical': 0.007992007992007992, 'mean_word_length': 2.8962386511024643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '401074', 'n_tokens_mistral': 862, 'n_tokens_neox': 831, 'n_words': 169}