Class: Mountapi::Router
- Inherits:
-
Object
- Object
- Mountapi::Router
- Defined in:
- lib/mountapi/router.rb
Overview
the router hold routes allow to add and find route for a request Internaly the routes are store in hash to optimise search The deep hash store routes by version / method / fragment_count
Class Method Summary collapse
-
.url_parts(url) ⇒ Object
extract version from url.
Instance Method Summary collapse
- #add(route) ⇒ Object
- #find_route(url, http_method) ⇒ Object
-
#initialize(routes = []) ⇒ Router
constructor
A new instance of Router.
-
#routes ⇒ Object
Routes list.
-
#url_for(operation_id, *url_params) ⇒ Object
Return an url for the operation_id.
Constructor Details
#initialize(routes = []) ⇒ Router
9 10 11 |
# File 'lib/mountapi/router.rb', line 9 def initialize(routes = []) routes.each { |route| add(route) } end |
Class Method Details
.url_parts(url) ⇒ Object
extract version from url
29 30 31 32 33 34 35 36 37 |
# File 'lib/mountapi/router.rb', line 29 def self.url_parts(url) url_parts = url.split("/") if Mountapi.config.version_in_path root, version, *url_tail = url_parts {version: version, segments: url_tail.unshift(root)} else {version: '0.0.0', segments: url_parts} end end |
Instance Method Details
#add(route) ⇒ Object
13 14 15 16 |
# File 'lib/mountapi/router.rb', line 13 def add(route) routes = route_store_get(route.version, route.method, route.segment_count) route_store_set(route.version, route.method, route.segment_count, routes << route) end |
#find_route(url, http_method) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/mountapi/router.rb', line 18 def find_route(url, http_method) url_parts = self.class.url_parts(url) routes = route_store_get(url_parts[:version], Route::Method.new(http_method), url_parts[:segments].count) result = routes.find { |route| route.match?(url, http_method) } result || not_found_error(url, http_method) end |
#routes ⇒ Object
Routes list
45 46 47 |
# File 'lib/mountapi/router.rb', line 45 def routes routes_store.values.flat_map(&:values).flat_map(&:values).flatten end |
#url_for(operation_id, *url_params) ⇒ Object
Return an url for the operation_id
40 41 42 |
# File 'lib/mountapi/router.rb', line 40 def url_for(operation_id, *url_params) routes.find { |r| r.operation_id?(operation_id) }.to_url(*url_params) end |