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
-
#domains ⇒ Array
The list of domains which mark a positive match.
-
#replace_block ⇒ Proc
A block to use to manipulate the request host before trying the match.
-
#replace_pattern ⇒ String|Regexp
A optional pattern to manipulate the request host before trying the match.
-
#replace_string ⇒ String
A string to manipulate the request host before trying the match.
Instance Method Summary collapse
-
#initialize(domains, replace_pattern = /\.dev$/, replace_string = "", &replace_block) ⇒ RequestDomainMatcher
constructor
Creates a new matcher.
-
#matches?(request) ⇒ Boolean
Matches a request.
Constructor Details
#initialize(domains, replace_pattern = /\.dev$/, replace_string = "", &replace_block) ⇒ RequestDomainMatcher
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
#domains ⇒ Array
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 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_block ⇒ Proc
Returns 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_pattern ⇒ String|Regexp
Returns 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_string ⇒ String
Returns 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.
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 |