Class: FlowChat::BaseApp

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

Direct Known Subclasses

Http::App, Ussd::App, Whatsapp::App

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ BaseApp

Returns a new instance of BaseApp.



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

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#inputObject (readonly)

Returns the value of attribute input.



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

def input
  @input
end

Returns the value of attribute navigation_stack.



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

def navigation_stack
  @navigation_stack
end

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

Instance Method Details

#contact_nameObject



67
68
69
# File 'lib/flow_chat/base_app.rb', line 67

def contact_name
  nil
end

#gatewayObject



47
48
49
# File 'lib/flow_chat/base_app.rb', line 47

def gateway
  context["request.gateway"]
end

#go_backObject



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

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



71
72
73
# File 'lib/flow_chat/base_app.rb', line 71

def location
  nil
end

#mediaObject



75
76
77
# File 'lib/flow_chat/base_app.rb', line 75

def media
  nil
end

#message_idObject



59
60
61
# File 'lib/flow_chat/base_app.rb', line 59

def message_id
  context["request.message_id"]
end

#msisdnObject



55
56
57
# File 'lib/flow_chat/base_app.rb', line 55

def msisdn
  context["request.msisdn"]
end

#platformObject



43
44
45
# File 'lib/flow_chat/base_app.rb', line 43

def platform
  context["request.platform"]
end

#say(msg, media: nil) ⇒ Object



39
40
41
# File 'lib/flow_chat/base_app.rb', line 39

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

#screen(key) ⇒ Object

Raises:

  • (ArgumentError)


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

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

#timestampObject



63
64
65
# File 'lib/flow_chat/base_app.rb', line 63

def timestamp
  context["request.timestamp"]
end

#user_idObject



51
52
53
# File 'lib/flow_chat/base_app.rb', line 51

def user_id
  context["request.user_id"]
end