Class: Rollerskates::Routing::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rollerskates/routing/mapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(endpoints) ⇒ Mapper

Returns a new instance of Mapper.



4
5
6
# File 'lib/rollerskates/routing/mapper.rb', line 4

def initialize(endpoints)
  @endpoints = endpoints
end

Instance Method Details

#map_to_route(request) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/rollerskates/routing/mapper.rb', line 8

def map_to_route(request)
  @request = request
  path = request.path_info
  path[-1] = "" if path[-1] == "/" && path != "/"
  method = request.request_method.downcase.to_sym
  result = @endpoints[method].detect do |endpoint|
    match_path_with_pattern path, endpoint
  end
  return Route.new(@request, result[:klass_and_method]) if result
end

#match_path_with_pattern(path, endpoint) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/rollerskates/routing/mapper.rb', line 19

def match_path_with_pattern(path, endpoint)
  regexp, placeholders = endpoint[:pattern]
  if path =~ regexp
    match_data = Regexp.last_match
    placeholders.each do |placeholder|
      @request.update_param(placeholder, match_data[placeholder])
    end
    true
  end
end