<commit_before>module Identity
  class HerokuCookie
    def initialize(app, opts={})
      @app = app

      @domain       = opts[:domain] || raise("missing=domain")
      @expire_after = opts[:expire_after]
      @key          = opts[:key] || raise("missing=key")
    end

    def call(env)
      request = Rack::Request.new(env)
      if nonce = request.cookies["heroku_session_nonce"]
        env[@key] = {
          "nonce" => nonce
        }
      end

      status, headers, response = @app.call(env)

      if env[@key]
        set_cookie(headers, "heroku_session", "1")
        set_cookie(headers, "heroku_session_nonce", env[@key]["nonce"])
      else
        %w(heroku_session heroku_session_nonce).each do |key|
          delete_cookie(headers, key)
        end
      end

      [status, headers, response]
    end

    private

    def delete_cookie(headers, key)
      Rack::Utils.delete_cookie_header!(headers, key,
        domain: @domain,
      )
    end

    def set_cookie(headers, key, value)
      Rack::Utils.set_cookie_header!(headers, key,
        domain:  @domain,
        expires: @expire_after ? Time.now + @expire_after : nil,
        value:   value
      )
    end
  end
end
<commit_msg>Make this code more symmetrical<commit_after>module Identity
  class HerokuCookie
    def initialize(app, opts={})
      @app = app

      @domain       = opts[:domain] || raise("missing=domain")
      @expire_after = opts[:expire_after]
      @key          = opts[:key] || raise("missing=key")
    end

    def call(env)
      request = Rack::Request.new(env)
      if nonce = request.cookies["heroku_session_nonce"]
        env[@key] = {
          "nonce" => nonce
        }
      end

      status, headers, response = @app.call(env)

      if env[@key]
        set_cookie(headers, "heroku_session", "1")
        set_cookie(headers, "heroku_session_nonce", env[@key]["nonce"])
      else
        delete_cookie(headers, "heroku_session")
        delete_cookie(headers, "heroku_session_nonce")
      end

      [status, headers, response]
    end

    private

    def delete_cookie(headers, key)
      Rack::Utils.delete_cookie_header!(headers, key,
        domain: @domain,
      )
    end

    def set_cookie(headers, key, value)
      Rack::Utils.set_cookie_header!(headers, key,
        domain:  @domain,
        expires: @expire_after ? Time.now + @expire_after : nil,
        value:   value
      )
    end
  end
end

{'fraction_non_alphanumeric': 0.12008196721311476, 'fraction_numerical': 0.000819672131147541, 'mean_word_length': 2.3576341127922973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6861422', 'n_tokens_mistral': 790, 'n_tokens_neox': 772, 'n_words': 163}