Class: FlowChat::App

Inherits:
Object
  • Object
show all
Defined in:
lib/flow_chat/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ App

Returns a new instance of App.



5
6
7
8
9
# File 'lib/flow_chat/app.rb', line 5

def initialize(context)
  @context = context
  @input = context.input
  @navigation_stack = []
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



3
4
5
# File 'lib/flow_chat/app.rb', line 3

def context
  @context
end

#inputObject (readonly)

Returns the value of attribute input.



3
4
5
# File 'lib/flow_chat/app.rb', line 3

def input
  @input
end

Returns the value of attribute navigation_stack.



3
4
5
# File 'lib/flow_chat/app.rb', line 3

def navigation_stack
  @navigation_stack
end

Instance Method Details

#contact_nameObject



66
67
68
# File 'lib/flow_chat/app.rb', line 66

def contact_name
  nil
end

#gatewayObject



46
47
48
# File 'lib/flow_chat/app.rb', line 46

def gateway
  context["request.gateway"]
end

#go_backObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/flow_chat/app.rb', line 27

def go_back
  return false if navigation_stack.empty?

  @context.input = nil
  current_screen = navigation_stack.last
  session.delete(current_screen)

  # Restart the flow from the beginning
  raise FlowChat::Interrupt::RestartFlow.new
end

#locationObject



70
71
72
# File 'lib/flow_chat/app.rb', line 70

def location
  nil
end

#mediaObject



74
75
76
# File 'lib/flow_chat/app.rb', line 74

def media
  nil
end

#message_idObject



58
59
60
# File 'lib/flow_chat/app.rb', line 58

def message_id
  context["request.message_id"]
end

#msisdnObject



54
55
56
# File 'lib/flow_chat/app.rb', line 54

def msisdn
  context["request.msisdn"]
end

#platformObject



42
43
44
# File 'lib/flow_chat/app.rb', line 42

def platform
  context["request.platform"]
end

#say(msg, media: nil) ⇒ Object



38
39
40
# File 'lib/flow_chat/app.rb', line 38

def say(msg, media: nil)
  raise FlowChat::Interrupt::Terminate.new(msg, media: media)
end

#screen(key) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/flow_chat/app.rb', line 11

def screen(key)
  raise ArgumentError, "a block is expected" unless block_given?
  raise ArgumentError, "screen has already been presented" if navigation_stack.include?(key)

  navigation_stack << key
  return session.get(key) if session.get(key).present?

  user_input = prepare_user_input
  prompt = FlowChat::Prompt.new user_input
  @input = nil # input is being submitted to prompt so we clear it

  value = yield prompt
  session.set(key, value)
  value
end

#sessionObject



78
79
80
# File 'lib/flow_chat/app.rb', line 78

def session
  @context.session
end

#timestampObject



62
63
64
# File 'lib/flow_chat/app.rb', line 62

def timestamp
  context["request.timestamp"]
end

#user_idObject



50
51
52
# File 'lib/flow_chat/app.rb', line 50

def user_id
  context["request.user_id"]
end