Class: HttpFake::Router

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

Overview

Matches an incoming (method, path) pair against a list of Route objects. Supports :param segments in patterns, e.g. “/v1/charges/:id”.

Defined Under Namespace

Classes: Match

Instance Method Summary collapse

Constructor Details

#initialize(routes) ⇒ Router

Returns a new instance of Router.



9
10
11
# File 'lib/httpfake/router.rb', line 9

def initialize(routes)
  @routes = routes
end

Instance Method Details

#match(method, path, scenario: nil) ⇒ Object

Returns a Match or nil.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/httpfake/router.rb', line 14

def match(method, path, scenario: nil)
  http_method = method.to_s.upcase
  @routes.each do |route|
    next unless route.method == http_method
    next unless route.scenario == scenario

    captures = extract_params(route.pattern, path)
    next if captures.nil?

    return Match.new(route: route, params: captures)
  end
  nil
end