Class: Scripted::Formatters::Websocket

Inherits:
Blank
  • Object
show all
Includes:
HumanStatus
Defined in:
lib/scripted/formatters/websocket.rb

Instance Attribute Summary

Attributes inherited from Blank

#configuration, #raw_out

Instance Method Summary collapse

Methods included from HumanStatus

#human_status, #status_code

Constructor Details

#initialize(out, configuration) ⇒ Websocket

Returns a new instance of Websocket.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/scripted/formatters/websocket.rb', line 12

def initialize(out, configuration)
  @uri = URI.parse(out)
  super(@uri, configuration)
  @buffers = Hash.new { |h,k| h[k] = "" }
  @flusher = Thread.new do
    loop do
      flush
      sleep 0.1
    end
  end
  publish :action => :initialize
end

Instance Method Details

#closeObject



70
71
72
73
74
# File 'lib/scripted/formatters/websocket.rb', line 70

def close
  @flusher.exit
  flush!
  publish :action => :close
end

#done(command) ⇒ Object



44
45
46
47
# File 'lib/scripted/formatters/websocket.rb', line 44

def done(command)
  flush!
  publish :action => :done, :command => command_json(command)
end

#each_char(char, command) ⇒ Object



25
26
27
# File 'lib/scripted/formatters/websocket.rb', line 25

def each_char(char, command)
  @buffers[command] << char
end

#exception(command, exception) ⇒ Object



39
40
41
42
# File 'lib/scripted/formatters/websocket.rb', line 39

def exception(command, exception)
  flush!
  publish :action => :exception, :command => command_json(command), :exception => exception, :backtrace => exception.backtrace
end

#execute(command) ⇒ Object



54
55
56
57
# File 'lib/scripted/formatters/websocket.rb', line 54

def execute(command)
  flush!
  publish :action => :execute, :command => command_json(command)
end

#flushObject



64
65
66
67
68
# File 'lib/scripted/formatters/websocket.rb', line 64

def flush
  if output != ""
    publish :action => :output, :output => output
  end
end

#flush!Object



59
60
61
62
# File 'lib/scripted/formatters/websocket.rb', line 59

def flush!
  flush
  @buffer = ""
end

#halted(command) ⇒ Object



49
50
51
52
# File 'lib/scripted/formatters/websocket.rb', line 49

def halted(command)
  flush!
  publish :action => :halted, :command => command_json(command)
end

#start(commands, runner) ⇒ Object



29
30
31
32
# File 'lib/scripted/formatters/websocket.rb', line 29

def start(commands, runner)
  flush!
  publish :action => :start, :commands => commands.map { |command| command_json(command) }, :runner => runner_json(runner)
end

#stop(commands, runner) ⇒ Object



34
35
36
37
# File 'lib/scripted/formatters/websocket.rb', line 34

def stop(commands, runner)
  flush!
  publish :action => :stop, :commands => commands.map { |command| command_json(command) }, :runner => runner_json(runner)
end