Class: Mailpeek::WebRoute

Inherits:
Object
  • Object
show all
Defined in:
lib/mailpeek/web/router.rb

Overview

Public: WebRoute

Constant Summary collapse

NAMED_SEGMENTS_PATTERN =
%r{\/([^\/]*):([^\.:$\/]+)}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_method, pattern, block) ⇒ WebRoute

Returns a new instance of WebRoute.



75
76
77
78
79
# File 'lib/mailpeek/web/router.rb', line 75

def initialize(request_method, pattern, block)
  @request_method = request_method
  @pattern = pattern
  @block = block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



71
72
73
# File 'lib/mailpeek/web/router.rb', line 71

def block
  @block
end

#nameObject

Returns the value of attribute name.



71
72
73
# File 'lib/mailpeek/web/router.rb', line 71

def name
  @name
end

#patternObject

Returns the value of attribute pattern.



71
72
73
# File 'lib/mailpeek/web/router.rb', line 71

def pattern
  @pattern
end

#request_methodObject

Returns the value of attribute request_method.



71
72
73
# File 'lib/mailpeek/web/router.rb', line 71

def request_method
  @request_method
end

Instance Method Details

#compileObject



85
86
87
88
89
90
91
92
93
# File 'lib/mailpeek/web/router.rb', line 85

def compile
  if pattern.match(NAMED_SEGMENTS_PATTERN)
    p = pattern.gsub(NAMED_SEGMENTS_PATTERN, '/\1(?<\2>[^$/]+)')

    Regexp.new("\\A#{p}\\Z")
  else
    pattern
  end
end

#match(_request_method, path) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mailpeek/web/router.rb', line 95

def match(_request_method, path)
  case matcher
  when String
    {} if path == matcher
  else
    path_match = path.match(matcher)
    if path_match
      Hash[path_match.names.map(&:to_sym).zip(path_match.captures)]
    end
  end
end

#matcherObject



81
82
83
# File 'lib/mailpeek/web/router.rb', line 81

def matcher
  @matcher ||= compile
end