From 08670860dc388a084fc5ca6ca3d267e927db59d2 Mon Sep 17 00:00:00 2001 From: Wender Freese Date: Wed, 29 Jan 2020 15:33:57 -0300 Subject: [PATCH] Add Event driven Instrumentation (#1) --- lib/recaptcha.rb | 4 ++++ lib/recaptcha/instrumentation.rb | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 lib/recaptcha/instrumentation.rb diff --git a/lib/recaptcha.rb b/lib/recaptcha.rb index d8e30c5f..05c63b3f 100644 --- a/lib/recaptcha.rb +++ b/lib/recaptcha.rb @@ -8,6 +8,7 @@ require 'recaptcha/helpers' require 'recaptcha/adapters/controller_methods' require 'recaptcha/adapters/view_methods' +require 'recaptcha/instrumentation' if defined?(Rails) require 'recaptcha/railtie' end @@ -99,6 +100,9 @@ def self.verify_via_api_call_free(response, options) verify_hash['remoteip'] = options[:remote_ip] if options.key?(:remote_ip) reply = api_verification_free(verify_hash, timeout: options[:timeout], json: options[:json]) + Instrumentation.report( + { token: response, options: options, response: reply } + ) success = reply['success'].to_s == 'true' && hostname_valid?(reply['hostname'], options[:hostname]) && action_valid?(reply['action'], options[:action]) && diff --git a/lib/recaptcha/instrumentation.rb b/lib/recaptcha/instrumentation.rb new file mode 100644 index 00000000..dc38b226 --- /dev/null +++ b/lib/recaptcha/instrumentation.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Recaptcha + class Instrumentation + CHANNEL = 'Recaptcha::API::Response' + + def self.report(payload = {}) + ActiveSupport::Notifications.instrument(CHANNEL, payload) + end + + def self.subscribe(&block) + ActiveSupport::Notifications.subscribe(CHANNEL) do |*args| + event = ActiveSupport::Notifications::Event.new(*args) + + yield event if block_given? + end + end + end +end