Class: Tennpipes::PathRouter::Matcher
- Inherits:
-
Object
- Object
- Tennpipes::PathRouter::Matcher
- Defined in:
- lib/tennpipes-base/path_router/matcher.rb
Constant Summary collapse
- GROUP_REGEXP =
To count group of regexp
%r{\((?!\?:|\?!|\?<=|\?<!|\?=).+?\)}.freeze
Instance Method Summary collapse
-
#capture_length ⇒ Object
Returns captures parameter length.
-
#expand(params) ⇒ Object
Expands the path by using parameters.
-
#handler ⇒ Object
Returns the handler which is an instance of Mustermann or Regexp.
-
#initialize(path, options = {}) ⇒ Matcher
constructor
Constructs an instance of PathRouter::Matcher.
-
#match(pattern) ⇒ Object
Matches a pattern with the route matcher.
-
#mustermann? ⇒ Boolean
Returns true if handler is an instance of Mustermann.
-
#names ⇒ Object
Returns names of the handler.
-
#params_for(pattern, others) ⇒ Object
Builds a parameters, and returns them.
-
#to_regexp ⇒ Object
Returns a regexp from handler.
-
#to_s ⇒ Object
Converts the handler into string.
Constructor Details
#initialize(path, options = {}) ⇒ Matcher
Constructs an instance of PathRouter::Matcher.
12 13 14 15 16 |
# File 'lib/tennpipes-base/path_router/matcher.rb', line 12 def initialize(path, = {}) @path = path.is_a?(String) && path.empty? ? "/" : path @capture = [:capture] @default_values = [:default_values] end |
Instance Method Details
#capture_length ⇒ Object
Returns captures parameter length.
105 106 107 108 109 110 111 |
# File 'lib/tennpipes-base/path_router/matcher.rb', line 105 def capture_length if mustermann? handler.named_captures.inject(0) { |count, (_, capture)| count += capture.length } else handler.inspect.scan(GROUP_REGEXP).length end end |
#expand(params) ⇒ Object
Expands the path by using parameters.
39 40 41 42 43 |
# File 'lib/tennpipes-base/path_router/matcher.rb', line 39 def (params) params = params.merge(@default_values) if @default_values.is_a?(Hash) = handler.(:append, params) end |
#handler ⇒ Object
Returns the handler which is an instance of Mustermann or Regexp.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/tennpipes-base/path_router/matcher.rb', line 75 def handler @handler ||= case @path when String Mustermann.new(@path, :capture => @capture) when Regexp /^(?:#{@path})$/ else @path end end |
#match(pattern) ⇒ Object
Matches a pattern with the route matcher.
21 22 23 24 25 26 27 |
# File 'lib/tennpipes-base/path_router/matcher.rb', line 21 def match(pattern) if match_data = handler.match(pattern) match_data elsif mustermann? && pattern != "/" && pattern.end_with?("/") handler.match(pattern[0..-2]) end end |
#mustermann? ⇒ Boolean
Returns true if handler is an instance of Mustermann.
48 49 50 |
# File 'lib/tennpipes-base/path_router/matcher.rb', line 48 def mustermann? handler.instance_of?(Mustermann::Sinatra) end |
#names ⇒ Object
Returns names of the handler.
98 99 100 |
# File 'lib/tennpipes-base/path_router/matcher.rb', line 98 def names handler.names end |
#params_for(pattern, others) ⇒ Object
Builds a parameters, and returns them.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/tennpipes-base/path_router/matcher.rb', line 55 def params_for(pattern, others) data = match(pattern) params = indifferent_hash if data.names.empty? params.merge!(:captures => data.captures) unless data.captures.empty? else if mustermann? new_params = handler.params(pattern, :captures => data) params.merge!(new_params) if new_params elsif data params.merge!(Hash[names.zip(data.captures)]) end params.merge!(others){ |_, old, new| old || new } end params end |
#to_regexp ⇒ Object
Returns a regexp from handler.
32 33 34 |
# File 'lib/tennpipes-base/path_router/matcher.rb', line 32 def to_regexp mustermann? ? handler.to_regexp : handler end |
#to_s ⇒ Object
Converts the handler into string.
90 91 92 |
# File 'lib/tennpipes-base/path_router/matcher.rb', line 90 def to_s handler.to_s end |