class Ballast::RequestDomainMatcher

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 manipulate the request host before trying the match. See `String#gsub`.

@attribute replace_string

@return [String] A string to manipulate the request host before trying the match. See `String#gsub`.

@attribute replace_block

@return [Proc] A block to use to manipulate the request host before trying the match. See `String#gsub`.

Attributes

domains[RW]
replace_block[RW]
replace_pattern[RW]
replace_string[RW]

Public Class Methods

new(domains, replace_pattern = /\.dev$/, replace_string = "", &replace_block) click to toggle source

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`.

# 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

Public Instance Methods

matches?(request) click to toggle source

Matches a request.

@param request [ActionDispatch::Request] The request to match. @return [Boolean] `true` if the request matches, `false` otherwise.

# 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