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

Constants included from FlowChat::Instrumentation

FlowChat::Instrumentation::DELIVERED_MESSAGE_ID_KEY, FlowChat::Instrumentation::DELIVERY_DURATION_KEY

Instance Attribute Summary collapse

Attributes included from GatewayAsyncSupport

#context, #controller

Class Method Summary collapse

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?, #side_events_already_published?

Methods included from FlowChat::Instrumentation

#elapsed_ms_since, #inbound_message?, #instrument, instrument, report_api_error, #report_delivery_failure, #report_delivery_success, #report_to_app, #report_to_subscribers

Constructor Details

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

Returns a new instance of IntercomApi.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/flow_chat/intercom/gateway/intercom_api.rb', line 27

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.



11
12
13
# File 'lib/flow_chat/intercom/gateway/intercom_api.rb', line 11

def client
  @client
end

Class Method Details

.configure_middleware_stack(builder, custom_middleware) ⇒ Object

Configure Intercom specific middleware stack



17
18
19
20
21
22
23
24
25
# File 'lib/flow_chat/intercom/gateway/intercom_api.rb', line 17

def self.configure_middleware_stack(builder, custom_middleware)
  FlowChat.logger.debug { "IntercomApi: Configuring Intercom middleware stack" }

  builder.use custom_middleware
  FlowChat.logger.debug { "IntercomApi: Added custom middleware" }

  builder.use FlowChat::Intercom::Middleware::ChoiceMapper
  FlowChat.logger.debug { "IntercomApi: Added Intercom::Middleware::ChoiceMapper" }
end

Instance Method Details

#call(context) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/flow_chat/intercom/gateway/intercom_api.rb', line 39

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