Class: Ballast::RequestDomainMatcher
- Inherits:
-
Object
- Object
- Ballast::RequestDomainMatcher
- Defined in:
- lib/ballast/request_domain_matcher.rb
Overview
A small class to match requests basing on the domain.
Instance Attribute Summary (collapse)
-
- (Array) domains
The list of domains which mark a positive match.
-
- (Proc) replace_block
A block to use for replacement in the request host.
-
- (String|Regexp) replace_pattern
A optional pattern to replace in the request host.
-
- (String) replace_string
A string to use for replacement in the request host.
Instance Method Summary (collapse)
-
- (RequestDomainMatcher) initialize(domains, replace_pattern = /\.dev$/, replace_string = "", &replace_block)
constructor
Creates a new matcher.
-
- (Boolean) matches?(request)
Matches a request.
Constructor Details
- (RequestDomainMatcher) initialize(domains, replace_pattern = /\.dev$/, replace_string = "", &replace_block)
Creates a new matcher.
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
- (Array) domains
Returns 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 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 |
- (Proc) replace_block
Returns A block to use for replacement in the request host. 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 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 |
- (String|Regexp) replace_pattern
Returns A optional pattern to replace in the request host. 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 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 |
- (String) replace_string
Returns A string to use for replacement in the request host. 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 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 |
Instance Method Details
- (Boolean) matches?(request)
Matches a request.
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 |