Class: Stealth::Controller
- Inherits:
-
Object
- Object
- Stealth::Controller
show all
- Includes:
- Callbacks, CatchAll, DynamicDelay, Helpers, Replies
- Defined in:
- lib/stealth/controller/helpers.rb,
lib/stealth/controller/replies.rb,
lib/stealth/controller/callbacks.rb,
lib/stealth/controller/catch_all.rb,
lib/stealth/controller/controller.rb,
lib/stealth/controller/dynamic_delay.rb
Defined Under Namespace
Modules: Callbacks, CatchAll, DynamicDelay, Helpers, Replies
Constant Summary
DynamicDelay::LONG_DELAY, DynamicDelay::SHORT_DELAY, DynamicDelay::STANDARD_DELAY
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#action(action: nil) ⇒ Object
-
#current_session ⇒ Object
-
#has_attachments? ⇒ Boolean
-
#has_location? ⇒ Boolean
-
#initialize(service_message:, current_flow: nil) ⇒ Controller
constructor
A new instance of Controller.
-
#previous_session ⇒ Object
-
#progressed? ⇒ Boolean
-
#route ⇒ Object
-
#step_to(session: nil, flow: nil, state: nil) ⇒ Object
-
#step_to_at(timestamp, session: nil, flow: nil, state: nil) ⇒ Object
-
#step_to_in(delay, session: nil, flow: nil, state: nil) ⇒ Object
-
#update_session_to(session: nil, flow: nil, state: nil) ⇒ Object
#calculate_delay_from_text
Constructor Details
#initialize(service_message:, current_flow: nil) ⇒ Controller
Returns a new instance of Controller.
17
18
19
20
21
22
23
|
# File 'lib/stealth/controller/controller.rb', line 17
def initialize(service_message:, current_flow: nil)
@current_message = service_message
@current_service = service_message.service
@current_user_id = @current_session_id = service_message.sender_id
@current_flow = current_flow
@progressed = false
end
|
Instance Attribute Details
#action_name ⇒ Object
Returns the value of attribute action_name.
13
14
15
|
# File 'lib/stealth/controller/controller.rb', line 13
def action_name
@action_name
end
|
#current_flow ⇒ Object
Returns the value of attribute current_flow.
13
14
15
|
# File 'lib/stealth/controller/controller.rb', line 13
def current_flow
@current_flow
end
|
#current_message ⇒ Object
Returns the value of attribute current_message.
13
14
15
|
# File 'lib/stealth/controller/controller.rb', line 13
def current_message
@current_message
end
|
#current_service ⇒ Object
Returns the value of attribute current_service.
13
14
15
|
# File 'lib/stealth/controller/controller.rb', line 13
def current_service
@current_service
end
|
#current_session_id ⇒ Object
Returns the value of attribute current_session_id.
13
14
15
|
# File 'lib/stealth/controller/controller.rb', line 13
def current_session_id
@current_session_id
end
|
#current_user_id ⇒ Object
Returns the value of attribute current_user_id.
13
14
15
|
# File 'lib/stealth/controller/controller.rb', line 13
def current_user_id
@current_user_id
end
|
#flow_controller ⇒ Object
Returns the value of attribute flow_controller.
13
14
15
|
# File 'lib/stealth/controller/controller.rb', line 13
def flow_controller
@flow_controller
end
|
Instance Method Details
#action(action: nil) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/stealth/controller/controller.rb', line 59
def action(action: nil)
@action_name = action
@action_name ||= current_session.state_string
if current_session.flow.current_state.redirects_to.present?
Stealth::Logger.l(
topic: "redirect",
message: "From #{current_session.session} to #{current_session.flow.current_state.redirects_to.session}"
)
step_to(session: current_session.flow.current_state.redirects_to)
return
end
run_callbacks :action do
begin
flow_controller.send(@action_name)
run_catch_all(reason: 'Did not send replies, update session, or step') unless flow_controller.progressed?
rescue StandardError => e
Stealth::Logger.l(topic: "catch_all", message: e.backtrace.join("\n"))
run_catch_all(reason: e.message)
end
end
end
|
#current_session ⇒ Object
51
52
53
|
# File 'lib/stealth/controller/controller.rb', line 51
def current_session
@current_session ||= Stealth::Session.new(user_id: current_session_id)
end
|
#has_attachments? ⇒ Boolean
29
30
31
|
# File 'lib/stealth/controller/controller.rb', line 29
def has_attachments?
current_message.attachments.present?
end
|
#has_location? ⇒ Boolean
25
26
27
|
# File 'lib/stealth/controller/controller.rb', line 25
def has_location?
current_message.location.present?
end
|
#previous_session ⇒ Object
55
56
57
|
# File 'lib/stealth/controller/controller.rb', line 55
def previous_session
@previous_session ||= Stealth::Session.new(user_id: current_session_id, previous: true)
end
|
#progressed? ⇒ Boolean
33
34
35
|
# File 'lib/stealth/controller/controller.rb', line 33
def progressed?
@progressed.present?
end
|
#step_to(session: nil, flow: nil, state: nil) ⇒ Object
106
107
108
109
|
# File 'lib/stealth/controller/controller.rb', line 106
def step_to(session: nil, flow: nil, state: nil)
flow, state = get_flow_and_state(session: session, flow: flow, state: state)
step(flow: flow, state: state)
end
|
#step_to_at(timestamp, session: nil, flow: nil, state: nil) ⇒ Object
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/stealth/controller/controller.rb', line 95
def step_to_at(timestamp, session: nil, flow: nil, state: nil)
flow, state = get_flow_and_state(session: session, flow: flow, state: state)
unless timestamp.is_a?(DateTime)
raise ArgumentError, "Please specify your step_to_at `timestamp` parameter as a DateTime"
end
Stealth::ScheduledReplyJob.perform_at(timestamp, current_service, current_session_id, flow, state)
Stealth::Logger.l(topic: "session", message: "User #{current_session_id}: scheduled session step to #{flow}->#{state} at #{timestamp.iso8601}")
end
|
#step_to_in(delay, session: nil, flow: nil, state: nil) ⇒ Object
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/stealth/controller/controller.rb', line 84
def step_to_in(delay, session: nil, flow: nil, state: nil)
flow, state = get_flow_and_state(session: session, flow: flow, state: state)
unless delay.is_a?(ActiveSupport::Duration)
raise ArgumentError, "Please specify your step_to_in `delay` parameter using ActiveSupport::Duration, e.g. `1.day` or `5.hours`"
end
Stealth::ScheduledReplyJob.perform_in(delay, current_service, current_session_id, flow, state)
Stealth::Logger.l(topic: "session", message: "User #{current_session_id}: scheduled session step to #{flow}->#{state} in #{delay} seconds")
end
|
#update_session_to(session: nil, flow: nil, state: nil) ⇒ Object
111
112
113
114
|
# File 'lib/stealth/controller/controller.rb', line 111
def update_session_to(session: nil, flow: nil, state: nil)
flow, state = get_flow_and_state(session: session, flow: flow, state: state)
update_session(flow: flow, state: state)
end
|