Class: ComponentHandler

Inherits:
Object show all
Defined in:
lib/volt/server/component_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(component_paths) ⇒ ComponentHandler

Returns a new instance of ComponentHandler.



8
9
10
# File 'lib/volt/server/component_handler.rb', line 8

def initialize(component_paths)
  @component_paths = component_paths
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/volt/server/component_handler.rb', line 12

def call(env)
  req = Rack::Request.new(env)

  # TODO: Sanatize template path
  component_name = req.path.strip.gsub(/^\/components\//, '').gsub(/[.]js$/, '')

  code = ''

  component_files = ComponentFiles.new(component_name, @component_paths)
  component_files.component_paths.each do |component_path, component_name|
    code << ComponentTemplates.new(component_path, component_name).code
    code << "\n\n"
  end
  
  javascript_code = Opal.compile(code)

  # puts "ENV: #{env.inspect}"
  [200, {"Content-Type" => "text/html"}, StringIO.new(javascript_code)]
end