Class: ChatSDK::Webhook::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/chat_sdk/webhook/endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chat:, adapter:, adapter_name:) ⇒ Endpoint

Returns a new instance of Endpoint.



8
9
10
11
12
# File 'lib/chat_sdk/webhook/endpoint.rb', line 8

def initialize(chat:, adapter:, adapter_name:)
  @chat = chat
  @adapter = adapter
  @adapter_name = adapter_name
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



6
7
8
# File 'lib/chat_sdk/webhook/endpoint.rb', line 6

def adapter
  @adapter
end

#adapter_nameObject (readonly)

Returns the value of attribute adapter_name.



6
7
8
# File 'lib/chat_sdk/webhook/endpoint.rb', line 6

def adapter_name
  @adapter_name
end

#chatObject (readonly)

Returns the value of attribute chat.



6
7
8
# File 'lib/chat_sdk/webhook/endpoint.rb', line 6

def chat
  @chat
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chat_sdk/webhook/endpoint.rb', line 14

def call(env)
  request = Rack::Request.new(env) if defined?(Rack)

  adapter.verify_request!(request || env)

  ack = adapter.ack_response(request || env)
  return ack if ack

  events = adapter.parse_events(request || env)
  events.each { |event| chat.dispatch(event, adapter_name: adapter_name) }

  [200, {"content-type" => "text/plain"}, [""]]
rescue ChatSDK::SignatureVerificationError => e
  ChatSDK::Log.warn("Signature verification failed: #{e.message}")
  [401, {"content-type" => "text/plain"}, ["Unauthorized"]]
rescue => e
  ChatSDK::Log.error("Webhook error: #{e.message}")
  [200, {"content-type" => "text/plain"}, [""]]
end