diff --git a/LICENSE.txt b/LICENSE.txt index 37934af..3b971db 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,5 +1,6 @@ The MIT License Copyright (c) 2015, 2016 Adam Kliment +Copyright (c) 2016 Gonzalo Bulnes Guilpain Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/dredd_hooks.gemspec b/dredd_hooks.gemspec index bc50ff3..e39a7b7 100644 --- a/dredd_hooks.gemspec +++ b/dredd_hooks.gemspec @@ -6,20 +6,19 @@ require 'dredd_hooks/version' Gem::Specification.new do |spec| spec.name = "dredd_hooks" spec.version = DreddHooks::VERSION - spec.authors = ["Adam Kliment"] - spec.email = ["adam@apiary.io"] + spec.authors = ["Adam Kliment", "Gonzalo Bulnes Guilpain"] + spec.email = ["adam@apiary.io", "gon.bulnes@gmail.com"] spec.summary = %q{Ruby Hooks Handler for Dredd API Testing Framework} - spec.description = %q{Write Dredd hooks in Ruby to glue together API Blueprint with your Ruby project} + spec.description = %q{Write Dredd hooks in Ruby to glue together API Blueprint with your Ruby project.} spec.homepage = "https://github.com/apiaryio/dredd-hooks-ruby" spec.license = "MIT" - spec.files = `git ls-files -z`.split("\x0") - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ["lib"] + spec.executables = "dredd-hooks-ruby" + spec.files = Dir["{bin,lib}/**/*", "CHANGELOG.md", "Gemfile", "LICENSE.txt", "Rakefile", "README.md" ] + spec.test_files = Dir["features/**/*"] + spec.add_development_dependency "aruba", "~> 0.6.2" spec.add_development_dependency "bundler", "~> 1.6" spec.add_development_dependency "rake", "~> 10.0" - spec.add_development_dependency "aruba", "~> 0.6.2" spec.add_development_dependency "sinatra", "~> 1.4.5" end diff --git a/features/execution_order.feature b/features/execution_order.feature index ae81373..5361851 100644 --- a/features/execution_order.feature +++ b/features/execution_order.feature @@ -23,47 +23,48 @@ Feature: Execution order Scenario: Given a file named "hookfile.rb" with: """ + require 'dredd_hooks/methods' include DreddHooks::Methods key = 'hooks_modifications' before("/message > GET") do |transaction| - transaction[key] = [] if transaction[key].nil? + transaction[key] ||= [] transaction[key].push "before modification" end after("/message > GET") do |transaction| - transaction[key] = [] if transaction[key].nil? + transaction[key] ||= [] transaction[key].push "after modification" end before_validation("/message > GET") do |transaction| - transaction[key] = [] if transaction[key].nil? + transaction[key] ||= [] transaction[key].push "before validation modification" end before_all do |transaction| - transaction[0][key] = [] if transaction[0][key].nil? + transaction[0][key] ||= [] transaction[0][key].push "before all modification" end after_all do |transaction| - transaction[0][key] = [] if transaction[0][key].nil? + transaction[0][key] ||= [] transaction[0][key].push "after all modification" end before_each do |transaction| - transaction[key] = [] if transaction[key].nil? + transaction[key] ||= [] transaction[key].push "before each modification" end before_each_validation do |transaction| - transaction[key] = [] if transaction[key].nil? + transaction[key] ||= [] transaction[key].push "before each validation modification" end after_each do |transaction| - transaction[key] = [] if transaction[key].nil? + transaction[key] ||= [] transaction[key].push "after each modification" end diff --git a/features/failing_transaction.feature b/features/failing_transaction.feature index d9dd3b6..311a632 100644 --- a/features/failing_transaction.feature +++ b/features/failing_transaction.feature @@ -23,6 +23,7 @@ Feature: Failing a transacstion Scenario: Given a file named "hookfile.rb" with: """ + require 'dredd_hooks/methods' include DreddHooks::Methods before("/message > GET") do |transaction| transaction['fail'] = "Yay! Failed in ruby!" diff --git a/features/hook_handlers.feature b/features/hook_handlers.feature index 4f8810c..3541f89 100644 --- a/features/hook_handlers.feature +++ b/features/hook_handlers.feature @@ -23,6 +23,7 @@ Feature: Hook handlers Scenario: Given a file named "hookfile.rb" with: """ + require 'dredd_hooks/methods' include DreddHooks::Methods before("/message > GET") do |transaction| diff --git a/features/multiple_hookfiles.feature b/features/multiple_hookfiles.feature index d7ce29c..63cd37d 100644 --- a/features/multiple_hookfiles.feature +++ b/features/multiple_hookfiles.feature @@ -23,6 +23,7 @@ Feature: Multiple hookfiles with a glob Scenario: Given a file named "hookfile1.rb" with: """ + require 'dredd_hooks/methods' include DreddHooks::Methods before("/message > GET") do |transaction| puts "It's me, File1" @@ -30,6 +31,7 @@ Feature: Multiple hookfiles with a glob """ And a file named "hookfile2.rb" with: """ + require 'dredd_hooks/methods' include DreddHooks::Methods before("/message > GET") do |transaction| puts "It's me, File2" @@ -37,6 +39,7 @@ Feature: Multiple hookfiles with a glob """ And a file named "hookfile_to_be_globed.rb" with: """ + require 'dredd_hooks/methods' include DreddHooks::Methods before("/message > GET") do |transaction| puts "It's me, File3" @@ -55,4 +58,4 @@ Feature: Multiple hookfiles with a glob And the output should contain: """ It's me, File3 - """ \ No newline at end of file + """ diff --git a/lib/dredd_hooks.rb b/lib/dredd_hooks.rb index 57034f8..791e04c 100644 --- a/lib/dredd_hooks.rb +++ b/lib/dredd_hooks.rb @@ -1,6 +1,5 @@ -require "dredd_hooks/version" +require 'dredd_hooks/file_loader' +require 'dredd_hooks/server' + +module DreddHooks; end -require File.join(File.dirname(__FILE__), './dredd_hooks/methods.rb') -require File.join(File.dirname(__FILE__), './dredd_hooks/runner.rb') -require File.join(File.dirname(__FILE__), './dredd_hooks/file_loader.rb') -require File.join(File.dirname(__FILE__), './dredd_hooks/server.rb') diff --git a/lib/dredd_hooks/file_loader.rb b/lib/dredd_hooks/file_loader.rb index 9ca2997..62e3247 100644 --- a/lib/dredd_hooks/file_loader.rb +++ b/lib/dredd_hooks/file_loader.rb @@ -1,18 +1,20 @@ module DreddHooks module FileLoader - def self.unique_paths globs - paths = [] - globs.each do |glob| - paths += Dir.glob glob - end - paths.uniq - end - def self.load globs - self.unique_paths(globs).each do |path| + def self.load(patterns) + self.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 end -end \ No newline at end of file +end + diff --git a/lib/dredd_hooks/runner.rb b/lib/dredd_hooks/runner.rb index ea6715f..1f77f17 100644 --- a/lib/dredd_hooks/runner.rb +++ b/lib/dredd_hooks/runner.rb @@ -1,3 +1,5 @@ +require 'dredd_hooks/methods' + module DreddHooks module Runner @@ -5,35 +7,29 @@ module Runner # Runers for Transaction specific hooks # - def self.run_before_hooks_for_transaction transaction + def self.run_before_hooks_for_transaction(transaction) transaction_name = transaction["name"] - hooks = Methods.class_variable_get("@@before_hooks")[transaction_name] - if hooks.kind_of? Array - hooks.each do |hook_proc| - hook_proc.call transaction - end + hooks = Methods.class_variable_get("@@before_hooks")[transaction_name] || [] + hooks.each do |hook_proc| + hook_proc.call(transaction) end return transaction end - def self.run_before_validation_hooks_for_transaction transaction + def self.run_before_validation_hooks_for_transaction(transaction) transaction_name = transaction["name"] - hooks = Methods.class_variable_get("@@before_validation_hooks")[transaction_name] - if hooks.kind_of? Array - hooks.each do |hook_proc| - hook_proc.call transaction - end + hooks = Methods.class_variable_get("@@before_validation_hooks")[transaction_name] || [] + hooks.each do |hook_proc| + hook_proc.call(transaction) end return transaction end - def self.run_after_hooks_for_transaction transaction + def self.run_after_hooks_for_transaction(transaction) transaction_name = transaction["name"] - hooks = Methods.class_variable_get("@@after_hooks")[transaction_name] - if hooks.kind_of? Array - hooks.each do |hook_proc| - hook_proc.call transaction - end + hooks = Methods.class_variable_get("@@after_hooks")[transaction_name] || [] + hooks.each do |hook_proc| + hook_proc.call(transaction) end return transaction end @@ -42,23 +38,23 @@ def self.run_after_hooks_for_transaction transaction # Runners for *_each hooks API # - def self.run_before_each_hooks_for_transaction transaction + def self.run_before_each_hooks_for_transaction(transaction) Methods.class_variable_get("@@before_each_hooks").each do |hook_proc| - hook_proc.call transaction + hook_proc.call(transaction) end return transaction end - def self.run_before_each_validation_hooks_for_transaction transaction + def self.run_before_each_validation_hooks_for_transaction(transaction) Methods.class_variable_get("@@before_each_validation_hooks").each do |hook_proc| - hook_proc.call transaction + hook_proc.call(transaction) end return transaction end - def self.run_after_each_hooks_for_transaction transaction + def self.run_after_each_hooks_for_transaction(transaction) Methods.class_variable_get("@@after_each_hooks").each do |hook_proc| - hook_proc.call transaction + hook_proc.call(transaction) end return transaction end @@ -67,18 +63,18 @@ def self.run_after_each_hooks_for_transaction transaction # Runners for *_all hooks API # - def self.run_before_all_hooks_for_transactions transactions + def self.run_before_all_hooks_for_transaction(transaction) Methods.class_variable_get("@@before_all_hooks").each do |hook_proc| - hook_proc.call transactions + hook_proc.call(transaction) end - return transactions + return transaction end - def self.run_after_all_hooks_for_transactions transactions + def self.run_after_all_hooks_for_transaction(transaction) Methods.class_variable_get("@@after_all_hooks").each do |hook_proc| - hook_proc.call transactions + hook_proc.call(transaction) end - return transactions + return transaction end end -end \ No newline at end of file +end diff --git a/lib/dredd_hooks/server.rb b/lib/dredd_hooks/server.rb index 2144790..9b33445 100644 --- a/lib/dredd_hooks/server.rb +++ b/lib/dredd_hooks/server.rb @@ -1,55 +1,57 @@ require 'socket' require 'json' +require 'dredd_hooks/runner' + module DreddHooks + + # The hooks worker server class Server - # - # The hooks worker server - # HOST = '127.0.0.1' PORT = 61321 MESSAGE_DELIMITER = "\n" - @server = nil + def initialize + @server = TCPServer.new HOST, PORT + end def process_message message, client event = message['event'] - data = message['data'] + transaction = message['data'] if event == "beforeEach" - data = DreddHooks::Runner.run_before_each_hooks_for_transaction data - data = DreddHooks::Runner.run_before_hooks_for_transaction data + transaction = DreddHooks::Runner.run_before_each_hooks_for_transaction(transaction) + transaction = DreddHooks::Runner.run_before_hooks_for_transaction(transaction) end if event == "beforeEachValidation" - data = DreddHooks::Runner.run_before_each_validation_hooks_for_transaction data - data = DreddHooks::Runner.run_before_validation_hooks_for_transaction data + transaction = DreddHooks::Runner.run_before_each_validation_hooks_for_transaction(transaction) + transaction = DreddHooks::Runner.run_before_validation_hooks_for_transaction(transaction) end if event == "afterEach" - data = DreddHooks::Runner.run_after_hooks_for_transaction data - data = DreddHooks::Runner.run_after_each_hooks_for_transaction data + transaction = DreddHooks::Runner.run_after_hooks_for_transaction(transaction) + transaction = DreddHooks::Runner.run_after_each_hooks_for_transaction(transaction) end if event == "beforeAll" - data = DreddHooks::Runner.run_before_all_hooks_for_transactions data + transaction = DreddHooks::Runner.run_before_all_hooks_for_transaction(transaction) end if event == "afterAll" - data = DreddHooks::Runner.run_after_all_hooks_for_transactions data + transaction = DreddHooks::Runner.run_after_all_hooks_for_transaction(transaction) end to_send = { "uuid" => message['uuid'], "event" => event, - "data" => data + "data" => transaction }.to_json client.puts to_send + "\n" end def run - @server = TCPServer.new HOST, PORT loop do #Thread.abort_on_exception=true client = @server.accept @@ -61,21 +63,20 @@ def run splitted_buffer = buffer.split(MESSAGE_DELIMITER) buffer = "" - messages = [] - - splitted_buffer.each do |message| + messages = splitted_buffer.inject([]) { |messages, message| begin messages.push JSON.parse(message) - rescue JSON::ParserError - # if message aftger delimiter is not parseable json, it's - # a chunk of next message, put it back to the buffer + # If the message after the delimiter is not parseable JSON, + # then it's a chunk of next message, and should be put back + # into the buffer. buffer += message + messages end - end + } messages.each do |message| - process_message message, client + process_message(message, client) end end end @@ -83,4 +84,4 @@ def run end end end -end \ No newline at end of file +end