Class: ApiAlerts::Client
- Inherits:
-
Object
- Object
- ApiAlerts::Client
- Defined in:
- lib/apialerts/client.rb
Overview
Instance-based API Alerts client.
Use the module-level methods (ApiAlerts.configure / ApiAlerts.send_async) for a convenient global singleton, or construct this class directly when you need multiple clients or full lifecycle control.
Instance Method Summary collapse
-
#initialize(api_key, debug: false) ⇒ Client
constructor
A new instance of Client.
-
#send(event, api_key: nil) ⇒ Object
Send an event - fire-and-forget.
-
#send_async(event, api_key: nil) ⇒ Object
Send an event and return a SendResult.
-
#set_overrides(integration, version, base_url) ⇒ Object
Override the integration name, version, and base URL.
Constructor Details
#initialize(api_key, debug: false) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 22 |
# File 'lib/apialerts/client.rb', line 16 def initialize(api_key, debug: false) @api_key = api_key @debug = debug @integration = INTEGRATION_NAME @integration_version = VERSION @base_url = API_URL end |
Instance Method Details
#send(event, api_key: nil) ⇒ Object
Send an event - fire-and-forget. Never raises. Critical errors (not configured, missing key, empty message) are always logged to stderr. HTTP errors and success are only logged when debug is enabled.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/apialerts/client.rb', line 39 def send(event, api_key: nil) key = api_key.nil? || api_key.empty? ? @api_key : api_key if key.nil? || key.empty? warn 'x (apialerts.com) Error: api key is missing' return end if event..nil? || event..empty? warn 'x (apialerts.com) Error: message is required' return end result = post(event, api_key: key) return unless @debug if result.success? warn "✓ (apialerts.com) Alert sent to #{result.workspace} (#{result.channel})" result.warnings.each { |w| warn "! (apialerts.com) Warning: #{w}" } else warn "x (apialerts.com) Error: #{result.error}" end end |
#send_async(event, api_key: nil) ⇒ Object
Send an event and return a SendResult. Never raises. Check result.success? to determine whether delivery succeeded.
63 64 65 66 67 68 69 |
# File 'lib/apialerts/client.rb', line 63 def send_async(event, api_key: nil) key = api_key.nil? || api_key.empty? ? @api_key : api_key return SendResult.new(success: false, error: 'api key is missing') if key.nil? || key.empty? return SendResult.new(success: false, error: 'message is required') if event..nil? || event..empty? post(event, api_key: key) end |
#set_overrides(integration, version, base_url) ⇒ Object
Override the integration name, version, and base URL.
Used by official integrations and in tests to redirect requests to a mock server.
28 29 30 31 32 33 |
# File 'lib/apialerts/client.rb', line 28 def set_overrides(integration, version, base_url) @integration = integration @integration_version = version @base_url = base_url self end |