Class: Socrates::Core::Dispatcher
- Inherits:
-
Object
- Object
- Socrates::Core::Dispatcher
- Defined in:
- lib/socrates/core/dispatcher.rb
Instance Method Summary collapse
- #conversation_state(user) ⇒ Object
- #dispatch(message, context: {}) ⇒ Object
-
#initialize(adapter:, state_factory:, storage: nil) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #start_conversation(user, state_id, message: nil) ⇒ Object
Constructor Details
#initialize(adapter:, state_factory:, storage: nil) ⇒ Dispatcher
Returns a new instance of Dispatcher.
15 16 17 18 19 20 21 22 23 |
# File 'lib/socrates/core/dispatcher.rb', line 15 def initialize(adapter:, state_factory:, storage: nil) @adapter = adapter @state_factory = state_factory @storage = storage || Socrates.config.storage @logger = Socrates.config.logger @error_handler = Socrates.config.error_handler @error_message = Socrates.config. || DEFAULT_ERROR_MESSAGE end |
Instance Method Details
#conversation_state(user) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/socrates/core/dispatcher.rb', line 56 def conversation_state(user) client_id = @adapter.client_id_from(user: user) return nil unless @storage.has_key?(client_id) state_data = @storage.fetch(client_id) state_data = nil if state_data&.expired? || state_data&.finished? state_data end |
#dispatch(message, context: {}) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/socrates/core/dispatcher.rb', line 25 def dispatch(, context: {}) client_id = @adapter.client_id_from(context: context) channel = @adapter.channel_from(context: context) user = @adapter.user_from(context: context) session = Session.new(client_id: client_id, channel: channel, user: user) do_dispatch(session, ) end |
#start_conversation(user, state_id, message: nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/socrates/core/dispatcher.rb', line 35 def start_conversation(user, state_id, message: nil) client_id = @adapter.client_id_from(user: user) channel = @adapter.channel_from(user: user) session = Session.new(client_id: client_id, channel: channel, user: user) # Now, we assume the user of this code does this check on their own... # return false unless conversation_state(user).nil? # Create state data to match the request. state_data = StateData.new(state_id: state_id, state_action: :ask) persist_state_data(session.client_id, state_data) # Send our initial message if one was passed to us. @adapter.(session, , user) if .present? do_dispatch(session, nil) true end |