Class: Gloo::WebSvr::Asset

Inherits:
Object
  • Object
show all
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

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_routesObject

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



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/gloo/web_svr/asset.rb', line 159

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_containersObject

Create the containers for the assets if they do not exist.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/gloo/web_svr/asset.rb', line 174

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, info) ⇒ Object

Add a file object (page route) to the given container.



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/gloo/web_svr/asset.rb', line 263

def add_file_obj( can, name, pn, info )
  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 )
  # @factory.create_file( info.published_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.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/gloo/web_svr/asset.rb', line 194

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
      info = register_asset( name, pn, full_path )
      add_file_obj( container, name, pn, info )
    end
  end
end

#add_imagesObject

Add the images to the web server pages.



214
215
216
217
218
219
220
221
222
# File 'lib/gloo/web_svr/asset.rb', line 214

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_javascriptObject

Add the Javascript files to the web server pages.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/gloo/web_svr/asset.rb', line 245

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_stylesheetsObject

Add the stylesheets to the web server pages.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/gloo/web_svr/asset.rb', line 227

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_folderObject

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_folderObject

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

#is_asset?(name) ⇒ Boolean

Check if the given name is an asset.

Returns:

  • (Boolean)


122
123
124
# File 'lib/gloo/web_svr/asset.rb', line 122

def is_asset? name
  return name == ASSET_FOLDER
end

#javascript_folderObject

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

#list_css_assetsObject

List all css assets. This looks in the css container and lists the css files found earlier. A Debugging tool.



321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/gloo/web_svr/asset.rb', line 321

def list_css_assets
  data = []
  @stylesheets.children.each do |o|
    data << [ o.name, o.pn, o.value ]
  end
  headers = [ "Name", "PN", "Value" ]

  puts Gloo::App::Platform::RETURN
  title = "Stylesheet Assets with Routes"
  @engine.platform.table.show headers, data, title        
  puts Gloo::App::Platform::RETURN
end

#list_image_assetsObject

List all image assets. This looks in the image container and lists the images found earlier. A Debugging tool.



285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/gloo/web_svr/asset.rb', line 285

def list_image_assets
  data = []
  @images.children.each do |o|
    data << [ o.name, o.pn, o.value ]
  end
  headers = [ "Name", "PN", "Value" ]

  puts Gloo::App::Platform::RETURN
  title = "Image Assets with Routes"
  @engine.platform.table.show headers, data, title
  puts Gloo::App::Platform::RETURN
end

#list_js_assetsObject

List all js assets. This looks in the js container and lists the js files found earlier. A Debugging tool.



303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/gloo/web_svr/asset.rb', line 303

def list_js_assets
  data = []
  @javascript.children.each do |o|
    data << [ o.name, o.pn, o.value ]
  end
  headers = [ "Name", "PN", "Value" ]

  puts Gloo::App::Platform::RETURN
  title = "JavaScript Assets with Routes"
  @engine.platform.table.show headers, data, title
  puts Gloo::App::Platform::RETURN
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

#published_name(asset_name) ⇒ Object

Get the published name for the given asset name.



147
148
149
# File 'lib/gloo/web_svr/asset.rb', line 147

def published_name asset_name
  return AssetInfo.find_published_name_for( asset_name )
end

#register_asset(name, pn, full_path) ⇒ Object

Register an asset with the web server. Adds fingerprint to the file names for later access.

full_path is the FILE from which we build the SHA256 hash pn is the path and name within the assets directory name is the simple file name (icon.png)



139
140
141
142
# File 'lib/gloo/web_svr/asset.rb', line 139

def register_asset name, pn, full_path
  asset_pn = "/asset/#{pn}"
  return AssetInfo.new( @engine, full_path, name, asset_pn ).register
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, true )
end

#stylesheet_folderObject

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