Class: Gloo::WebSvr::Routing::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/web_svr/routing/router.rb

Constant Summary collapse

PAGE_CONTAINER =
'page'.freeze
SEGMENT_DIVIDER =
'/'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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_segmentsObject (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_routesObject

Add all page routes to the web server pages (routes).



86
87
88
89
90
91
92
93
94
# File 'lib/gloo/web_svr/routing/router.rb', line 86

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.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gloo/web_svr/routing/router.rb', line 101

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.



117
118
119
120
121
122
123
124
125
# File 'lib/gloo/web_svr/routing/router.rb', line 117

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.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/gloo/web_svr/routing/router.rb', line 195

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.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/gloo/web_svr/routing/router.rb', line 143

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?

Returns:

  • (Boolean)


188
189
190
# File 'lib/gloo/web_svr/routing/router.rb', line 188

def is_root_path?
  return @route_segments.count == 0
end

#next_segmentObject

Get the next segment in the route.



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/gloo/web_svr/routing/router.rb', line 174

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_containerObject

Get the root level page container.



78
79
80
81
# File 'lib/gloo/web_svr/routing/router.rb', line 78

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
# 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
  
  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 ] 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 ) 
    return [ page, @id ] if page
  end

  return nil
end

#show_routesObject

Show the routes in the running app. This uses the ShowRoutes helper class.



136
137
138
# File 'lib/gloo/web_svr/routing/router.rb', line 136

def show_routes
  @show_routes.show page_container
end