Class: Waitress::LibraryHandler
- Defined in:
- lib/waitress/handlers/libhandler.rb
Overview
The LibraryHandler is used to handle requests to the VHost regarding the libraries to be loaded by other Handlers and .wrb files. This will take any requests to /libraries (or whatever the user has set it to) to load libraries
Instance Attribute Summary collapse
-
#priority ⇒ Object
Returns the value of attribute priority.
Instance Method Summary collapse
- #basic_libs ⇒ Object
- #compile_less(main_file, path) ⇒ Object
- #compiler_libs ⇒ Object
- #dirType(k) ⇒ Object
-
#initialize(libraries, libdir, liburi, vhost) ⇒ LibraryHandler
constructor
A new instance of LibraryHandler.
- #loadBower(main, libname, home, append_type = false) ⇒ Object
- #parse_libraries ⇒ Object
- #respond?(request, vhost) ⇒ Boolean
- #serve(request, response, client, vhost) ⇒ Object
- #setup_bower ⇒ Object
Methods inherited from Handler
Constructor Details
#initialize(libraries, libdir, liburi, vhost) ⇒ LibraryHandler
Returns a new instance of LibraryHandler.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/waitress/handlers/libhandler.rb', line 14 def initialize libraries, libdir, liburi, vhost @priority = 150 @vhost = vhost @libraries, @libdir, @liburi = libraries, File.(libdir), liburi @cachedir = File.join(@libdir, ".libcache") @lesscache = File.join(@cachedir, "less_compile") FileUtils.mkdir_p(@libdir) unless File.exist?(@libdir) parse_libraries basic_libs compiler_libs setup_bower end |
Instance Attribute Details
#priority ⇒ Object
Returns the value of attribute priority.
12 13 14 |
# File 'lib/waitress/handlers/libhandler.rb', line 12 def priority @priority end |
Instance Method Details
#basic_libs ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/waitress/handlers/libhandler.rb', line 54 def basic_libs [:css, :js].each do |k| d = dirType k FileUtils.mkdir_p(d) unless File.exist?(d) Dir["#{d}/**/*.#{k.to_s}"].each do |fl| @libraries[File.basename(fl).to_sym] = { :file => fl, :type => k } end end end |
#compile_less(main_file, path) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/waitress/handlers/libhandler.rb', line 28 def compile_less main_file, path parser = Less::Parser.new :paths => [path].flatten, :filename => main_file tree = parser.parse(File.read(main_file)) FileUtils.mkdir_p(@lesscache) unless File.exist?(@lesscache) output = File.join(@lesscache, "#{Digest::MD5.hexdigest(main_file)}.min.css") File.write(output, tree.to_css(:compress => true)) {:file => output, :type => :css} end |
#compiler_libs ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/waitress/handlers/libhandler.rb', line 65 def compiler_libs lessdir = dirType :less FileUtils.mkdir_p(lessdir) unless File.exist?(lessdir) entry = File.join(lessdir, "less.yml") if File.exist? entry conf = YAML.load(File.read(entry)) unless conf["libs"].nil? conf["libs"].each do |libname, lib| @libraries[libname.to_sym] = compile_less File.(lib, lessdir), lessdir end end unless conf["watch"].nil? || (conf["watch"].length == 0) map = conf["watch"].map { |x| File.(x, lessdir) } Waitress::LESSWatcher.new(map) { |file| compile_less(file, lessdir) } end else File.write(entry, File.read(File.join(Waitress::Chef.resources, "default_less.yml"))) end end |
#dirType(k) ⇒ Object
116 117 118 |
# File 'lib/waitress/handlers/libhandler.rb', line 116 def dirType k File.join(@libdir, k.to_s) end |
#loadBower(main, libname, home, append_type = false) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/waitress/handlers/libhandler.rb', line 95 def loadBower main, libname, home, append_type=false ext = File.extname(main) lib = {} main = File.(main, home) if ext == ".css" lib[:file] = main lib[:type] = :css elsif ext == ".js" lib[:file] = main lib[:type] = :js elsif ext == ".less" lib = compile_less main, File.dirname(libname) end libname = libname + "_#{lib[:type].to_s}" if append_type libname = libname.to_sym @libraries[libname] = lib end |
#parse_libraries ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/waitress/handlers/libhandler.rb', line 39 def parse_libraries @libraries.each do |name, lib| l = {} d = dirType(lib[:bindtype]) matches = Dir["#{d}/**/*.#{lib[:bindtype].to_s}"].select { |x| (x =~ lib[:pattern]) != nil } if matches.length > 0 l[:file] = matches[0] l[:type] = lib[:bindtype] else l = nil end @libraries[name] = l end end |
#respond?(request, vhost) ⇒ Boolean
120 121 122 123 124 125 |
# File 'lib/waitress/handlers/libhandler.rb', line 120 def respond? request, vhost path = request.path return false unless path.start_with?("/#{@liburi}/") name = path.sub("/#{@liburi}/", "").to_sym @libraries.include?(name) end |
#serve(request, response, client, vhost) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/waitress/handlers/libhandler.rb', line 127 def serve request, response, client, vhost path = request.path name = path.sub("/#{@liburi}/", "").to_sym lib = @libraries[name] Waitress::Chef.serve_file request, response, client, vhost, lib[:file] end |
#setup_bower ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/waitress/handlers/libhandler.rb', line 86 def setup_bower bowerdir = File.join(@libdir, "bower_components") Dir["#{bowerdir}/**"].each do |component| bowerfile = JSON.parse File.read(File.join(component, "bower.json")) loadfiles = [bowerfile["main"]].flatten loadfiles.each { |x| loadBower(x, bowerfile["name"], component, loadfiles.length > 1) } end end |