dredd_hooks 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e812ae943d277f3163afe9c084e48aacce110730
4
+ data.tar.gz: e6b2557296918b237c4a67ead7267ead330cc1bf
5
+ SHA512:
6
+ metadata.gz: b99c296c26c164cd209f597563be725b8d9d7795d646c2d8b7e32f47333daf55c428c7fa4fe4083218f9cfd74d68e68b9ec7346966a02c65bcbfcfa2f946041d
7
+ data.tar.gz: 6c601a534be422f7b24e33df2834580e9d854f74ab0b157d62dd4bf07c8fe796920bf1572f6f461236aac1835354b3d7e01a89f571592f623e21851f9eba4eec
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ npm-debug.log
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ before_install:
3
+ - npm install -g dredd@unstable
4
+
5
+ rvm:
6
+ - 1.9.3
7
+ - 2.2.0
8
+
9
+ script: bundle exec cucumber
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dredd_worker.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Adam Kliment
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+
2
+ # Ruby Hooks Handler for Dredd API Testing Framework
3
+
4
+ [![Build Status](https://travis-ci.org/apiaryio/dredd-hooks-ruby.svg?branch=master)](https://travis-ci.org/apiaryio/dredd-hooks-ruby)
5
+
6
+ Test your api with [Dredd API testing framework](https://github.com/apiaryio/dredd) and write [hooks](http://dredd.readthedocs.org/en/latest/hooks/) in Ruby to glue together API Blueprint with your Ruby project.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'dredd_worker'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install dredd_hooks
23
+
24
+ ## Usage
25
+
26
+ 1. Create a hook file in `hooks.rb`:
27
+
28
+ ```ruby
29
+ include DreddHooks::Methods
30
+
31
+ before "Machines > Machines collection > Get Machines" do |transaction|
32
+ transaction['skip'] = "true"
33
+ end
34
+ ```
35
+
36
+ 2. Run it with Dredd
37
+
38
+ ```
39
+ $ dredd apiary.apib localhost:3000 --language ruby --hookfiles hooks.rb
40
+ ```
41
+
42
+ ## API
43
+
44
+ Module `DreddHooks::Methods` mixes in following methods `before`, `after`, `before_all`, `after_all`, `before_each`, `after_each`, `before_validation`, `before_each_validation`
45
+
46
+ `before`, `before_validation` `after` hooks are identified by [transaction name](http://dredd.readthedocs.org/en/latest/hooks/#getting-transaction-names).
47
+
48
+ Usage is very similar to [sync JS hooks API](http://dredd.readthedocs.org/en/latest/hooks/#sync-api)
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'dredd_hooks'
4
+
5
+ # Disables stdout buffering. This makes node.js able to capture stdout of this process with no delay
6
+ # http://stackoverflow.com/questions/23001033/how-to-live-stream-output-from-ruby-script-using-child-process-spawn
7
+ $stdout.sync = true
8
+
9
+ # Load all files given on the command-line
10
+ DreddHooks::FileLoader.load ARGV
11
+
12
+ # Run the server
13
+
14
+ puts 'Starting Ruby Dredd Hooks Worker'
15
+ server = DreddHooks::Server.new
16
+ server.run
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dredd_hooks/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dredd_hooks"
8
+ spec.version = DreddHooks::VERSION
9
+ spec.authors = ["Adam Kliment"]
10
+ spec.email = ["adam@apiary.io"]
11
+ spec.summary = %q{Ruby Hooks Handler for Dredd API Testing Framework}
12
+ spec.description = %q{Write Dredd hooks in Ruby to glue together API Blueprint with your Ruby project}
13
+ spec.homepage = "https://github.com/apiaryio/dredd-hooks-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "aruba", "~> 0.6.2"
24
+ spec.add_development_dependency "sinatra", "~> 1.4.5"
25
+ end
@@ -0,0 +1,86 @@
1
+ Feature: Execution order
2
+
3
+ Background:
4
+ Given I have "dredd-hooks-ruby" command installed
5
+ And I have "dredd" command installed
6
+ And a file named "server.rb" with:
7
+ """
8
+ require 'sinatra'
9
+ get '/message' do
10
+ "Hello World!\n\n"
11
+ end
12
+ """
13
+
14
+ And a file named "apiary.apib" with:
15
+ """
16
+ # My Api
17
+ ## GET /message
18
+ + Response 200 (text/html;charset=utf-8)
19
+ Hello World!
20
+ """
21
+
22
+ @debug
23
+ Scenario:
24
+ Given a file named "hookfile.rb" with:
25
+ """
26
+ include DreddHooks::Methods
27
+
28
+ key = 'hooks_modifications'
29
+
30
+ before("/message > GET") do |transaction|
31
+ transaction[key] = [] if transaction[key].nil?
32
+ transaction[key].push "before modification"
33
+ end
34
+
35
+ after("/message > GET") do |transaction|
36
+ transaction[key] = [] if transaction[key].nil?
37
+ transaction[key].push "after modification"
38
+ end
39
+
40
+ before_validation("/message > GET") do |transaction|
41
+ transaction[key] = [] if transaction[key].nil?
42
+ transaction[key].push "before validation modification"
43
+ end
44
+
45
+ before_all do |transaction|
46
+ transaction[0][key] = [] if transaction[0][key].nil?
47
+ transaction[0][key].push "before all modification"
48
+ end
49
+
50
+ after_all do |transaction|
51
+ transaction[0][key] = [] if transaction[0][key].nil?
52
+ transaction[0][key].push "after all modification"
53
+ end
54
+
55
+ before_each do |transaction|
56
+ transaction[key] = [] if transaction[key].nil?
57
+ transaction[key].push "before each modification"
58
+ end
59
+
60
+ before_each_validation do |transaction|
61
+ transaction[key] = [] if transaction[key].nil?
62
+ transaction[key].push "before each validation modification"
63
+ end
64
+
65
+ after_each do |transaction|
66
+ transaction[key] = [] if transaction[key].nil?
67
+ transaction[key].push "after each modification"
68
+ end
69
+
70
+ """
71
+ Given I set the environment variables to:
72
+ | variable | value |
73
+ | TEST_DREDD_HOOKS_HANDLER_ORDER | true |
74
+ When I run `dredd ./apiary.apib http://localhost:4567 --server "ruby server.rb" --language dredd-hooks-ruby --hookfiles ./hookfile.rb`
75
+ Then the exit status should be 0
76
+ And the output should contain:
77
+ """
78
+ 0 before all modification
79
+ 1 before each modification
80
+ 2 before modification
81
+ 3 before each validation modification
82
+ 4 before validation modification
83
+ 5 after modification
84
+ 6 after each modification
85
+ 7 after all modification
86
+ """
@@ -0,0 +1,36 @@
1
+ Feature: Failing a transacstion
2
+
3
+ Background:
4
+ Given I have "dredd-hooks-ruby" command installed
5
+ And I have "dredd" command installed
6
+ And a file named "server.rb" with:
7
+ """
8
+ require 'sinatra'
9
+ get '/message' do
10
+ "Hello World!\n\n"
11
+ end
12
+ """
13
+
14
+ And a file named "apiary.apib" with:
15
+ """
16
+ # My Api
17
+ ## GET /message
18
+ + Response 200 (text/html;charset=utf-8)
19
+ Hello World!
20
+ """
21
+
22
+ @debug
23
+ Scenario:
24
+ Given a file named "hookfile.rb" with:
25
+ """
26
+ include DreddHooks::Methods
27
+ before("/message > GET") do |transaction|
28
+ transaction['fail'] = "Yay! Failed in ruby!"
29
+ end
30
+ """
31
+ When I run `dredd ./apiary.apib http://localhost:4567 --server "ruby server.rb" --language dredd-hooks-ruby --hookfiles ./hookfile.rb`
32
+ Then the exit status should be 1
33
+ And the output should contain:
34
+ """
35
+ Yay! Failed in ruby!
36
+ """
@@ -0,0 +1,95 @@
1
+ Feature: Hook handlers
2
+
3
+ Background:
4
+ Given I have "dredd-hooks-ruby" command installed
5
+ And I have "dredd" command installed
6
+ And a file named "server.rb" with:
7
+ """
8
+ require 'sinatra'
9
+ get '/message' do
10
+ "Hello World!\n\n"
11
+ end
12
+ """
13
+
14
+ And a file named "apiary.apib" with:
15
+ """
16
+ # My Api
17
+ ## GET /message
18
+ + Response 200 (text/html;charset=utf-8)
19
+ Hello World!
20
+ """
21
+
22
+ @debug
23
+ Scenario:
24
+ Given a file named "hookfile.rb" with:
25
+ """
26
+ include DreddHooks::Methods
27
+
28
+ before("/message > GET") do |transaction|
29
+ puts "before hook handled"
30
+ end
31
+
32
+ after("/message > GET") do |transaction|
33
+ puts "after hook handled"
34
+ end
35
+
36
+ before_validation("/message > GET") do |transaction|
37
+ puts "before validation hook handled"
38
+ end
39
+
40
+ before_all do |transaction|
41
+ puts "before all hook handled"
42
+ end
43
+
44
+ after_all do |transaction|
45
+ puts "after all hook handled"
46
+ end
47
+
48
+ before_each do |transaction|
49
+ puts "before each hook handled"
50
+ end
51
+
52
+ before_each_validation do |transaction|
53
+ puts "before each validation hook handled"
54
+ end
55
+
56
+ after_each do |transaction|
57
+ puts "after each hook handled"
58
+ end
59
+
60
+ """
61
+
62
+ When I run `dredd ./apiary.apib http://localhost:4567 --server "ruby server.rb" --language dredd-hooks-ruby --hookfiles ./hookfile.rb`
63
+ Then the exit status should be 0
64
+ And the output should contain:
65
+ """
66
+ before hook handled
67
+ """
68
+ And the output should contain:
69
+ """
70
+ before validation hook handled
71
+ """
72
+ And the output should contain:
73
+ """
74
+ after hook handled
75
+ """
76
+ And the output should contain:
77
+ """
78
+ before each hook handled
79
+ """
80
+ And the output should contain:
81
+ """
82
+ before each validation hook handled
83
+ """
84
+ And the output should contain:
85
+ """
86
+ after each hook handled
87
+ """
88
+ And the output should contain:
89
+ """
90
+ before all hook handled
91
+ """
92
+ And the output should contain:
93
+ """
94
+ after all hook handled
95
+ """
@@ -0,0 +1,58 @@
1
+ Feature: Multiple hookfiles with a glob
2
+
3
+ Background:
4
+ Given I have "dredd-hooks-ruby" command installed
5
+ And I have "dredd" command installed
6
+ And a file named "server.rb" with:
7
+ """
8
+ require 'sinatra'
9
+ get '/message' do
10
+ "Hello World!\n\n"
11
+ end
12
+ """
13
+
14
+ And a file named "apiary.apib" with:
15
+ """
16
+ # My Api
17
+ ## GET /message
18
+ + Response 200 (text/html;charset=utf-8)
19
+ Hello World!
20
+ """
21
+
22
+ @debug
23
+ Scenario:
24
+ Given a file named "hookfile1.rb" with:
25
+ """
26
+ include DreddHooks::Methods
27
+ before("/message > GET") do |transaction|
28
+ puts "It's me, File1"
29
+ end
30
+ """
31
+ And a file named "hookfile2.rb" with:
32
+ """
33
+ include DreddHooks::Methods
34
+ before("/message > GET") do |transaction|
35
+ puts "It's me, File2"
36
+ end
37
+ """
38
+ And a file named "hookfile_to_be_globed.rb" with:
39
+ """
40
+ include DreddHooks::Methods
41
+ before("/message > GET") do |transaction|
42
+ puts "It's me, File3"
43
+ end
44
+ """
45
+ When I run `dredd ./apiary.apib http://localhost:4567 --server "ruby server.rb" --language dredd-hooks-ruby --hookfiles ./hookfile1.rb --hookfiles ./hookfile2.rb --hookfiles ./hookfile_*.rb`
46
+ Then the exit status should be 0
47
+ And the output should contain:
48
+ """
49
+ It's me, File1
50
+ """
51
+ And the output should contain:
52
+ """
53
+ It's me, File2
54
+ """
55
+ And the output should contain:
56
+ """
57
+ It's me, File3
58
+ """
@@ -0,0 +1,42 @@
1
+ require 'json'
2
+ require 'socket'
3
+
4
+
5
+ Given(/^I have "([^"]*)" command installed$/) do |command|
6
+ is_present = system("which #{ command} > /dev/null 2>&1")
7
+ raise "Command #{command} is not present in the system" if not is_present
8
+ end
9
+
10
+ Given(/^server under test is running$/) do
11
+ end
12
+
13
+ Then(/^It should start listening on localhost port "([^"]*)"$/) do |port|
14
+ @client = TCPSocket.new 'localhost', port
15
+ @client.close
16
+ end
17
+
18
+ Given(/^I connect to the server$/) do
19
+ @client = TCPSocket.new 'localhost', 61321
20
+ end
21
+
22
+ When(/^I send a JSON message to the socket:$/) do |string|
23
+ @data_sent = string
24
+ @client.send @data_sent, 0
25
+ end
26
+
27
+ When(/^I send a newline character as a message delimiter to the socket$/) do
28
+ @client.send "\n", 0
29
+ end
30
+
31
+ Then(/^I should receive same response$/) do
32
+ sleep 1
33
+ data_received = @client.readline
34
+ if JSON.parse(data_received) != JSON.parse(@data_sent)
35
+ @client.close!
36
+ raise "Data received:\n#{data_received}\nDoesn't match data sent: #{@data_sent}\n"
37
+ end
38
+ end
39
+
40
+ Then(/^I should be able to gracefully disconnect$/) do
41
+ @client.close
42
+ end
@@ -0,0 +1,12 @@
1
+ require 'aruba/cucumber'
2
+ require "sinatra/base"
3
+
4
+ Before do
5
+ puts "Killing server..."
6
+ system "for i in `ps axu | grep 'server.rb' | grep ruby | awk '{print $2}'`; do kill -9 $i; done > /dev/null 2>&1"
7
+ puts "Killing handler..."
8
+ system "for i in `ps axu | grep 'dredd-hooks' | grep ruby | awk '{print $2}'`; do kill -9 $i; done > /dev/null 2>&1"
9
+ sleep 3
10
+
11
+ @aruba_timeout_seconds = 10
12
+ end
@@ -0,0 +1,5 @@
1
+ require 'sinatra'
2
+
3
+ get '/message' do
4
+ "Hello World!"
5
+ end
@@ -0,0 +1,66 @@
1
+ Feature: TCP server and messages
2
+
3
+ Scenario: TCP server
4
+ When I run `dredd-hooks-ruby` interactively
5
+ And I wait for output to contain "Starting"
6
+ Then It should start listening on localhost port "61321"
7
+
8
+ Scenario: Message exchange for event beforeEach
9
+ Given I run `dredd-hooks-ruby` interactively
10
+ When I wait for output to contain "Starting"
11
+ And I connect to the server
12
+ And I send a JSON message to the socket:
13
+ """
14
+ {"event": "beforeEach", "uuid": "1234-abcd", "data": {"key":"value"}}
15
+ """
16
+ And I send a newline character as a message delimiter to the socket
17
+ Then I should receive same response
18
+ And I should be able to gracefully disconnect
19
+
20
+ Scenario: Message exchange for event beforeEachValidation
21
+ Given I run `dredd-hooks-ruby` interactively
22
+ When I wait for output to contain "Starting"
23
+ And I connect to the server
24
+ And I send a JSON message to the socket:
25
+ """
26
+ {"event": "beforeEachValidation", "uuid": "2234-abcd", "data": {"key":"value"}}
27
+ """
28
+ And I send a newline character as a message delimiter to the socket
29
+ Then I should receive same response
30
+ And I should be able to gracefully disconnect
31
+
32
+ Scenario: Message exchange for event afterEach
33
+ Given I run `dredd-hooks-ruby` interactively
34
+ When I wait for output to contain "Starting"
35
+ And I connect to the server
36
+ And I send a JSON message to the socket:
37
+ """
38
+ {"event": "afterEach", "uuid": "3234-abcd", "data": {"key":"value"}}
39
+ """
40
+ And I send a newline character as a message delimiter to the socket
41
+ Then I should receive same response
42
+ And I should be able to gracefully disconnect
43
+
44
+ Scenario: Message exchange for event beforeAll
45
+ Given I run `dredd-hooks-ruby` interactively
46
+ When I wait for output to contain "Starting"
47
+ And I connect to the server
48
+ And I send a JSON message to the socket:
49
+ """
50
+ {"event": "beforeAll", "uuid": "4234-abcd", "data": {"key":"value"}}
51
+ """
52
+ And I send a newline character as a message delimiter to the socket
53
+ Then I should receive same response
54
+ And I should be able to gracefully disconnect
55
+
56
+ Scenario: Message exchange for event afterAll
57
+ Given I run `dredd-hooks-ruby` interactively
58
+ When I wait for output to contain "Starting"
59
+ And I connect to the server
60
+ And I send a JSON message to the socket:
61
+ """
62
+ {"event": "afterAll", "uuid": "5234-abcd", "data": {"key":"value"}}
63
+ """
64
+ And I send a newline character as a message delimiter to the socket
65
+ Then I should receive same response
66
+ And I should be able to gracefully disconnect
@@ -0,0 +1,6 @@
1
+ require "dredd_hooks/version"
2
+
3
+ require File.join(File.dirname(__FILE__), './dredd_hooks/methods.rb')
4
+ require File.join(File.dirname(__FILE__), './dredd_hooks/runner.rb')
5
+ require File.join(File.dirname(__FILE__), './dredd_hooks/file_loader.rb')
6
+ require File.join(File.dirname(__FILE__), './dredd_hooks/server.rb')
@@ -0,0 +1,18 @@
1
+ module DreddHooks
2
+ module FileLoader
3
+ def self.unique_paths globs
4
+ paths = []
5
+ globs.each do |glob|
6
+ paths += Dir.glob glob
7
+ end
8
+ paths.uniq
9
+ end
10
+
11
+ def self.load globs
12
+ self.unique_paths(globs).each do |path|
13
+ puts path
14
+ require path
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,53 @@
1
+ module DreddHooks
2
+ module Methods
3
+ @@before_hooks = {}
4
+ @@before_validation_hooks = {}
5
+ @@after_hooks = {}
6
+
7
+ @@before_each_hooks = []
8
+ @@before_each_validation_hooks = []
9
+ @@after_each_hooks = []
10
+
11
+ @@before_all_hooks = []
12
+ @@after_all_hooks = []
13
+
14
+ #
15
+ # Ruby hooks API
16
+ #
17
+
18
+ def before transaction_name, &block
19
+ @@before_hooks[transaction_name] = [] if @@before_hooks[transaction_name].nil?
20
+ @@before_hooks[transaction_name].push block
21
+ end
22
+
23
+ def before_validation transaction_name, &block
24
+ @@before_validation_hooks[transaction_name] = [] if @@before_validation_hooks[transaction_name].nil?
25
+ @@before_validation_hooks[transaction_name].push block
26
+ end
27
+
28
+ def after transaction_name, &block
29
+ @@after_hooks[transaction_name] = [] if @@after_hooks[transaction_name].nil?
30
+ @@after_hooks[transaction_name].push block
31
+ end
32
+
33
+ def before_each &block
34
+ @@before_each_hooks.push block
35
+ end
36
+
37
+ def before_each_validation &block
38
+ @@before_each_validation_hooks.push block
39
+ end
40
+
41
+ def after_each &block
42
+ @@after_each_hooks.push block
43
+ end
44
+
45
+ def before_all &block
46
+ @@before_all_hooks.push block
47
+ end
48
+
49
+ def after_all &block
50
+ @@after_all_hooks.push block
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,84 @@
1
+ module DreddHooks
2
+ module Runner
3
+
4
+ #
5
+ # Runers for Transaction specific hooks
6
+ #
7
+
8
+ def self.run_before_hooks_for_transaction transaction
9
+ transaction_name = transaction["name"]
10
+ hooks = Methods.class_variable_get("@@before_hooks")[transaction_name]
11
+ if hooks.kind_of? Array
12
+ hooks.each do |hook_proc|
13
+ hook_proc.call transaction
14
+ end
15
+ end
16
+ return transaction
17
+ end
18
+
19
+ def self.run_before_validation_hooks_for_transaction transaction
20
+ transaction_name = transaction["name"]
21
+ hooks = Methods.class_variable_get("@@before_validation_hooks")[transaction_name]
22
+ if hooks.kind_of? Array
23
+ hooks.each do |hook_proc|
24
+ hook_proc.call transaction
25
+ end
26
+ end
27
+ return transaction
28
+ end
29
+
30
+ def self.run_after_hooks_for_transaction transaction
31
+ transaction_name = transaction["name"]
32
+ hooks = Methods.class_variable_get("@@after_hooks")[transaction_name]
33
+ if hooks.kind_of? Array
34
+ hooks.each do |hook_proc|
35
+ hook_proc.call transaction
36
+ end
37
+ end
38
+ return transaction
39
+ end
40
+
41
+ #
42
+ # Runners for *_each hooks API
43
+ #
44
+
45
+ def self.run_before_each_hooks_for_transaction transaction
46
+ Methods.class_variable_get("@@before_each_hooks").each do |hook_proc|
47
+ hook_proc.call transaction
48
+ end
49
+ return transaction
50
+ end
51
+
52
+ def self.run_before_each_validation_hooks_for_transaction transaction
53
+ Methods.class_variable_get("@@before_each_validation_hooks").each do |hook_proc|
54
+ hook_proc.call transaction
55
+ end
56
+ return transaction
57
+ end
58
+
59
+ def self.run_after_each_hooks_for_transaction transaction
60
+ Methods.class_variable_get("@@after_each_hooks").each do |hook_proc|
61
+ hook_proc.call transaction
62
+ end
63
+ return transaction
64
+ end
65
+
66
+ #
67
+ # Runners for *_all hooks API
68
+ #
69
+
70
+ def self.run_before_all_hooks_for_transactions transactions
71
+ Methods.class_variable_get("@@before_all_hooks").each do |hook_proc|
72
+ hook_proc.call transactions
73
+ end
74
+ return transactions
75
+ end
76
+
77
+ def self.run_after_all_hooks_for_transactions transactions
78
+ Methods.class_variable_get("@@after_all_hooks").each do |hook_proc|
79
+ hook_proc.call transactions
80
+ end
81
+ return transactions
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,86 @@
1
+ require 'socket'
2
+ require 'json'
3
+
4
+ module DreddHooks
5
+ class Server
6
+ #
7
+ # The hooks worker server
8
+ #
9
+
10
+ HOST = '127.0.0.1'
11
+ PORT = 61321
12
+ MESSAGE_DELIMITER = "\n"
13
+
14
+ @server = nil
15
+
16
+ def process_message message, client
17
+ event = message['event']
18
+ data = message['data']
19
+
20
+ if event == "beforeEach"
21
+ data = DreddHooks::Runner.run_before_each_hooks_for_transaction data
22
+ data = DreddHooks::Runner.run_before_hooks_for_transaction data
23
+ end
24
+
25
+ if event == "beforeEachValidation"
26
+ data = DreddHooks::Runner.run_before_each_validation_hooks_for_transaction data
27
+ data = DreddHooks::Runner.run_before_validation_hooks_for_transaction data
28
+ end
29
+
30
+ if event == "afterEach"
31
+ data = DreddHooks::Runner.run_after_hooks_for_transaction data
32
+ data = DreddHooks::Runner.run_after_each_hooks_for_transaction data
33
+ end
34
+
35
+ if event == "beforeAll"
36
+ data = DreddHooks::Runner.run_before_all_hooks_for_transactions data
37
+ end
38
+
39
+ if event == "afterAll"
40
+ data = DreddHooks::Runner.run_after_all_hooks_for_transactions data
41
+ end
42
+
43
+ to_send = {
44
+ "uuid" => message['uuid'],
45
+ "event" => event,
46
+ "data" => data
47
+ }.to_json
48
+ client.puts to_send + "\n"
49
+ end
50
+
51
+ def run
52
+ @server = TCPServer.new HOST, PORT
53
+ loop do
54
+ #Thread.abort_on_exception=true
55
+ client = @server.accept
56
+ STDERR.puts 'Dredd connected to Ruby Dredd hooks worker'
57
+ buffer = ""
58
+ while (data = client.recv(10))
59
+ buffer += data
60
+ if buffer.include? MESSAGE_DELIMITER
61
+ splitted_buffer = buffer.split(MESSAGE_DELIMITER)
62
+ buffer = ""
63
+
64
+ messages = []
65
+
66
+ splitted_buffer.each do |message|
67
+ begin
68
+ messages.push JSON.parse(message)
69
+
70
+ rescue JSON::ParserError
71
+ # if message aftger delimiter is not parseable json, it's
72
+ # a chunk of next message, put it back to the buffer
73
+ buffer += message
74
+ end
75
+ end
76
+
77
+ messages.each do |message|
78
+ process_message message, client
79
+ end
80
+ end
81
+ end
82
+ client.close
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module DreddHooks
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dredd_hooks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Kliment
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aruba
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.6.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.6.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: sinatra
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.4.5
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.4.5
69
+ description: Write Dredd hooks in Ruby to glue together API Blueprint with your Ruby
70
+ project
71
+ email:
72
+ - adam@apiary.io
73
+ executables:
74
+ - dredd-hooks-ruby
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - .travis.yml
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/dredd-hooks-ruby
85
+ - dredd_hooks.gemspec
86
+ - features/execution_order.feature
87
+ - features/failing_transaction.feature
88
+ - features/hook_handlers.feature
89
+ - features/multiple_hookfiles.feature
90
+ - features/step_definitions/dredd_steps.rb
91
+ - features/support/env.rb
92
+ - features/support/server.rb
93
+ - features/tcp_server.feature
94
+ - lib/dredd_hooks.rb
95
+ - lib/dredd_hooks/file_loader.rb
96
+ - lib/dredd_hooks/methods.rb
97
+ - lib/dredd_hooks/runner.rb
98
+ - lib/dredd_hooks/server.rb
99
+ - lib/dredd_hooks/version.rb
100
+ homepage: https://github.com/apiaryio/dredd-hooks-ruby
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.4.1
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Ruby Hooks Handler for Dredd API Testing Framework
124
+ test_files:
125
+ - features/execution_order.feature
126
+ - features/failing_transaction.feature
127
+ - features/hook_handlers.feature
128
+ - features/multiple_hookfiles.feature
129
+ - features/step_definitions/dredd_steps.rb
130
+ - features/support/env.rb
131
+ - features/support/server.rb
132
+ - features/tcp_server.feature