Class: FlowChat::Intercom::Gateway::IntercomApi

Inherits:
Object
  • Object
show all
Includes:
GatewayAsyncSupport, FlowChat::Instrumentation
Defined in:
lib/flow_chat/intercom/gateway/intercom_api.rb

Constant Summary collapse

DEFAULT_WEBHOOK_TOPICS =

Default webhook topics to process

["conversation.user.created", "conversation.user.replied"].freeze

Instance Attribute Summary collapse

Attributes included from GatewayAsyncSupport

#context, #controller

Instance Method Summary collapse

Methods included from GatewayAsyncSupport

#async_supported?, #enqueue_async_job, #extract_body_for_background, #extract_headers_for_background, #extract_host, #extract_path, #extract_remote_ip, #in_background?, #should_enqueue_async?

Methods included from FlowChat::Instrumentation

#instrument, instrument, report_api_error

Constructor Details

#initialize(app, config = nil, additional_webhook_topics = nil) ⇒ IntercomApi

Returns a new instance of IntercomApi.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/flow_chat/intercom/gateway/intercom_api.rb', line 19

def initialize(app, config = nil, additional_webhook_topics = nil)
  @app = app
  @config = config || FlowChat::Intercom::Configuration.from_credentials
  @client = FlowChat::Intercom::Client.new(@config)
  # Always include default topics, plus any additional ones
  @allowed_webhook_topics = DEFAULT_WEBHOOK_TOPICS + Array(additional_webhook_topics)

  FlowChat.logger.info { "IntercomApi: Initialized Intercom API gateway" }
  FlowChat.logger.debug { "IntercomApi: Gateway configuration - API base URL: #{@config.api_base_url}" }
  FlowChat.logger.debug { "IntercomApi: Allowed webhook topics: #{@allowed_webhook_topics.inspect}" }
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/flow_chat/intercom/gateway/intercom_api.rb', line 14

def client
  @client
end

Instance Method Details

#call(context) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/flow_chat/intercom/gateway/intercom_api.rb', line 31

def call(context)
  @context = context
  @controller = context.controller
  request = @controller.request

  FlowChat.logger.debug { "IntercomApi: Processing #{request.request_method} request to #{request.path}" }

  # Skip webhook-specific handling in background mode
  unless in_background?
    # Handle webhook URL validation (HEAD request)
    if request.head?
      FlowChat.logger.info { "IntercomApi: Handling webhook URL validation request" }
      return @controller.head :ok
    end
  end

  # Handle webhook notifications (POST request)
  if request.post?
    FlowChat.logger.info { "IntercomApi: Handling webhook notification (background: #{in_background?})" }
    return handle_webhook(context)
  end

  FlowChat.logger.warn { "IntercomApi: Invalid request method or parameters - returning bad request" }
  @controller.head :bad_request
end