Skip to content

Commit

Permalink
Merge pull request #23 from gonzalo-bulnes/gonzalo-bulnes/refactor-en…
Browse files Browse the repository at this point in the history
…sure-server-can-be-used-as-library

Refactor ensure DreddHooks::Server can be used as library
  • Loading branch information
gonzalo-bulnes committed Aug 6, 2016
2 parents 8e7df2a + 94e0214 commit 6c6efc1
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 12 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.1.0] - [Unreleased]
## 0.1.0 - [Unreleased]

### Added

- This change log : )
- Possibility to use DreddHooks::Server as a library (see DreddHooks::CLI for an example)

### Changed

Expand All @@ -27,8 +28,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

The original [proof of concept][poc] was written by @netmilk.


[Unreleased]: https://github.com/gonzalo-bulnes/dredd-hooks-ruby/compare/v0.0.1...gonzalo-bulnes:gonzalo-bulnes/refactor-ensure-server-can-be-used-as-library?expand=1
[poc]: https://github.com/gonzalo-bulnes/dredd-rack/issues/7#issue-70936733
[0.1.0]: https://github.com/gonzalo-bulnes/dredd-hooks-ruby/compare/v0.0.1...v0.1.0

## Inspiration

Expand Down
1 change: 0 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ PLATFORMS

DEPENDENCIES
aruba (~> 0.6.2)
bundler (~> 1.6)
dredd_hooks!
rake (~> 10.0)
rspec (~> 3.0)
Expand Down
1 change: 0 additions & 1 deletion bin/dredd-hooks-ruby
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ $LOAD_PATH.push File.join(File.dirname(__FILE__), "/../lib" )
require 'dredd_hooks/cli'

DreddHooks::CLI.start(ARGV)

1 change: 0 additions & 1 deletion dredd_hooks.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Gem::Specification.new do |spec|
spec.test_files = Dir["{features,spec}/**/*"]

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 "rspec", "~> 3.0"
spec.add_development_dependency "sinatra", "~> 1.4.5"
Expand Down
9 changes: 2 additions & 7 deletions lib/dredd_hooks/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
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
def self.start(error=STDERR, out=STDOUT, files)

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

# Run the server

out.puts 'Starting Ruby Dredd Hooks Worker...'
server = DreddHooks::Server.new(error, out)
server.run
Expand Down
9 changes: 9 additions & 0 deletions lib/dredd_hooks/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def initialize(error=STDERR, out=STDOUT)
end

def run
disable_buffering(out)
loop do
client = server.accept
out.puts 'Dredd connected to Ruby Dredd hooks worker'
Expand All @@ -45,6 +46,14 @@ def run

private

# Write to a file (e.g. STDOUT) without delay
#
# See https://stackoverflow.com/q/23001033
# and http://ruby-doc.org/core-2.3.1/IO.html#method-i-sync-3D
def disable_buffering(file)
file.sync = true
end

def process_message(message)
event = message['event']
transaction = message['data']
Expand Down
3 changes: 3 additions & 0 deletions lib/dredd_hooks/server/buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def initialize(message_delimiter)
@message_delimiter = message_delimiter
end

# Empty the buffer.
#
# Returns the buffer content String.
def flush!
content = @content
@content = ""
Expand Down

0 comments on commit 6c6efc1

Please sign in to comment.