Class: Opal::CliRunners::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, port) ⇒ Server


6
7
8
9
# File 'lib/opal/cli_runners/server.rb', line 6

def initialize(output, port)
  @output ||= output || $stdout
  @port = port
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output


10
11
12
# File 'lib/opal/cli_runners/server.rb', line 10

def output
  @output
end

#portObject (readonly)

Returns the value of attribute port


10
11
12
# File 'lib/opal/cli_runners/server.rb', line 10

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server


10
11
12
# File 'lib/opal/cli_runners/server.rb', line 10

def server
  @server
end

Instance Method Details

#app(source) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/opal/cli_runners/server.rb', line 33

def app(source)
  lambda do |env|
    case env['PATH_INFO']
    when '/'
      body = "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\"/>\n<script src=\"/cli_runner.js\"></script>\n</head>\n</html>\n"
      [200, {}, [body]]
    when '/cli_runner.js'
      [200, {'Content-Type' => 'text/javascript'}, [source]]
    else
      [404, {}, [body]]
    end
  end
end

#exit_statusObject


29
30
31
# File 'lib/opal/cli_runners/server.rb', line 29

def exit_status
  nil
end

#run(source, argv) ⇒ Object


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

def run(source, argv)
  unless argv.empty?
    raise ArgumentError, 'Program arguments are not supported on the PhantomJS runner'
  end

  require 'rack'
  require 'webrick'
  require 'logger'

  @server = Rack::Server.start(
    :app       => app(source),
    :Port      => port,
    :AccessLog => [],
    :Logger    => Logger.new(output)
  )
end