Class: Gloo::WebSvr::Routing::Router
- Inherits:
-
Object
- Object
- Gloo::WebSvr::Routing::Router
- Defined in:
- lib/gloo/web_svr/routing/router.rb
Constant Summary collapse
- PAGE_CONTAINER =
'page'.freeze
- SEGMENT_DIVIDER =
'/'.freeze
Instance Attribute Summary collapse
-
#route_segments ⇒ Object
readonly
Returns the value of attribute route_segments.
Instance Method Summary collapse
-
#add_page_routes ⇒ Object
Add all page routes to the web server pages (routes).
-
#add_pages(can, parent) ⇒ Object
Add the pages to the web server pages.
-
#add_route_alias(parent, name, pn) ⇒ Object
Add route alias to the page.
-
#detect_segments(path) ⇒ Object
Create a list of path segments.
-
#find_route_segment(objs) ⇒ Object
Find the route segment in the object container.
-
#initialize(engine, web_svr_obj) ⇒ Router
constructor
Set up the web server.
-
#is_root_path? ⇒ Boolean
Is this the root path?.
-
#next_segment ⇒ Object
Get the next segment in the route.
-
#page_container ⇒ Object
Get the root level page container.
-
#page_for_route(path, method) ⇒ Object
Find and return the page for the given route.
-
#show_routes ⇒ Object
Show the routes in the running app.
Constructor Details
#initialize(engine, web_svr_obj) ⇒ Router
Set up the web server.
25 26 27 28 29 30 31 |
# File 'lib/gloo/web_svr/routing/router.rb', line 25 def initialize( engine, web_svr_obj ) @engine = engine @log = @engine.log @web_svr_obj = web_svr_obj @show_routes = ShowRoutes.new( @engine ) end |
Instance Attribute Details
#route_segments ⇒ Object (readonly)
Returns the value of attribute route_segments.
15 16 17 |
# File 'lib/gloo/web_svr/routing/router.rb', line 15 def route_segments @route_segments end |
Instance Method Details
#add_page_routes ⇒ Object
Add all page routes to the web server pages (routes).
94 95 96 97 98 99 100 101 102 |
# File 'lib/gloo/web_svr/routing/router.rb', line 94 def add_page_routes can = page_container return unless can @log.debug 'Adding page routes to web server…' @factory = @engine.factory add_pages can, @web_svr_obj.pages_container end |
#add_pages(can, parent) ⇒ Object
Add the pages to the web server pages. This is a recursive function that will add all pages in the folder and subfolders.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/gloo/web_svr/routing/router.rb', line 109 def add_pages can, parent # for each file in the page container # create a page object and add it to the routes can.children.each do |obj| if obj.class == Gloo::Objs::Container child_can = parent.find_add_child( obj.name, 'container' ) add_pages( obj, child_can ) elsif obj.class == Gloo::Objs::Page add_route_alias( parent, obj.name, obj.pn ) end end end |
#add_route_alias(parent, name, pn) ⇒ Object
Add route alias to the page.
125 126 127 128 129 130 131 132 133 |
# File 'lib/gloo/web_svr/routing/router.rb', line 125 def add_route_alias( parent, name, pn ) name = name.gsub( '.', '_' ) # First make sure the child doesn't already exist. child = parent.find_child( name ) return if child @factory.create_alias( name, pn, parent ) end |
#detect_segments(path) ⇒ Object
Create a list of path segments.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/gloo/web_svr/routing/router.rb', line 203 def detect_segments path # Split the path into segments. @route_segments = path.split SEGMENT_DIVIDER # Remove the first segment if it is empty. @route_segments.shift if @route_segments.first.blank? @route_segments.each do |seg| if seg.to_i.to_s == seg @id = seg.to_i @log.info "found id for route: #{@id}" @route_segments.delete seg @route_segments << ResourceRouter.segment_for_method( @method ) end end return @route_segments end |
#find_route_segment(objs) ⇒ Object
Find the route segment in the object container.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/gloo/web_svr/routing/router.rb', line 151 def find_route_segment objs this_segment = next_segment if this_segment.blank? # && Gloo::WebSvr::WebMethod.is_post?( @method ) this_segment = Gloo::WebSvr::Routing::ResourceRouter::INDEX end objs.each do |o| o = Gloo::Objs::Alias.resolve_alias( @engine, o ) if o.name == this_segment if o.class == Gloo::Objs::Page @log.debug "found page for route: #{o.pn}" return o elsif o.class == Gloo::Objs::FileHandle @log.debug "found static file for route: #{o.pn}" return o else return nil unless o.child_count > 0 return find_route_segment( o.children ) end end end return nil end |
#is_root_path? ⇒ Boolean
Is this the root path?
196 197 198 |
# File 'lib/gloo/web_svr/routing/router.rb', line 196 def is_root_path? return @route_segments.count == 0 end |
#next_segment ⇒ Object
Get the next segment in the route.
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/gloo/web_svr/routing/router.rb', line 182 def next_segment this_segment = @route_segments.shift return nil if this_segment.nil? # A URL might include a dot in a name, but we can't do that # because dot is a reserve path thing. So we replace it with # an underscore. this_segment = this_segment.gsub( '.', '_' ) return this_segment end |
#page_container ⇒ Object
Get the root level page container.
86 87 88 89 |
# File 'lib/gloo/web_svr/routing/router.rb', line 86 def page_container pn = Gloo::Core::Pn.new( @engine, PAGE_CONTAINER ) return pn.resolve end |
#page_for_route(path, method) ⇒ Object
Find and return the page for the given route.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/gloo/web_svr/routing/router.rb', line 41 def page_for_route( path, method ) @log.info "routing to #{path} for method #{method}" @method = method route_params = nil @id = nil detect_segments path return @web_svr_obj.home_page if is_root_path? pages = @web_svr_obj.pages_container if pages # If the method is POST and the last segment is NOT 'create', # we'll add create to the route segments. if Gloo::WebSvr::Routing::ResourceRouter.is_implicit_create?( method, @route_segments.last ) @route_segments << Gloo::WebSvr::Routing::ResourceRouter::POST_ROUTE page = find_route_segment( pages.children ) return [ page, @id, route_params ] if page # We didn't find the page, so remove the last segment and try again # posting to the resource. @route_segments.pop end page = find_route_segment( pages.children ) # Are there any remaining segments to be added as route parameters? if @route_segments.count > 0 route_params = @route_segments end return [ page, @id, route_params ] if page end return nil end |
#show_routes ⇒ Object
Show the routes in the running app. This uses the ShowRoutes helper class.
144 145 146 |
# File 'lib/gloo/web_svr/routing/router.rb', line 144 def show_routes @show_routes.show page_container end |