class Dialogue::Conversation

Attributes

channel_id[RW]
message[R]
steps[R]
team_id[RW]
templates[R]
user_id[RW]

Public Class Methods

new(template=nil, message=nil, options={}) click to toggle source
# File lib/dialogue/conversation.rb, line 11
def initialize(template=nil, message=nil, options={})
  guard_options! options

  @templates = []
  @steps = []

  unless template.nil?
    @templates = [template]
    @steps << template.template
  end

  @message = message
  unless @message.nil?
    @channel_id = message.channel_id
    @team_id = message.team_id
    @user_id = message.user_id
  end

  @data = options.delete(:data)
  @options = options.freeze
end

Public Instance Methods

ask(question, opts={}, &block) click to toggle source
# File lib/dialogue/conversation.rb, line 33
def ask(question, opts={}, &block)
  say question, opts
  steps << block
end
continue(*args)
Alias for: perform
diverge(template_name) click to toggle source
# File lib/dialogue/conversation.rb, line 38
def diverge(template_name)
  template = Dialogue.find_template template_name
  unless template.nil?
    templates << template
    steps << template.template
    perform
  end
end
end(statement=nil) click to toggle source
# File lib/dialogue/conversation.rb, line 47
def end(statement=nil)
  say statement unless statement.nil?
  Dialogue.unregister_conversation self
end
perform(*args) click to toggle source
# File lib/dialogue/conversation.rb, line 52
def perform(*args)
  step = steps.pop
  step.call self, *args unless step.nil?
  Dialogue.unregister_conversation self if steps.empty?
end
Also aliased as: continue
reply(statement, opts={})
Alias for: say
say(statement, opts={}) click to toggle source
# File lib/dialogue/conversation.rb, line 59
def say(statement, opts={})
  ensure_registration
  Dialogue::Streams::Slack.new(options[:access_token])
    .puts statement, channel_id, user_id, opts
end
Also aliased as: reply

Private Instance Methods

ensure_registration() click to toggle source
# File lib/dialogue/conversation.rb, line 68
def ensure_registration
  # The conversation can be unregistered from a diverge, so let's ensure it
  # is registered
  if !Dialogue.conversation_registered? user_id, channel_id
    Dialogue.register_conversation self
  end
end