Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Refactor encapsulate the command-line interface #22

15 changes: 3 additions & 12 deletions bin/dredd-hooks-ruby
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
#!/usr/bin/env ruby
$LOAD_PATH.push File.join(File.dirname(__FILE__), "/../lib" )

require 'dredd_hooks'
require 'dredd_hooks/cli'

# Disables stdout buffering. This makes node.js able to capture stdout of this process with no delay
# http://stackoverflow.com/questions/23001033/how-to-live-stream-output-from-ruby-script-using-child-process-spawn
$stdout.sync = true
DreddHooks::CLI.start(ARGV)

# Load all files given on the command-line
DreddHooks::FileLoader.load ARGV

# Run the server

puts 'Starting Ruby Dredd Hooks Worker'
server = DreddHooks::Server.new
server.run
3 changes: 1 addition & 2 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ How to Add New Hooks

Dredd does support new hooks? It's time to extend the Ruby DSL!

Most of the new hooks definition is automated, but not everything yet.
In order to enable your new hook:

1. Determine if the hook is specific to a transaction or applies to all of them
Expand All @@ -12,7 +11,7 @@ In order to enable your new hook:
1. Add the usage example to the [**Execution order** feature][feature]
1. Run the entire test suite and watch the tests fail (start worrying if they don't!)
1. Add the hook name to the corresponding list in the [definitions file][def]
1. Add the corresponding Dredd **event** to the [events definition][events-handler] (be careful, the hooks order does matter!)
1. Add the corresponding Dredd **event** to the [events definition][def] (be careful, the hooks order does matter!)
1. Run the test suite and watch it pass : )

Finally, bump the [_minor_][semver] version number, update the `README`, the `CHANGELOG` and do anything you need to do in order to release!
Expand Down
23 changes: 23 additions & 0 deletions lib/dredd_hooks/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'dredd_hooks'

module DreddHooks
class CLI

def self.start(error=STDERR, out=STDOUT)

# Disables stdout buffering. This makes node.js able to capture stdout of this process with no delay
# http://stackoverflow.com/questions/23001033/how-to-live-stream-output-from-ruby-script-using-child-process-spawn
out.sync = true

# Load all files given on the command-line
DreddHooks::FileLoader.load(ARGV)

# Run the server

out.puts 'Starting Ruby Dredd Hooks Worker...'
server = DreddHooks::Server.new(error, out)
server.run
end
end
end

14 changes: 7 additions & 7 deletions lib/dredd_hooks/file_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ module DreddHooks
module FileLoader

def self.load(patterns)
self.unique_paths(patterns).each do |path|
unique_paths(patterns).each do |path|
puts path
require path
end
end

private

def self.unique_paths(patterns)
patterns.inject([]) { |paths, pattern|
paths += Dir.glob(pattern)
}.uniq
end
def self.unique_paths(patterns)
patterns.inject([]) { |paths, pattern|
paths + Dir.glob(pattern)
}.uniq
end
private_class_method :unique_paths
end
end

34 changes: 20 additions & 14 deletions lib/dredd_hooks/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@ module DreddHooks
# The hooks worker server
class Server

attr_reader :events_handler
private :events_handler
attr_reader :buffer, :error, :events_handler, :out, :server
private :buffer, :error, :events_handler, :out, :server

HOST = '127.0.0.1'
PORT = 61321
MESSAGE_DELIMITER = "\n"

def initialize
@server = TCPServer.new HOST, PORT
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

def run
loop do
client = @server.accept
STDERR.puts 'Dredd connected to Ruby Dredd hooks worker'
@buffer.flush!
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
buffer << data
if buffer.any_message?
messages = buffer.unshift_messages

messages.each do |message|
response = process_message(message)
Expand All @@ -49,10 +51,14 @@ def process_message(message)

transaction = events_handler.handle(event, transaction)

response = {
"uuid" => message['uuid'],
"event" => event,
"data" => transaction
response(message['uuid'], event, transaction)
end

def response(message_uuid, event, transaction)
{
uuid: message_uuid,
event: event,
data: transaction,
}.to_json
end

Expand Down
13 changes: 5 additions & 8 deletions lib/dredd_hooks/server/events_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ def initialize(events=EVENTS, runner=Runner.instance)

def handle(event, transaction)

begin
events.fetch(event.to_sym).each do |hook_name|
begin
transaction = runner.send("run_#{hook_name}_hooks_for_transaction", transaction)
rescue NoMethodError
raise UnknownHookError.new(hook_name)
end
events.fetch(event.to_sym, []).each do |hook_name|
begin
transaction = runner.send("run_#{hook_name}_hooks_for_transaction", transaction)
rescue NoMethodError
raise UnknownHookError.new(hook_name)
end
rescue KeyError => error
end

transaction
Expand Down