Class: Dialogue::Conversation

Inherits:
Object
  • Object
show all
Includes:
ConversationOptions, Storable
Defined in:
lib/dialogue/conversation.rb

Instance Attribute Summary collapse

Attributes included from ConversationOptions

#options

Instance Method Summary collapse

Methods included from Storable

#data, #fetch, #has_data?, #store!

Methods included from ConversationOptions

#method_missing

Constructor Details

#initialize(template = nil, message = nil, options = {}) ⇒ Conversation

Returns a new instance of Conversation.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dialogue::ConversationOptions

Instance Attribute Details

#channel_idObject

Returns the value of attribute channel_id.



8
9
10
# File 'lib/dialogue/conversation.rb', line 8

def channel_id
  @channel_id
end

#messageObject (readonly)

Returns the value of attribute message.



9
10
11
# File 'lib/dialogue/conversation.rb', line 9

def message
  @message
end

#stepsObject (readonly)

Returns the value of attribute steps.



9
10
11
# File 'lib/dialogue/conversation.rb', line 9

def steps
  @steps
end

#team_idObject

Returns the value of attribute team_id.



8
9
10
# File 'lib/dialogue/conversation.rb', line 8

def team_id
  @team_id
end

#templatesObject (readonly)

Returns the value of attribute templates.



9
10
11
# File 'lib/dialogue/conversation.rb', line 9

def templates
  @templates
end

#user_idObject

Returns the value of attribute user_id.



8
9
10
# File 'lib/dialogue/conversation.rb', line 8

def user_id
  @user_id
end

Instance Method Details

#ask(question, opts = {}, &block) ⇒ Object



33
34
35
36
# File 'lib/dialogue/conversation.rb', line 33

def ask(question, opts={}, &block)
  say question, opts
  steps << block
end

#diverge(template_name) ⇒ Object



38
39
40
41
42
43
44
45
# 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) ⇒ Object



47
48
49
50
# File 'lib/dialogue/conversation.rb', line 47

def end(statement=nil)
  say statement unless statement.nil?
  Dialogue.unregister_conversation self
end

#perform(*args) ⇒ Object Also known as: continue



52
53
54
55
56
# 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

#say(statement, opts = {}) ⇒ Object Also known as: reply



59
60
61
62
63
# 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