<commit_before>defmodule Weber.Session do
  
  @moduledoc """
  Weber session handler. Every process handle
  one use session.
  """

  use GenServer.Behaviour

  defrecord Session,
    session_life_time: nil,
    session_id:  nil

  def start_link(session_life_time, session_id) do
    :gen_server.start_link(__MODULE__, [session_life_time, session_id], [])
  end

  def init([session_life_time, session_id]) do
    :timer.send_after(session_life_time * 1000, :erlang.self, {:time_to_die, session_id})
    { :ok, Session.new session_life_time: session_life_time, session_id: session_id }
  end

  def handle_info({:time_to_die, session_id}, state) do
    :ets.delete(:cookie_storage, session_id)
    {:stop, :normal, state}
  end

end<commit_msg>[Session] API for sessions added<commit_after>defmodule Weber.Session do
  
  @moduledoc """
  Weber session handler. Every process handle
  one use session.
  """

  use GenServer.Behaviour

  import Weber.Http.Params

  defrecord Session,
    session_life_time: nil,
    session_id:  nil

  def start_link(session_life_time, session_id) do
    :gen_server.start_link(__MODULE__, [session_life_time, session_id], [])
  end

  def init([session_life_time, session_id]) do
    :timer.send_after(session_life_time * 1000, :erlang.self, {:time_to_die, session_id})
    { :ok, Session.new session_life_time: session_life_time, session_id: session_id }
  end

  def handle_info({:time_to_die, session_id}, state) do
    :ets.delete(:cookie_storage, session_id)
    {:stop, :normal, state}
  end

  def get_session do
    case get_session_helper() do
      [] -> []
      [{_, _, opts}] -> opts
    end
  end

  def get_session_val(key) do
    case get_session_helper() do
      [] -> []
      [{_, _, opts}] -> 
        val = :lists.keyfind(key, 1, opts)
        case val do
          false -> []
          {_, v} -> v
        end
    end

  end

  def set_session_val(key, val) do
    case get_session_helper() do
      [] -> []
      [{s, pid, opts}] ->
        :ets.delete(:cookie_storage, s)
        :ets.insert(:cookie_storage, {s, pid, [{key, val} | opts]})
        :ok
    end
  end

  defp get_session_helper do
    cookie = get_cookie("weber")

    case cookie do
      :undefined -> []
      _ ->
        sessions_list = :ets.tab2list(:cookie_storage)
        Enum.filter(sessions_list, 
          fn({s, _, _}) ->
            :erlang.list_to_binary(:lists.concat(:lists.concat(:erlang.binary_to_list(s)))) == cookie
          end)
    end

  end

end
{'fraction_non_alphanumeric': 0.11905713144226927, 'fraction_numerical': 0.003995205753096284, 'mean_word_length': 3.0128205128205128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, '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': '3684915', 'n_tokens_mistral': 884, 'n_tokens_neox': 845, 'n_words': 188}