|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Hooks |
| 4 | + module Core |
| 5 | + # Shared module providing access to global components (logger, stats, failbot) |
| 6 | + # |
| 7 | + # This module provides a consistent interface for accessing global components |
| 8 | + # across all plugin types, eliminating code duplication and ensuring consistent |
| 9 | + # behavior throughout the application. |
| 10 | + # |
| 11 | + # @example Usage in a class that needs instance methods |
| 12 | + # class MyHandler |
| 13 | + # include Hooks::Core::ComponentAccess |
| 14 | + # |
| 15 | + # def process |
| 16 | + # log.info("Processing request") |
| 17 | + # stats.increment("requests.processed") |
| 18 | + # failbot.report("Error occurred") if error? |
| 19 | + # end |
| 20 | + # end |
| 21 | + # |
| 22 | + # @example Usage in a class that needs class methods |
| 23 | + # class MyValidator |
| 24 | + # extend Hooks::Core::ComponentAccess |
| 25 | + # |
| 26 | + # def self.validate |
| 27 | + # log.info("Validating request") |
| 28 | + # stats.increment("requests.validated") |
| 29 | + # end |
| 30 | + # end |
| 31 | + module ComponentAccess |
| 32 | + # Short logger accessor |
| 33 | + # @return [Hooks::Log] Logger instance for logging messages |
| 34 | + # |
| 35 | + # Provides a convenient way to log messages without needing |
| 36 | + # to reference the full Hooks::Log namespace. |
| 37 | + # |
| 38 | + # @example Logging an error |
| 39 | + # log.error("Something went wrong") |
| 40 | + def log |
| 41 | + Hooks::Log.instance |
| 42 | + end |
| 43 | + |
| 44 | + # Global stats component accessor |
| 45 | + # @return [Hooks::Plugins::Instruments::Stats] Stats instance for metrics reporting |
| 46 | + # |
| 47 | + # Provides access to the global stats component for reporting metrics |
| 48 | + # to services like DataDog, New Relic, etc. |
| 49 | + # |
| 50 | + # @example Recording a metric |
| 51 | + # stats.increment("webhook.processed", { handler: "MyHandler" }) |
| 52 | + def stats |
| 53 | + Hooks::Core::GlobalComponents.stats |
| 54 | + end |
| 55 | + |
| 56 | + # Global failbot component accessor |
| 57 | + # @return [Hooks::Plugins::Instruments::Failbot] Failbot instance for error reporting |
| 58 | + # |
| 59 | + # Provides access to the global failbot component for reporting errors |
| 60 | + # to services like Sentry, Rollbar, etc. |
| 61 | + # |
| 62 | + # @example Reporting an error |
| 63 | + # failbot.report("Something went wrong", { context: "additional info" }) |
| 64 | + def failbot |
| 65 | + Hooks::Core::GlobalComponents.failbot |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments