<commit_before>module Apartment
  module Elevators
    # Provides a rack based db switching solution based on subdomains
    # Assumes that database name should match subdomain
    class Subdomain

      def initialize(app)
        @app = app
      end

      def call(env)
        request = ActionDispatch::Request.new(env)

        Rails.logger.error "  Requested URL: #{request.url}"
        database = subdomain(request)

        if database
          #switch database
          begin
            database.gsub! '-', '_'

            Apartment::Tenant.switch database

            Rails.logger.error "  Using database '#{database}'"

            #set image location
            # Spree::Image.change_paths database

            #namespace cache keys
            ENV['RAILS_CACHE_ID']= database

            #reset Mail settings
            Spree::Core::MailSettings.init
          rescue Exception => e
            Rails.logger.error "  Stopped request due to: #{e.message}"

            #fallback
            ENV['RAILS_CACHE_ID'] = ""
            Apartment::Tenant.switch nil
            ActiveRecord::Base.establish_connection
            return ahh_no
          end

          #continue on to rails
          @app.call(env)
        else
          ahh_no
        end
      end

      def subdomain(request)
        request.subdomain.to_s.split('.').first
      end
<commit_msg>Remove defunkt mail settings.<commit_after>module Apartment
  module Elevators
    # Provides a rack based db switching solution based on subdomains
    # Assumes that database name should match subdomain
    class Subdomain

      def initialize(app)
        @app = app
      end

      def call(env)
        request = ActionDispatch::Request.new(env)

        Rails.logger.error "  Requested URL: #{request.url}"
        database = subdomain(request)

        if database
          #switch database
          begin
            database.gsub! '-', '_'

            Apartment::Tenant.switch database

            Rails.logger.error "  Using database '#{database}'"

            #set image location
            # Spree::Image.change_paths database

            #namespace cache keys
            ENV['RAILS_CACHE_ID']= database
          rescue Exception => e
            Rails.logger.error "  Stopped request due to: #{e.message}"

            #fallback
            ENV['RAILS_CACHE_ID'] = ""
            Apartment::Tenant.switch nil
            ActiveRecord::Base.establish_connection
            return ahh_no
          end

          #continue on to rails
          @app.call(env)
        else
          ahh_no
        end
      end

      def subdomain(request)
        request.subdomain.to_s.split('.').first
      end

{'fraction_non_alphanumeric': 0.07794606575544884, 'fraction_numerical': 0.0, 'mean_word_length': 1.7270896273917422, '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': '3384359', 'n_tokens_mistral': 727, 'n_tokens_neox': 696, 'n_words': 187}