Skip to content

Commit

Permalink
Merge branch 'gonzalo-bulnes/refactor-make-code-more-idiomatic'
Browse files Browse the repository at this point in the history
As a Ruby developer
In order to make the gem maintenance easier
I want its code to be as simple as possible
And as idiomatic as possible

Fixes #9
  • Loading branch information
gonzalo-bulnes committed Jul 27, 2016
2 parents ab5d2d8 + 3e19d75 commit 9bcd282
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 87 deletions.
1 change: 1 addition & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 7 additions & 8 deletions dredd_hooks.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ["[email protected]"]
spec.authors = ["Adam Kliment", "Gonzalo Bulnes Guilpain"]
spec.email = ["[email protected]", "[email protected]"]
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
17 changes: 9 additions & 8 deletions features/execution_order.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions features/failing_transaction.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down
1 change: 1 addition & 0 deletions features/hook_handlers.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
5 changes: 4 additions & 1 deletion features/multiple_hookfiles.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@ 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"
end
"""
And a file named "hookfile2.rb" with:
"""
require 'dredd_hooks/methods'
include DreddHooks::Methods
before("/message > GET") do |transaction|
puts "It's me, File2"
end
"""
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"
Expand All @@ -55,4 +58,4 @@ Feature: Multiple hookfiles with a glob
And the output should contain:
"""
It's me, File3
"""
"""
9 changes: 4 additions & 5 deletions lib/dredd_hooks.rb
Original file line number Diff line number Diff line change
@@ -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')
22 changes: 12 additions & 10 deletions lib/dredd_hooks/file_loader.rb
Original file line number Diff line number Diff line change
@@ -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
end

58 changes: 27 additions & 31 deletions lib/dredd_hooks/runner.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
require 'dredd_hooks/methods'

module DreddHooks
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
Expand All @@ -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
Expand All @@ -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
end
Loading

0 comments on commit 9bcd282

Please sign in to comment.