Class: Stealth::Controller
- Inherits:
-
Object
- Object
- Stealth::Controller
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- lib/stealth/controller.rb
Instance Attribute Summary collapse
-
#current_flow ⇒ Object
readonly
Returns the value of attribute current_flow.
-
#current_message ⇒ Object
readonly
Returns the value of attribute current_message.
-
#current_service ⇒ Object
readonly
Returns the value of attribute current_service.
-
#current_user_id ⇒ Object
readonly
Returns the value of attribute current_user_id.
-
#flow_controller ⇒ Object
readonly
Returns the value of attribute flow_controller.
Class Method Summary collapse
- .after_action(*args, &block) ⇒ Object
- .around_action(*args, &block) ⇒ Object
- .before_action(*args, &block) ⇒ Object
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.
- #route ⇒ Object
- #send_replies ⇒ Object
- #step_to(session: nil, flow: nil, state: nil) ⇒ Object
- #step_to_in(delay, session: nil, flow: nil, state: nil) ⇒ Object
- #step_to_next ⇒ Object
- #update_session_to(session: nil, flow: nil, state: nil) ⇒ Object
- #update_session_to_next ⇒ Object
Constructor Details
#initialize(service_message:, current_flow: nil) ⇒ Controller
Returns a new instance of Controller.
14 15 16 17 18 19 |
# File 'lib/stealth/controller.rb', line 14 def initialize(service_message:, current_flow: nil) = @current_service = .service @current_user_id = .sender_id @current_flow = current_flow end |
Instance Attribute Details
#current_flow ⇒ Object (readonly)
Returns the value of attribute current_flow.
11 12 13 |
# File 'lib/stealth/controller.rb', line 11 def current_flow @current_flow end |
#current_message ⇒ Object (readonly)
Returns the value of attribute current_message.
11 12 13 |
# File 'lib/stealth/controller.rb', line 11 def end |
#current_service ⇒ Object (readonly)
Returns the value of attribute current_service.
11 12 13 |
# File 'lib/stealth/controller.rb', line 11 def current_service @current_service end |
#current_user_id ⇒ Object (readonly)
Returns the value of attribute current_user_id.
11 12 13 |
# File 'lib/stealth/controller.rb', line 11 def current_user_id @current_user_id end |
#flow_controller ⇒ Object (readonly)
Returns the value of attribute flow_controller.
11 12 13 |
# File 'lib/stealth/controller.rb', line 11 def flow_controller @flow_controller end |
Class Method Details
.after_action(*args, &block) ⇒ Object
121 122 123 |
# File 'lib/stealth/controller.rb', line 121 def self.after_action(*args, &block) set_callback(:action, :after, *args, &block) end |
.around_action(*args, &block) ⇒ Object
117 118 119 |
# File 'lib/stealth/controller.rb', line 117 def self.around_action(*args, &block) set_callback(:action, :around, *args, &block) end |
.before_action(*args, &block) ⇒ Object
113 114 115 |
# File 'lib/stealth/controller.rb', line 113 def self.before_action(*args, &block) set_callback(:action, :before, *args, &block) end |
Instance Method Details
#action(action: nil) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/stealth/controller.rb', line 76 def action(action: nil) run_callbacks :action do action ||= current_session.state_string flow_controller.send(action) end end |
#current_session ⇒ Object
72 73 74 |
# File 'lib/stealth/controller.rb', line 72 def current_session @current_session ||= Stealth::Session.new(user_id: current_user_id) end |
#has_attachments? ⇒ Boolean
25 26 27 |
# File 'lib/stealth/controller.rb', line 25 def ..present? end |
#has_location? ⇒ Boolean
21 22 23 |
# File 'lib/stealth/controller.rb', line 21 def has_location? .location.present? end |
#route ⇒ Object
29 30 31 |
# File 'lib/stealth/controller.rb', line 29 def route raise(Stealth::Errors::ControllerRoutingNotImplemented, "Please implement `route` method in BotController.") end |
#send_replies ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/stealth/controller.rb', line 33 def send_replies service_reply = Stealth::ServiceReply.new( recipient_id: current_user_id, yaml_reply: action_replies, context: binding ) for reply in service_reply.replies do handler = reply_handler.new( recipient_id: current_user_id, reply: reply ) translated_reply = handler.send(reply.reply_type) client = service_client.new(reply: translated_reply) client.transmit # If this was a 'delay' type of reply, let's respect the delay if reply.reply_type == 'delay' begin sleep_duration = Float(reply["duration"]) sleep(sleep_duration) rescue ArgumentError, TypeError raise(ArgumentError, 'Invalid duration specified. Duration must be a float.') end end end end |
#step_to(session: nil, flow: nil, state: nil) ⇒ Object
93 94 95 96 |
# File 'lib/stealth/controller.rb', line 93 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_in(delay, session: nil, flow: nil, state: nil) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/stealth/controller.rb', line 83 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 schedule_step_to `in` parameter using ActiveSupport::Duration, e.g. `1.day` or `5.hours`" end Stealth::ScheduledReplyJob.perform_in(delay, current_service, current_user_id, flow, state) end |
#step_to_next ⇒ Object
103 104 105 106 |
# File 'lib/stealth/controller.rb', line 103 def step_to_next flow, state = get_next_state step(flow: flow, state: state) end |
#update_session_to(session: nil, flow: nil, state: nil) ⇒ Object
98 99 100 101 |
# File 'lib/stealth/controller.rb', line 98 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 |
#update_session_to_next ⇒ Object
108 109 110 111 |
# File 'lib/stealth/controller.rb', line 108 def update_session_to_next flow, state = get_next_state update_session(flow: flow, state: state) end |