Class: Gloo::WebSvr::Asset
- Inherits:
-
Object
- Object
- Gloo::WebSvr::Asset
- Defined in:
- lib/gloo/web_svr/asset.rb
Constant Summary collapse
- ASSET_FOLDER =
'asset'.freeze
- IMAGE_FOLDER =
'image'.freeze
- STYLESHEET_FOLDER =
'stylesheet'.freeze
- JAVASCRIPT_FOLDER =
'javascript'.freeze
- CSS_TYPE =
'text/css'.freeze
- JS_TYPE =
'text/javascript'.freeze
- IMAGE_TYPE =
'image/'.freeze
- FAVICON_TYPE =
'image/x-icon'.freeze
Instance Method Summary collapse
-
#add_asset_routes ⇒ Object
Add all asssets to the web server pages (routes).
-
#add_containers ⇒ Object
Create the containers for the assets if they do not exist.
-
#add_file_obj(can, name, pn) ⇒ Object
Add a file object (page route) to the given container.
-
#add_files_in_folder(folder, container, path) ⇒ Object
Traverse the given folder and add all files to the container.
-
#add_images ⇒ Object
Add the images to the web server pages.
-
#add_javascript ⇒ Object
Add the Javascript files to the web server pages.
-
#add_stylesheets ⇒ Object
Add the stylesheets to the web server pages.
-
#asset_folder ⇒ Object
Get the asset folder in the project.
-
#image_folder ⇒ Object
Get the images folder in the project.
-
#initialize(engine, web_svr_obj) ⇒ Asset
constructor
Set up the web server.
-
#javascript_folder ⇒ Object
Get the stylesheets folder in the project.
-
#path_for_file(file) ⇒ Object
Find and return the page for the given route.
-
#render_file(file) ⇒ Object
Helper to create a successful image response with the given data.
-
#stylesheet_folder ⇒ Object
Get the stylesheets folder in the project.
-
#type_for_file(file) ⇒ Object
Get the return type for the given file.
Constructor Details
#initialize(engine, web_svr_obj) ⇒ Asset
Set up the web server.
30 31 32 33 34 35 |
# File 'lib/gloo/web_svr/asset.rb', line 30 def initialize( engine, web_svr_obj ) @engine = engine @log = @engine.log @web_svr_obj = web_svr_obj end |
Instance Method Details
#add_asset_routes ⇒ Object
Add all asssets to the web server pages (routes).
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/gloo/web_svr/asset.rb', line 127 def add_asset_routes return unless File.exist? asset_folder @log.debug 'Adding asset routes to web server…' @factory = @engine.factory add_containers add_images add_stylesheets add_javascript end |
#add_containers ⇒ Object
Create the containers for the assets if they do not exist.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/gloo/web_svr/asset.rb', line 142 def add_containers pages = @web_svr_obj.pages_container @assets = pages.find_child( ASSET_FOLDER ) || @factory.create_can( ASSET_FOLDER, pages ) @images = @assets.find_child( IMAGE_FOLDER ) || @factory.create_can( IMAGE_FOLDER, @assets ) @stylesheets = @assets.find_child( STYLESHEET_FOLDER ) || @factory.create_can( STYLESHEET_FOLDER, @assets ) @javascript = @assets.find_child( JAVASCRIPT_FOLDER ) || @factory.create_can( JAVASCRIPT_FOLDER, @assets ) end |
#add_file_obj(can, name, pn) ⇒ Object
Add a file object (page route) to the given container.
230 231 232 233 234 235 236 237 238 239 |
# File 'lib/gloo/web_svr/asset.rb', line 230 def add_file_obj( can, name, pn ) name = name.gsub( '.', '_' ) @log.debug "Adding route for file: #{name}" # First make sure the child doesn't already exist. child = can.find_child( name ) return if child @factory.create_file( name, pn, can ) end |
#add_files_in_folder(folder, container, path) ⇒ Object
Traverse the given folder and add all files to the container. This is a recursive method and look look for files in subfolders.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/gloo/web_svr/asset.rb', line 162 def add_files_in_folder( folder, container, path ) Dir.each_child( folder ) do |name| pn = File.join( path, name ) full_path = File.join( folder, name ) if File.directory? full_path child = container.find_child( name ) child = @factory.create_can( name, container ) if child.nil? add_files_in_folder( full_path, child, pn ) else add_file_obj( container, name, pn ) end end end |
#add_images ⇒ Object
Add the images to the web server pages.
181 182 183 184 185 186 187 188 189 |
# File 'lib/gloo/web_svr/asset.rb', line 181 def add_images @log.debug 'Adding image asset routes to web server…' return unless File.exist? image_folder # for each file in the images folder # create a file object and add it to the images container add_files_in_folder( image_folder, @images, IMAGE_FOLDER ) end |
#add_javascript ⇒ Object
Add the Javascript files to the web server pages.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/gloo/web_svr/asset.rb', line 212 def add_javascript @log.debug 'Adding javascript asset routes to web server…' return unless File.exist? javascript_folder # for each file in the javascript folder # create a file object and add it to the javascript container add_files_in_folder( javascript_folder, @javascript, JAVASCRIPT_FOLDER ) # Dir.each_child( javascript_folder ) do |name| # pn = File.join( JAVASCRIPT_FOLDER, name ) # add_file_obj( @javascript, name, pn ) # end end |
#add_stylesheets ⇒ Object
Add the stylesheets to the web server pages.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/gloo/web_svr/asset.rb', line 194 def add_stylesheets @log.debug 'Adding stylesheet asset routes to web server…' return unless File.exist? stylesheet_folder # for each file in the stylesheets folder # create a file object and add it to the stylesheets container add_files_in_folder( stylesheet_folder, @stylesheets, STYLESHEET_FOLDER ) # Dir.each_child( stylesheet_folder ) do |name| # pn = File.join( STYLESHEET_FOLDER, name ) # add_file_obj( @stylesheets, name, pn ) # end end |
#asset_folder ⇒ Object
Get the asset folder in the project.
45 46 47 |
# File 'lib/gloo/web_svr/asset.rb', line 45 def asset_folder return File.join( @engine.settings.project_path, ASSET_FOLDER ) end |
#image_folder ⇒ Object
Get the images folder in the project.
52 53 54 |
# File 'lib/gloo/web_svr/asset.rb', line 52 def image_folder return File.join( asset_folder, IMAGE_FOLDER ) end |
#javascript_folder ⇒ Object
Get the stylesheets folder in the project.
66 67 68 |
# File 'lib/gloo/web_svr/asset.rb', line 66 def javascript_folder return File.join( asset_folder, JAVASCRIPT_FOLDER ) end |
#path_for_file(file) ⇒ Object
Find and return the page for the given route.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gloo/web_svr/asset.rb', line 73 def path_for_file file pn = file.value # Is the file's value a recognizable file? return pn if File.exist? pn # Look in the web server's asset folder. pn = File.join( asset_folder, pn ) return pn end |
#render_file(file) ⇒ Object
Helper to create a successful image response with the given data.
111 112 113 114 115 116 117 |
# File 'lib/gloo/web_svr/asset.rb', line 111 def render_file( file ) type = type_for_file file data = File.binread file code = Gloo::WebSvr::ResponseCode::SUCCESS return Gloo::WebSvr::Response.new( @engine, code, type, data ) end |
#stylesheet_folder ⇒ Object
Get the stylesheets folder in the project.
59 60 61 |
# File 'lib/gloo/web_svr/asset.rb', line 59 def stylesheet_folder return File.join( asset_folder, STYLESHEET_FOLDER ) end |
#type_for_file(file) ⇒ Object
Get the return type for the given file.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/gloo/web_svr/asset.rb', line 88 def type_for_file file ext = File.extname( file ).downcase ext = ext[1..-1] if ext[0] == '.' if ext == 'css' return CSS_TYPE elsif ext == 'js' return JS_TYPE elsif ext == 'ico' return FAVICON_TYPE else return "#{IMAGE_TYPE}#{ext}" end end |