Module: Socrates::Core::State

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



12
13
14
# File 'lib/socrates/core/state.rb', line 12

def adapter
  @adapter
end

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/socrates/core/state.rb', line 12

def data
  @data
end

#sessionObject (readonly)

Returns the value of attribute session.



12
13
14
# File 'lib/socrates/core/state.rb', line 12

def session
  @session
end

#userObject (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

#askObject



90
91
92
# File 'lib/socrates/core/state.rb', line 90

def ask
  # stub implementation, to be overwritten.
end

#end_conversationObject



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(_message)
  # stub implementation, to be overwritten.
end

#next_state_actionObject



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_idObject



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_actionObject



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)
    message  = ERB.new(source, 0, "<>").result(binding)
  end

  return if message.empty?

  @logger.info %Q(#{@session.channel} send: "#{format_for_logging(message)}")
  @adapter.queue_message(@session, message, 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 send_message(to:, message:)
  displayable_to = to.respond_to?(:id) ? to.id : to

  @logger.info %Q(#{@session.channel} send direct to #{displayable_to}: "#{format_for_logging(message)}")
  @adapter.queue_direct_message(@session, message, 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