Class: Stealth::Controller

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/stealth/controller.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_message = service_message
  @current_service = service_message.service
  @current_user_id = service_message.sender_id
  @current_flow = current_flow
end

Instance Attribute Details

#current_flowObject (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_messageObject (readonly)

Returns the value of attribute current_message.



11
12
13
# File 'lib/stealth/controller.rb', line 11

def current_message
  @current_message
end

#current_serviceObject (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_idObject (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_controllerObject (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_sessionObject



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

Returns:

  • (Boolean)


25
26
27
# File 'lib/stealth/controller.rb', line 25

def has_attachments?
  current_message.attachments.present?
end

#has_location?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/stealth/controller.rb', line 21

def has_location?
  current_message.location.present?
end

#routeObject



29
30
31
# File 'lib/stealth/controller.rb', line 29

def route
  raise(Stealth::Errors::ControllerRoutingNotImplemented, "Please implement `route` method in BotController.")
end

#send_repliesObject



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_nextObject



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_nextObject



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