Class: MailGrabber::Web::ApplicationRouter::Route

Inherits:
Struct
  • Object
show all
Defined in:
lib/mail_grabber/web/application_router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



10
11
12
# File 'lib/mail_grabber/web/application_router.rb', line 10

def block
  @block
end

#patternObject

Returns the value of attribute pattern

Returns:

  • (Object)

    the current value of pattern



10
11
12
# File 'lib/mail_grabber/web/application_router.rb', line 10

def pattern
  @pattern
end

Instance Method Details

#extract_params(path) ⇒ Hash/NilClass

Extract parameters from the given path. All routes have a path pattern which helps to the router to find which block it should execute. If the path contains request parameters like ‘/test/1’ then it will match with the ‘/test/:id’ pattern. In this case, it will return with ‘=> “1”’ hash. If it is just a simple path like ‘/’ and it has a pattern to match, then it will return with ‘{}’. In the other case, it will return with nil.

Parameters:

  • path (String)

Returns:

  • (Hash/NilClass)

    with the extracted parameters, empty Hash or nil



24
25
26
27
28
29
30
31
32
33
# File 'lib/mail_grabber/web/application_router.rb', line 24

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

    path.match(Regexp.new("\\A#{named_pattern}\\Z"))&.named_captures
  elsif path == pattern
    {}
  end
end