Class: FlowChat::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/flow_chat/factory.rb

Overview

Factory provides centralized processor configuration for consistent setup across webhook and background contexts.

Example:

# In config/initializers/flow_chat.rb
FlowChat::Factory.register :whatsapp do |controller|
processor = FlowChat::Processor.new(controller) do |config|
  config.use_gateway FlowChat::Whatsapp::Gateway::CloudApi
  config.use_session_store FlowChat::Session::CacheSessionStore
  config.use_session_config(boundaries: [:flow])
  config.use_async(WhatsAppFlowJob)
end
processor.run(WhatsAppFlow, :start)
end

# In webhook controller
FlowChat::Factory.execute(:whatsapp, controller: self)

# In background job
FlowChat::Factory.execute(:whatsapp, controller: controller)

Defined Under Namespace

Classes: FactoryNotFoundError

Class Method Summary collapse

Class Method Details

.clear!void

This method returns an undefined value.

Clear all registered factories (primarily for testing)



79
80
81
82
# File 'lib/flow_chat/factory.rb', line 79

def clear!
  FlowChat.logger.debug { "Factory: Clearing all registered factories" }
  factories.clear
end

.execute(name, controller:) ⇒ void

This method returns an undefined value.

Execute a registered factory

Examples:

FlowChat::Factory.execute(:whatsapp, controller: self)

Parameters:

  • name (Symbol)

    The factory name

  • controller (Object)

    The controller instance (webhook or background)

Raises:



53
54
55
56
57
58
59
# File 'lib/flow_chat/factory.rb', line 53

def execute(name, controller:)
  factory = factories[name]
  raise FactoryNotFoundError, "Factory '#{name}' not registered" unless factory

  FlowChat.logger.debug { "Factory: Executing factory '#{name}'" }
  factory.call(controller)
end

.register(name, &block) ⇒ void

This method returns an undefined value.

Register a processor factory with a given name

Examples:

FlowChat::Factory.register :whatsapp do |controller|
  processor = FlowChat::Processor.new(controller) do |config|
    config.use_gateway FlowChat::Whatsapp::Gateway::CloudApi
  end
  processor.run(WhatsAppFlow, :start)
end

Parameters:

  • name (Symbol)

    The factory name (e.g., :whatsapp, :intercom)

  • block (Proc)

    The factory block that receives controller



39
40
41
42
# File 'lib/flow_chat/factory.rb', line 39

def register(name, &block)
  FlowChat.logger.debug { "Factory: Registering factory '#{name}'" }
  factories[name] = block
end

.registered?(name) ⇒ Boolean

Check if a factory is registered

Parameters:

  • name (Symbol)

    The factory name

Returns:

  • (Boolean)


65
66
67
# File 'lib/flow_chat/factory.rb', line 65

def registered?(name)
  factories.key?(name)
end

.registered_factoriesArray<Symbol>

Get all registered factory names

Returns:

  • (Array<Symbol>)


72
73
74
# File 'lib/flow_chat/factory.rb', line 72

def registered_factories
  factories.keys
end