Class: Rasti::Web::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/rasti/web/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, endpoint = nil, &block) ⇒ Route

Returns a new instance of Route.



7
8
9
10
11
# File 'lib/rasti/web/route.rb', line 7

def initialize(pattern, endpoint=nil, &block)
  @pattern = normalize pattern
  @endpoint = endpoint || Endpoint.new(&block)
  @regexp = compile
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



5
6
7
# File 'lib/rasti/web/route.rb', line 5

def pattern
  @pattern
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
# File 'lib/rasti/web/route.rb', line 22

def call(env)
  @endpoint.call env
end

#extract_params(path) ⇒ Object



17
18
19
20
# File 'lib/rasti/web/route.rb', line 17

def extract_params(path)
  result = @regexp.match normalize(path)
  result ? result.names.each_with_object(Hash::Indifferent.new) { |v,h| h[v] = result[v] } : {}
end

#match?(path) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/rasti/web/route.rb', line 13

def match?(path)
  !@regexp.match(normalize(path)).nil?
end