Class: JBundle::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/jbundle/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(jfile = JBundle::JFILE) ⇒ Server

Returns a new instance of Server.



7
8
9
# File 'lib/jbundle/server.rb', line 7

def initialize(jfile = JBundle::JFILE)
  @jfile = jfile
end

Instance Method Details

#call(env) ⇒ Object

Configure JBundle on every request. Expensive but allows for reloading changes to JFile



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jbundle/server.rb', line 13

def call(env)
  bundle_name = env['PATH_INFO'].split('/').last
  begin
    JBundle.config_from_file(@jfile)
    compiler = JBundle.build(bundle_name)
    body = compiler.buildable? ? compiler.src : compiler.raw_src
    [200, {'Content-Type' => ::Rack::Mime.mime_type(compiler.ext)}, [body]]
  rescue NoBundleError => boom
    p = bundle_name == '' ? '[bundle_name].js' : bundle_name
    [404, {'Content-Type' => 'text/plain'}, ["No bundle defined. Try defining /#{p} in your JFile"]]
  rescue NoJFileError => boom
    [404, {'Content-Type' => 'text/plain'}, [boom.message]]
  end
  
end