Class: SimpleConnect::Api::Events

Inherits:
Object
  • Object
show all
Includes:
ResponseAttachment
Defined in:
lib/simple_connect/api/events.rb

Overview

Events resource. Holds the shared Request and the endpoint URI; exposes #deliver (POST a domain event) and #detail (GET a previously-ingested event by its event_id).

Event-key validation is opt-in. Pass ‘event_keys:` to Client.new to enforce a whitelist client-side; omit it and the server becomes the source of truth (unknown keys return 422 from the server). The gem ships no hardcoded event list — each provider owns its own taxonomy.

Constant Summary collapse

DEFAULT_LANGUAGE =
"en"

Instance Method Summary collapse

Constructor Details

#initialize(request:, endpoint_uri:, event_keys: nil) ⇒ Events

Returns a new instance of Events.



18
19
20
21
22
# File 'lib/simple_connect/api/events.rb', line 18

def initialize(request:, endpoint_uri:, event_keys: nil)
  @request      = request
  @endpoint_uri = endpoint_uri
  @event_keys   = event_keys&.map(&:to_s)&.freeze
end

Instance Method Details

#deliver(event_key, fields = {}, event_id: nil, language: DEFAULT_LANGUAGE, occurred_at: nil, **extra_fields) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simple_connect/api/events.rb', line 24

def deliver(event_key, fields = {}, event_id: nil, language: DEFAULT_LANGUAGE, occurred_at: nil, **extra_fields)
  event_key = event_key.to_s
  raise ArgumentError, "event_key is required" if event_key.empty?
  if @event_keys && !@event_keys.include?(event_key)
    raise SimpleConnect::UnknownEventError,
          "Unknown event_key '#{event_key}'. Must be one of: #{@event_keys.join(", ")}"
  end

  merged_fields = stringify_keys(fields).merge(stringify_keys(extra_fields))
  body = build_body(event_key, merged_fields, event_id: event_id, language: language, occurred_at: occurred_at)
  attach_response(@request.post(@endpoint_uri, body: body), Responses::DeliverResponse)
end

#detail(event_id) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
# File 'lib/simple_connect/api/events.rb', line 37

def detail(event_id)
  raise ArgumentError, "event_id is required" if event_id.to_s.strip.empty?

  attach_response(@request.get(detail_uri(event_id)), Responses::EventResponse)
end