Sha256: a8a7c667b4136adc97a34f77ac513197b30a885d5848c3ac2bbaf88991ba144c

Contents?: true

Size: 1.92 KB

Versions: 17

Compression:

Stored size: 1.92 KB

Contents

#
# This file is part of the ballast gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
#

module Ballast
  # A small class to match requests basing on the domain.
  #
  # @attribute domains
  #   @return [Array] The list of domains which mark a positive match.
  # @attribute replace_pattern
  #   @return [String|Regexp] A optional pattern to replace in the request host. See `String#gsub`.
  # @attribute replace_string
  #   @return [String] A string to use for replacement in the request host. See `String#gsub`.
  # @attribute replace_block
  #   @return [Proc] A block to use for replacement in the request host. See `String#gsub`.
  class RequestDomainMatcher
    attr_accessor :domains, :replace_pattern, :replace_string, :replace_block

    # Creates a new matcher.
    #
    # @param domains [String|Array] The list of domains which mark a positive match.
    # @param replace_pattern [String|Regexp] A optional pattern to replace in the request host. See `String#gsub`.
    # @param replace_string [String] A string to use for replacement in the request host. See `String#gsub`.
    # @param replace_block [Proc] A block to use for replacement in the request host. See `String#gsub`.
    def initialize(domains, replace_pattern = /\.dev$/, replace_string = "", &replace_block)
      @domains = domains.ensure_array
      @replace_pattern = replace_pattern
      @replace_string = replace_string
      @replace_block = replace_block
    end

    # Matches a request.
    #
    # @param request [ActionDispatch::Request] The request to match.
    # @return [Boolean] `true` if the request matches, `false` otherwise.
    def matches?(request)
      final_host = @replace_block ? request.host.gsub(@replace_pattern, &@replace_block) : request.host.gsub(@replace_pattern, @replace_string)
      @domains.include?(final_host)
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ballast-1.9.3 lib/ballast/request_domain_matcher.rb
ballast-1.9.2 lib/ballast/request_domain_matcher.rb
ballast-1.9.1 lib/ballast/request_domain_matcher.rb
ballast-1.9.0 lib/ballast/request_domain_matcher.rb
ballast-1.8.0 lib/ballast/request_domain_matcher.rb
ballast-1.7.0 lib/ballast/request_domain_matcher.rb
ballast-1.6.0 lib/ballast/request_domain_matcher.rb
ballast-1.5.3 lib/ballast/request_domain_matcher.rb
ballast-1.5.2 lib/ballast/request_domain_matcher.rb
ballast-1.5.1 lib/ballast/request_domain_matcher.rb
ballast-1.5.0 lib/ballast/request_domain_matcher.rb
ballast-1.4.0 lib/ballast/request_domain_matcher.rb
ballast-1.3.0 lib/ballast/request_domain_matcher.rb
ballast-1.2.0 lib/ballast/request_domain_matcher.rb
ballast-1.1.2 lib/ballast/request_domain_matcher.rb
ballast-1.1.1 lib/ballast/request_domain_matcher.rb
ballast-1.1.0 lib/ballast/request_domain_matcher.rb