Class: Ballast::RequestDomainMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ballast/request_domain_matcher.rb

Overview

A small class to match requests basing on the domain.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domains, replace_pattern = /\.dev$/, replace_string = "", &replace_block) ⇒ RequestDomainMatcher

Creates a new matcher.

Parameters:

  • domains (String|Array)

    The list of domains which mark a positive match.

  • replace_pattern (String|Regexp) (defaults to: /\.dev$/)

    A optional pattern to manipulate the request host before trying the match. See String#gsub.

  • replace_string (String) (defaults to: "")

    A string to manipulate the request host before trying the match. See String#gsub.

  • replace_block (Proc)

    A block to use to manipulate the request host before trying the match. See String#gsub.



26
27
28
29
30
31
# File 'lib/ballast/request_domain_matcher.rb', line 26

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

Instance Attribute Details

#domainsArray

Returns The list of domains which mark a positive match.

Returns:

  • (Array)

    The list of domains which mark a positive match.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ballast/request_domain_matcher.rb', line 17

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 manipulate the request host before trying the match. See `String#gsub`.
  # @param replace_string [String] A string to manipulate the request host before trying the match. See `String#gsub`.
  # @param replace_block [Proc] A block to use to manipulate the request host before trying the match. 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

#replace_blockProc

Returns A block to use to manipulate the request host before trying the match. See String#gsub.

Returns:

  • (Proc)

    A block to use to manipulate the request host before trying the match. See String#gsub.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ballast/request_domain_matcher.rb', line 17

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 manipulate the request host before trying the match. See `String#gsub`.
  # @param replace_string [String] A string to manipulate the request host before trying the match. See `String#gsub`.
  # @param replace_block [Proc] A block to use to manipulate the request host before trying the match. 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

#replace_patternString|Regexp

Returns A optional pattern to manipulate the request host before trying the match. See String#gsub.

Returns:

  • (String|Regexp)

    A optional pattern to manipulate the request host before trying the match. See String#gsub.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ballast/request_domain_matcher.rb', line 17

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 manipulate the request host before trying the match. See `String#gsub`.
  # @param replace_string [String] A string to manipulate the request host before trying the match. See `String#gsub`.
  # @param replace_block [Proc] A block to use to manipulate the request host before trying the match. 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

#replace_stringString

Returns A string to manipulate the request host before trying the match. See String#gsub.

Returns:

  • (String)

    A string to manipulate the request host before trying the match. See String#gsub.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ballast/request_domain_matcher.rb', line 17

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 manipulate the request host before trying the match. See `String#gsub`.
  # @param replace_string [String] A string to manipulate the request host before trying the match. See `String#gsub`.
  # @param replace_block [Proc] A block to use to manipulate the request host before trying the match. 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

Instance Method Details

#matches?(request) ⇒ Boolean

Matches a request.

Parameters:

  • request (ActionDispatch::Request)

    The request to match.

Returns:

  • (Boolean)

    true if the request matches, false otherwise.



37
38
39
40
# File 'lib/ballast/request_domain_matcher.rb', line 37

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