Class: Vundabar::Routing::Mapper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoints) ⇒ Mapper

Returns a new instance of Mapper.



5
6
7
# File 'lib/vundabar/routing/mapper.rb', line 5

def initialize(endpoints)
  @endpoints = endpoints
end

Instance Attribute Details

#endpointsObject (readonly)

Returns the value of attribute endpoints.



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

def endpoints
  @endpoints
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

Instance Method Details

#find_route(request) ⇒ Object



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

def find_route(request)
  @request = request
  path = request.path_info
  method = request.request_method.downcase.to_sym
  endpoints[method].detect do |endpoint|
    match_path_with_endpoint path, endpoint
  end
end

#match_path_with_endpoint(path, endpoint) ⇒ Object



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

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