Module: Socrates::Core::State
- Included in:
- SampleStates::AskForBirthDate, SampleStates::AskForName, SampleStates::CalculateAge, SampleStates::Dms, SampleStates::EndConversation1, SampleStates::EndConversation2, SampleStates::Expired, SampleStates::GetStarted, SampleStates::Help, SampleStates::NoComprende, SampleStates::RaiseError
- Defined in:
- lib/socrates/core/state.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #ask ⇒ Object
- #end_conversation ⇒ Object
- #initialize(adapter:, session:, data: StateData.new) ⇒ Object
- #listen(_message) ⇒ Object
- #next_state_action ⇒ Object
- #next_state_id ⇒ Object
- #repeat_action ⇒ Object
- #respond(message: nil, template: nil, send_now: false) ⇒ Object
- #send_message(to:, message:) ⇒ Object
- #transition_to(state_id, action: nil, data: {}) ⇒ Object
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
12 13 14 |
# File 'lib/socrates/core/state.rb', line 12 def adapter @adapter end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
12 13 14 |
# File 'lib/socrates/core/state.rb', line 12 def data @data end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
12 13 14 |
# File 'lib/socrates/core/state.rb', line 12 def session @session end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
12 13 14 |
# File 'lib/socrates/core/state.rb', line 12 def user @user end |
Instance Method Details
#ask ⇒ Object
90 91 92 |
# File 'lib/socrates/core/state.rb', line 90 def ask # stub implementation, to be overwritten. end |
#end_conversation ⇒ Object
84 85 86 87 88 |
# File 'lib/socrates/core/state.rb', line 84 def end_conversation @data.clear transition_to StateData::END_OF_CONVERSATION, action: StateData::END_OF_CONVERSATION end |
#initialize(adapter:, session:, data: StateData.new) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/socrates/core/state.rb', line 14 def initialize(adapter:, session:, data: StateData.new) @data = data @adapter = adapter @session = session @user = session.user @next_state_id = nil @next_state_action = nil @logger = Socrates.config.logger || Socrates::Logger.default end |
#listen(_message) ⇒ Object
94 95 96 |
# File 'lib/socrates/core/state.rb', line 94 def listen() # stub implementation, to be overwritten. end |
#next_state_action ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/socrates/core/state.rb', line 32 def next_state_action if @next_state_action.nil? next_action(@data.state_action) else @next_state_action end end |
#next_state_id ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/socrates/core/state.rb', line 24 def next_state_id if @next_state_id.nil? state_id_from_classname else @next_state_id end end |
#repeat_action ⇒ Object
79 80 81 82 |
# File 'lib/socrates/core/state.rb', line 79 def repeat_action @next_state_id = @data.state_id @next_state_action = @data.state_action end |
#respond(message: nil, template: nil, send_now: false) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/socrates/core/state.rb', line 40 def respond(message: nil, template: nil, send_now: false) if template # TODO: Partials? filename = File.join(Socrates.config.view_path, template) source = File.read(filename) = ERB.new(source, 0, "<>").result(binding) end return if .empty? @logger.info %Q(#{@session.channel} send: "#{format_for_logging()}") @adapter.(@session, , send_now: send_now) end |
#send_message(to:, message:) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/socrates/core/state.rb', line 54 def (to:, message:) displayable_to = to.respond_to?(:id) ? to.id : to @logger.info %Q(#{@session.channel} send direct to #{displayable_to}: "#{format_for_logging()}") @adapter.(@session, , to) end |
#transition_to(state_id, action: nil, data: {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/socrates/core/state.rb', line 61 def transition_to(state_id, action: nil, data: {}) if action.nil? action = if state_id.nil? nil elsif state_id == state_id_from_classname next_action(@data.state_action) else :ask end end @next_state_id = state_id @next_state_action = action @data.merge(data) end |