Module: Rack::DomainFilter::Matcher
- Included in:
- Rack::DomainFilter
- Defined in:
- lib/rack/domain_filter/ext/matcher.rb
Instance Method Summary collapse
- #can_respond_no_match? ⇒ Boolean
- #can_skip_path?(env) ⇒ Boolean
- #match_regex(regex, req, block) ⇒ Object
- #match_string(string, req, block) ⇒ Object
-
#match_uri(env) ⇒ Object
See https://www.youtube.com/watch?v=b77V0rkr5rk Use host_with_port for better performance See: https://github.com/rack/rack/blob/master/lib/rack/request.rb.
- #skip_regex?(regex, req) ⇒ Boolean
- #skip_string?(string, req) ⇒ Boolean
- #trigger_no_match(env) ⇒ Object
Instance Method Details
#can_respond_no_match? ⇒ Boolean
20 21 22 |
# File 'lib/rack/domain_filter/ext/matcher.rb', line 20 def can_respond_no_match? !config.no_match_block.nil? end |
#can_skip_path?(env) ⇒ Boolean
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rack/domain_filter/ext/matcher.rb', line 49 def can_skip_path?(env) req = Rack::Request.new(env) skip_current_request = false config.skip_path_patterns.each do |pattern| break if skip_current_request if pattern.is_a?(String) skip_current_request ||= skip_string?(pattern, req) elsif pattern.is_a?(Regexp) skip_current_request ||= skip_regex?(pattern, req) else raise "Unknown pattern: #{pattern}. It must be a regex or a string!" end end skip_current_request end |
#match_regex(regex, req, block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rack/domain_filter/ext/matcher.rb', line 28 def match_regex(regex, req, block) matchdata = req.host_with_port.match(regex) return if matchdata.nil? block.call(matchdata[1], req.env) @match_found = true true end |
#match_string(string, req, block) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/rack/domain_filter/ext/matcher.rb', line 39 def match_string(string, req, block) return if string != req.host_with_port block.call(req.env) @match_found = true true end |
#match_uri(env) ⇒ Object
See https://www.youtube.com/watch?v=b77V0rkr5rk Use host_with_port for better performance See: https://github.com/rack/rack/blob/master/lib/rack/request.rb
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rack/domain_filter/ext/matcher.rb', line 7 def match_uri(env) req = Rack::Request.new(env) config.uri_mapping.each_pair do |pattern, block| if pattern.is_a?(String) break if match_string(pattern, req, block) elsif pattern.is_a?(Regexp) break if match_regex(pattern, req, block) else raise "Unknown pattern: #{pattern}. It must be a regex or a string!" end end end |
#skip_regex?(regex, req) ⇒ Boolean
65 66 67 |
# File 'lib/rack/domain_filter/ext/matcher.rb', line 65 def skip_regex?(regex, req) !req.path.match(regex).nil? end |
#skip_string?(string, req) ⇒ Boolean
69 70 71 |
# File 'lib/rack/domain_filter/ext/matcher.rb', line 69 def skip_string?(string, req) string == req.path end |
#trigger_no_match(env) ⇒ Object
24 25 26 |
# File 'lib/rack/domain_filter/ext/matcher.rb', line 24 def trigger_no_match(env) config.no_match_block.call(env) end |