Skip to content

Commit

Permalink
[WIP] Refactoring formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucca Baratera committed May 29, 2020
1 parent 534f6b2 commit 8fd2e43
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 78 deletions.
20 changes: 12 additions & 8 deletions lib/heartcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ class << self
# @attr [Array<Checks>] the checks to use when checking
attr_accessor :checks

# @attr [Heartcheck::Executors::Base] or [Heartcheck::Executors::HashResponse]
# the checks executor backend
# @attr [Heartcheck::Executors::Base] the checks executor backend
attr_accessor :executor

# @attr [Boolean] the option for using a hash response or an array response
attr_reader :hash_response
attr_reader :hash_formatter

# @attr_writer [Object] change the default logger
attr_writer :logger
Expand All @@ -43,7 +42,7 @@ def logger
#
# @return [void]
def setup(options = {})
@hash_response = options.fetch(:hash_response, false)
@hash_formatter = options.fetch(:hash_formatter, false)
yield(self)
end

Expand Down Expand Up @@ -100,13 +99,18 @@ def info_checks
checks.select { |ctx| ctx.respond_to?(:info) }
end

# an executor class that respond to execute(checkers)
#
# @return [Heartcheck::Services::ResponseFormatter] or nil
def formatter
@formatter ||= Heartcheck::Services::ResponseFormatter.new(hash_formatter)
end

# an executor class that respond to dispatch(checkers)
#
# @return [Heartcheck::Executors::Base] or [Heartcheck::Executors::HashResponse]
# @return [Heartcheck::Executors::Base]
def executor
@executor ||= hash_response ?
Heartcheck::Executors::HashResponse.new :
Heartcheck::Executors::Base.new
@executor ||= Heartcheck::Executors::Base.new
end

# change current executor to a threaded implementation
Expand Down
6 changes: 5 additions & 1 deletion lib/heartcheck/controllers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ def index
protected

def check(who)
MultiJson.dump(executor.dispatch(Heartcheck.send("#{who}_checks")))
checks = executor.dispatch(Heartcheck.send("#{who}_checks"))
MultiJson.dump(formatter.execute(checks))
end

private
def formatter
Heartcheck.formatter
end

def executor
Heartcheck.executor
Expand Down
20 changes: 0 additions & 20 deletions lib/heartcheck/executors/hash_response.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/heartcheck/services.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require 'heartcheck/services/firewall'
require 'heartcheck/services/response_formatter'
30 changes: 30 additions & 0 deletions lib/heartcheck/services/response_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Heartcheck
module Services
class ResponseFormatter
attr_reader :hash_formatter

def initialize(hash_formatter)
@hash_formatter = hash_formatter
end

def execute(checks)
return checks unless hash_formatter

checks.map do |check|
format(check)
end.reduce({}, :merge)
end

private

def format(check)
check.tap do |obj|
value = obj.values.first.merge(Hash[:time => obj[:time]])
obj.delete(:time)
obj.merge!(Hash[obj.keys.first => value])
end
end
end
end
end

39 changes: 0 additions & 39 deletions spec/lib/heartcheck/executors/hash_response_spec.rb

This file was deleted.

25 changes: 25 additions & 0 deletions spec/lib/heartcheck/services/response_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

describe Heartcheck::Services::ResponseFormatter do
describe "#execute" do
let(:check_01) { { dummy1: { status: :ok } , time: 1100 } }
let(:check_02) { { dummy2: { status: :ok } , time: 100 } }
let(:hash_formatter_true) { described_class.new(true) }
let(:hash_formatter_false) { described_class.new(false) }

it "when hash_formatter is true" do
expect(hash_formatter_true.execute([check_01,check_02])).to be_eql(
{:dummy1=>{:status=>:ok, :time=>1100},
:dummy2=>{:status=>:ok, :time=>100}})
end

it "when hash_formatter is false" do
expect(hash_formatter_false.execute([check_01,check_02])).to be_eql([
{:dummy1=>{:status=>:ok}, :time=>1100},
{:dummy2=>{:status=>:ok}, :time=>100}
])
end

end

end
19 changes: 9 additions & 10 deletions spec/lib/heartcheck_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'heartcheck/executors/hash_response'
describe Heartcheck do
let(:essential) { Heartcheck::Checks::Base.new }
let(:functional) { Heartcheck::Checks::Base.new.tap { |c| c.functional = true } }
Expand All @@ -22,32 +21,32 @@
end
end

describe '#executor' do
context 'with hash_response' do
before(:each) { described_class.instance_variable_set :@executor, nil }
it 'returns a Heartcheck::Executors::HashResponse' do
described_class.setup hash_response: true do |monitor|
expect(monitor.executor).to be_a(Heartcheck::Executors::HashResponse)
describe '#format' do
context 'with default' do
it 'returns a Heartcheck::Services::ResponseFormatter' do
described_class.setup do |monitor|
expect(monitor.formatter).to be_a(Heartcheck::Services::ResponseFormatter)
end
end
end
end

describe '#executor' do
context 'with default' do
before(:each) { described_class.instance_variable_set :@executor, nil }
it 'returns a Heartcheck::Executors::Base' do
described_class.setup do |monitor|
expect(monitor.executor).to be_a(Heartcheck::Executors::Base)
end
end
end

context 'with custom' do
context 'with custom' do
it 'returns a custom custom executor' do
described_class.executor = 'lala'
expect(described_class.executor).to be_a(String)
end
end

context 'with threaded' do
it 'returns a threaded executor' do
described_class.use_threaded_executor!
Expand Down

0 comments on commit 8fd2e43

Please sign in to comment.