Module: Coolhand::WebhookInterceptor

Defined in:
lib/coolhand/webhook_interceptor.rb

Instance Method Summary collapse

Instance Method Details

#intercept_batch_requestObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/coolhand/webhook_interceptor.rb', line 5

def intercept_batch_request
  Rails.logger.info("[Interceptor] #{controller_name}##{action_name}")

  @validator = Coolhand::OpenAi::WebhookValidator.new(request, webhook_secret)

  unless @validator.valid?
    Rails.logger.info("[Interceptor] Webhook validated failed: #{@validator.error_message}")
    head :unauthorized
    return false
  end

  payload = JSON.parse(@validator.payload)

  process_event(payload)
rescue StandardError => e
  Rails.logger.error("[Interceptor] Failed to intercept batch request: #{e.message}")
end

#process_event(payload) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/coolhand/webhook_interceptor.rb', line 27

def process_event(payload)
  event_type = payload["type"]
  event_data = payload["data"]

  case event_type
  when "batch.completed", "batch.failed", "batch.expired", "batch.cancelled"
    Coolhand::OpenAi::BatchResultProcessor.new(event_data: event_data).call
  else
    Rails.logger.info("[Interceptor] Unhandled OpenAI webhook event type: #{event_type}")
  end
end

#webhook_secretObject

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/coolhand/webhook_interceptor.rb', line 23

def webhook_secret
  raise NotImplementedError, "#{self.class} must implement #webhook_secret"
end