Class: Clearwater::HotLoader::Server

Inherits:
Object
  • Object
show all
Includes:
Faye
Defined in:
lib/clearwater/hot_loader/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hot_loader = HotLoader) ⇒ Server

Returns a new instance of Server.



11
12
13
# File 'lib/clearwater/hot_loader/server.rb', line 11

def initialize hot_loader=HotLoader
  @hot_loader = hot_loader
end

Instance Attribute Details

#hot_loaderObject (readonly)

Returns the value of attribute hot_loader.



9
10
11
# File 'lib/clearwater/hot_loader/server.rb', line 9

def hot_loader
  @hot_loader
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clearwater/hot_loader/server.rb', line 15

def call env
  if WebSocket.websocket? env
    ws = WebSocket.new(env)

    ws.on :open do
      hot_loader.add_socket ws
    end

    ws.on :close do
      hot_loader.remove_socket ws
    end

    ws.rack_response
  else
    [200, { 'Content-Type' => 'text/plain' }, ['Websockets only, please.']]
  end
end

#compile_file(filename) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/clearwater/hot_loader/server.rb', line 33

def compile_file filename
  code = File.read(filename)

  if filename.end_with? '.rb'
    { js: Opal.compile(code) }
  elsif filename.end_with? '.js'
    { js: code }
  elsif filename.end_with? '.scss'
    { css: { filename: filename, body: Sass.compile(code.gsub(/^\s*@import.*$/, '')) } }
  else
    {}
  end
rescue => e
  warn "[Clearwater::HotLoader] #{e}"
  {}
end