Class: DreddHooks::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/dredd_hooks/server.rb,
lib/dredd_hooks/server/buffer.rb,
lib/dredd_hooks/server/events_handler.rb

Overview

The hooks worker server

Defined Under Namespace

Classes: Buffer, EventsHandler

Constant Summary collapse

HOST =
'127.0.0.1'
PORT =
61321
MESSAGE_DELIMITER =
"\n"
EVENTS =
{
  beforeEach: [
    :before_each,
    :before,
  ],
  beforeEachValidation: [
    :before_each_validation,
    :before_validation,
  ],
  afterEach: [
    :after,
    :after_each,
  ],
  afterAll: [
    :after_all,
  ],
  beforeAll: [
    :before_all,
  ],
}

Instance Method Summary collapse

Constructor Details

#initialize(error = STDERR, out = STDOUT) ⇒ Server

Returns a new instance of Server.



18
19
20
21
22
23
24
# File 'lib/dredd_hooks/server.rb', line 18

def initialize(error=STDERR, out=STDOUT)
  @error = error
  @out = out
  @server = TCPServer.new(HOST, PORT)
  @buffer = Buffer.new(MESSAGE_DELIMITER)
  @events_handler = EventsHandler.new
end

Instance Method Details

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dredd_hooks/server.rb', line 26

def run
  disable_buffering(out)
  loop do
    client = server.accept
    out.puts 'Dredd connected to Ruby Dredd hooks worker'
    buffer.flush!
    while (data = client.recv(10))
      buffer << data
      if buffer.any_message?
        messages = buffer.unshift_messages

        messages.each do |message|
          response = process_message(message)
          client.puts response + MESSAGE_DELIMITER
        end
      end
    end
    client.close
  end
end