Skip to content

Instantly share code, notes, and snippets.

@kfarst
Created February 18, 2016 03:06
Show Gist options
  • Save kfarst/4ecd5c4b52f7a67f5116 to your computer and use it in GitHub Desktop.
Save kfarst/4ecd5c4b52f7a67f5116 to your computer and use it in GitHub Desktop.
module Callable
def call(*args, &block)
new(*args).call(&block)
end
module WithLogging
def call(*args, &block)
with_logging(*args) do
new(*args).call(&block)
end
end
def with_logging(*args)
filters = Rails.application.config.filter_parameters
f = ActionDispatch::Http::ParameterFilter.new filters
output_log("Calling service: ") { f.filter(convert_to_hash(args)).to_json }
result = yield
output_log("Service result: ") { f.filter(convert_to_hash(result)).to_json }
result
end
private
def convert_to_hash(value)
if value.is_a?(Hash)
value
else
{result: value}
end
end
def output_log(preamble)
Rails.logger.info "#{preamble}: #{self.name} -- #{yield}"
rescue
Rails.logger.error "#{preamble}: #{self.name} -- failed to serialize message"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment