Sha256: 295e7714d610bf6686ca975687bbcadbab2a7474699f700297e3402575ccc318

Contents?: true

Size: 964 Bytes

Versions: 6

Compression:

Stored size: 964 Bytes

Contents

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

      def parse_database_name(request)
        database = subdomain(request.host)

        database.present? && database || nil
      end

    private

      # *Almost* a direct ripoff of ActionDispatch::Request subdomain methods

      # Only care about the first subdomain for the database name
      def subdomain(host)
        subdomains(host).first
      end

      #   Assuming tld_length of 1, might need to make this configurable in Apartment in the future for things like .co.uk
      def subdomains(host, tld_length = 1)
        return [] unless named_host?(host)

        host.split('.')[0..-(tld_length + 2)]
      end

      def named_host?(host)
        !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
apartment-0.22.1 lib/apartment/elevators/subdomain.rb
apartment-0.22.0 lib/apartment/elevators/subdomain.rb
apartment-0.21.1 lib/apartment/elevators/subdomain.rb
apartment-0.21.0 lib/apartment/elevators/subdomain.rb
apartment-0.20.0 lib/apartment/elevators/subdomain.rb
apartment-0.19.0 lib/apartment/elevators/subdomain.rb