Class: Meta::PathMatching

Inherits:
Object
  • Object
show all
Defined in:
lib/meta/application/path_matching_mod.rb

Instance Method Summary collapse

Constructor Details

#initialize(path_schema, matching_mode) ⇒ PathMatching

Returns a new instance of PathMatching.



21
22
23
24
25
26
27
# File 'lib/meta/application/path_matching_mod.rb', line 21

def initialize(path_schema, matching_mode)
  raw_regex = path_schema
                .gsub(/:(\w+)/, '(?<\1>[^/]+)')
                .gsub(/\*(\w+)/, '(?<\1>.+)')
                .gsub(/:/, '[^/]+').gsub('*', '.+')
  @path_matching_regex = matching_mode == :prefix ? Regexp.new("^#{raw_regex}") : Regexp.new("^#{raw_regex}$")
end

Instance Method Details

#capture_both(real_path) ⇒ Object



39
40
41
42
43
# File 'lib/meta/application/path_matching_mod.rb', line 39

def capture_both(real_path)
  real_path = '' if real_path == '/'
  m = @path_matching_regex.match(real_path)
  [m.named_captures, m.post_match]
end

#capture_named_params(real_path) ⇒ Object



45
46
47
# File 'lib/meta/application/path_matching_mod.rb', line 45

def capture_named_params(real_path)
  capture_both(real_path)[0]
end

#capture_remaining_part(real_path) ⇒ Object



49
50
51
# File 'lib/meta/application/path_matching_mod.rb', line 49

def capture_remaining_part(real_path)
  capture_both(real_path)[1]
end

#match?(real_path) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/meta/application/path_matching_mod.rb', line 29

def match?(real_path)
  @path_matching_regex.match?(real_path)
end

#merge_path_params(path, request) ⇒ Object



33
34
35
36
37
# File 'lib/meta/application/path_matching_mod.rb', line 33

def merge_path_params(path, request)
  path_params, remaining_path = capture_both(path)
  path_params.each { |name, value| request.update_param(name, value) }
  remaining_path
end