kybus-bot 0.11.1 → 0.11.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6027eb3dbf33a839ee3b209eda5c8175a75372a3b03021cf171d07794f9ba1b2
4
- data.tar.gz: 1271955a903acbbebecec80eca93302f144bd97ec70b93ad6c2d6171381942a7
3
+ metadata.gz: 5621cdb73308df7a7580e484959a825170aa1e9028c3ad55950c86a4ca6f5a2c
4
+ data.tar.gz: caec550063dfc742d65687f8c8dda7de87cb91acb4409c9b81ac823e52843f14
5
5
  SHA512:
6
- metadata.gz: bee0655c5cc486692a84b0e63cace81b3c0028cd10c88699d486d0e05714acb866f9afc68708b66e1bc3a5c5d94aebc43a04ceb875c99331d7d600faea02d39c
7
- data.tar.gz: db7c59187ff02ca0931382d4ad49a9483047dff4ac6c55217aa68ffbcc6c6a9caa5ef4d5416b773e0a51c280e019d6865f359cb70f5689b9b127c6401190e0e7
6
+ metadata.gz: 98558bf269ec8e876e76eeca9c0ecbc308424db2dd14697f1ef8813e42e917fb1603875a6c3cf541ba5707ec0c2c3e6d86fbd972c13c95e8a0b8f39d5e664a65
7
+ data.tar.gz: bf47207fae0a45f1608470f480175a9ae4084a96080197bece81c2f793d87c4a4fc5b41bd743c838a6359e64fc743ef94d2014b81b9d4c766e0c29ecc7d73451
@@ -11,6 +11,7 @@ module Kybus
11
11
  # Wraps a debugging message inside a class.
12
12
  class DebugMessage < Kybus::Bot::Message
13
13
  # It receives a string with the raw text and the id of the channel
14
+ attr_accessor :replied_message
14
15
  attr_reader :attachment, :message_id
15
16
 
16
17
  class DebugFile
@@ -67,7 +68,7 @@ module Kybus
67
68
  end
68
69
 
69
70
  def reply?
70
- @reply
71
+ !!@replied_message
71
72
  end
72
73
 
73
74
  def is_private?
@@ -91,6 +92,8 @@ module Kybus
91
92
  @echo = echo
92
93
  end
93
94
 
95
+ attr_reader :last_sent_message
96
+
94
97
  # Checks if there are messages open or that has not been answered
95
98
  def open?
96
99
  @state == :open
@@ -120,7 +123,7 @@ module Kybus
120
123
  def answer(message, attachment = nil)
121
124
  send_data(message, attachment)
122
125
  @state = :open
123
- DebugMessage.new(message, @name)
126
+ @last_sent_message = DebugMessage.new(message, @name)
124
127
  end
125
128
  end
126
129
 
@@ -199,10 +202,7 @@ module Kybus
199
202
  end
200
203
  end
201
204
 
202
- include Kybus::Logger
203
-
204
205
  def message_builder(msg)
205
- log_info('Building message object', msg:, msg_class: msg.class.name)
206
206
  msg
207
207
  end
208
208
 
@@ -44,7 +44,7 @@ module Kybus
44
44
  @definitions = Kybus::Bot::CommandDefinition.new
45
45
  repository = create_repository(configs)
46
46
  command_factory = CommandStateFactory.new(repository, @definitions)
47
- @executor = create_executor(configs, command_factory)
47
+ create_executor(configs, command_factory)
48
48
  register_default_command
49
49
  register_abort_handler
50
50
  build_forker(configs)
@@ -70,8 +70,9 @@ module Kybus
70
70
  end
71
71
 
72
72
  def handle_job(job, args, channel_id)
73
- @executor.execution_context.load_state!(channel_id)
73
+ @executor.load_state!(channel_id)
74
74
  @forker.handle_job(job, args)
75
+ @executor.save_state!
75
76
  end
76
77
 
77
78
  def run
@@ -92,7 +93,7 @@ module Kybus
92
93
  definitions.register_command(klass, params, &)
93
94
  end
94
95
 
95
- def register_job(name, args, &)
96
+ def register_job(name, args = {}, &)
96
97
  @forker.register_command(name, args, &)
97
98
  end
98
99
 
@@ -100,6 +101,10 @@ module Kybus
100
101
  @forker.fork(name, args, dsl)
101
102
  end
102
103
 
104
+ def invoke_job_with_delay(name, delay, args)
105
+ @forker.fork(name, args, dsl, delay:)
106
+ end
107
+
103
108
  def rescue_from(klass, &)
104
109
  definitions.register_command(klass, [], &)
105
110
  end
@@ -119,12 +124,12 @@ module Kybus
119
124
  end
120
125
 
121
126
  def create_executor(configs, command_factory)
122
- if configs['sidekiq']
123
- require_relative 'sidekiq_command_executor'
124
- Kybus::Bot::SidekiqCommandExecutor.new(self, command_factory, configs)
125
- else
126
- Kybus::Bot::CommandExecutor.new(self, command_factory, configs['inline_args'])
127
- end
127
+ @executor = if configs['sidekiq']
128
+ require_relative 'sidekiq_command_executor'
129
+ Kybus::Bot::SidekiqCommandExecutor.new(self, command_factory, configs)
130
+ else
131
+ Kybus::Bot::CommandExecutor.new(self, command_factory, configs['inline_args'])
132
+ end
128
133
  end
129
134
 
130
135
  def register_default_command
@@ -20,7 +20,7 @@ module Kybus
20
20
  end
21
21
 
22
22
  def to_h
23
- { command: command.name, data: @data.to_h.merge(last_message: @data[:last_message]&.to_h) }
23
+ { command: command&.name, data: @data.to_h.merge(last_message: @data[:last_message]&.to_h) }
24
24
  end
25
25
 
26
26
  def clear_command
@@ -40,6 +40,10 @@ module Kybus
40
40
  end
41
41
 
42
42
  def last_message
43
+ if @data[:last_message].is_a?(String)
44
+ @data[:last_message] =
45
+ SerializedMessage.from_json(parse_json(@data[:last_message]))
46
+ end
43
47
  @data[:last_message]
44
48
  end
45
49
 
@@ -80,9 +84,11 @@ module Kybus
80
84
  end
81
85
 
82
86
  def metadata
87
+ @data[:metadata] = parse_json(@data[:metadata]) if @data[:metadata].is_a?(String)
83
88
  @data[:metadata] || {}
84
89
  end
85
90
 
91
+ include Kybus::Logger
86
92
  def save!
87
93
  backup = @data.clone
88
94
  serialize_data!
@@ -107,7 +113,7 @@ module Kybus
107
113
 
108
114
  def serialize_data!
109
115
  %i[params files last_message metadata].each do |param|
110
- @data[param] = @data[param].to_json
116
+ @data[param] = @data[param].to_json unless @data[param].is_a?(String)
111
117
  end
112
118
  end
113
119
  end
@@ -45,7 +45,7 @@ module Kybus
45
45
  end
46
46
 
47
47
  def process_message(message)
48
- setup_execution_context(message)
48
+ load_state!(message.channel_id)
49
49
  @parameter_saver.save_token!(message)
50
50
  msg = @command_handler.run_command_or_prepare!
51
51
  save_execution_context!
@@ -84,12 +84,17 @@ module Kybus
84
84
  execution_context.next_param = param
85
85
  end
86
86
 
87
- private
87
+ def load_state!(channel_id)
88
+ @execution_context = ExecutionContest.new(channel_id, @channel_factory)
89
+ @dsl.state = @execution_context.state
90
+ end
88
91
 
89
- def setup_execution_context(message)
90
- @execution_context = ExecutionContest.new(message.channel_id, @channel_factory)
92
+ def save_state!
93
+ @dsl.state.save!
91
94
  end
92
95
 
96
+ private
97
+
93
98
  def set_state_command(command, args)
94
99
  state.command = command
95
100
  command.params.zip(args).each do |param, value|
@@ -77,6 +77,10 @@ module Kybus
77
77
  state&.command&.name
78
78
  end
79
79
 
80
+ def save_metadata!
81
+ state.save!
82
+ end
83
+
80
84
  def redirect(*)
81
85
  @bot.redirect(*)
82
86
  end
@@ -88,6 +92,10 @@ module Kybus
88
92
  def fork(command, arguments = {})
89
93
  @bot.invoke_job(command, arguments)
90
94
  end
95
+
96
+ def fork_with_delay(command, delay, arguments = {})
97
+ @bot.invoke_job_with_delay(command, delay, arguments)
98
+ end
91
99
  end
92
100
  end
93
101
  end
@@ -38,13 +38,13 @@ module Kybus
38
38
  @command_definition.register_command(command, arguments, &)
39
39
  end
40
40
 
41
- def fork(command, arguments, dsl)
41
+ def fork(command, arguments, dsl, delay: 0)
42
42
  job_definition = @command_definition[command]
43
43
  raise JobNotFound if job_definition.nil?
44
44
 
45
45
  raise JobNotReady unless job_definition.ready?(arguments)
46
46
 
47
- invoke(command, arguments, job_definition, dsl)
47
+ invoke(command, arguments, job_definition, dsl, delay:)
48
48
  end
49
49
 
50
50
  def handle_job(command, args)
@@ -53,7 +53,6 @@ module Kybus
53
53
  @args = args
54
54
  instance_eval(&job_definition.block)
55
55
  end
56
- log_info('Job Executed', command:, args:)
57
56
  end
58
57
  end
59
58
  end
@@ -17,8 +17,9 @@ module Kybus
17
17
  @queue_url = @client.get_queue_url(queue_name: @queue).queue_url
18
18
  end
19
19
 
20
- def invoke(command, args, _job_definition, dsl)
21
- @client.send_message(queue_url: @queue_url, message_body: make_message(command, args, dsl).to_json)
20
+ def invoke(command, args, _job_definition, dsl, delay: 0)
21
+ @client.send_message(queue_url: @queue_url,
22
+ message_body: make_message(command, args, dsl).to_json, delay_seconds: delay)
22
23
  end
23
24
 
24
25
  def make_message(command, args, dsl)
@@ -4,10 +4,10 @@ module Kybus
4
4
  module Bot
5
5
  module Forkers
6
6
  class ThreadForker < Base
7
- def invoke(command, args, _job_definition, _dsl)
7
+ def invoke(command, args, _job_definition, dsl, delay: 0)
8
8
  Thread.new do
9
- log_info('Forking job', command:)
10
- handle_job(command, args)
9
+ sleep(delay) if delay.positive?
10
+ @bot.handle_job(command, args, dsl.state.channel_id)
11
11
  end
12
12
  end
13
13
  end
@@ -5,6 +5,17 @@ require_relative 'adapters/debug'
5
5
 
6
6
  module Kybus
7
7
  module Bot
8
+ module Forkers
9
+ class NoForker < Base
10
+ def invoke(command, args, _job_definition, dsl, delay: 0)
11
+ sleep(delay) if delay.positive?
12
+ @bot.handle_job(command, args, dsl.state.channel_id)
13
+ end
14
+ end
15
+
16
+ register_forker('nofork', NoForker)
17
+ end
18
+
8
19
  class Base
9
20
  CONFIG = {
10
21
  'name' => 'test',
@@ -17,6 +28,9 @@ module Kybus
17
28
  'name' => 'debug',
18
29
  'echo' => false,
19
30
  'channels' => { 'testing' => [] }
31
+ },
32
+ 'forker' => {
33
+ 'provider' => 'nofork'
20
34
  }
21
35
  }.freeze
22
36
 
@@ -31,7 +45,6 @@ module Kybus
31
45
  def receives(msg, attachments = nil)
32
46
  attachments = Adapter::DebugMessage::DebugFile.new(attachments) if attachments
33
47
  msg = Adapter::DebugMessage.new(msg, @default_channel_id, attachments)
34
- log_info('Received message', channel: @default_channel_id, msg: msg.raw_message)
35
48
  provider.last_message = msg
36
49
  executor.process_message(msg)
37
50
  end
@@ -39,6 +52,17 @@ module Kybus
39
52
  def expects(method)
40
53
  executor.dsl.expects(method)
41
54
  end
55
+
56
+ def replies(msg, attachments = nil)
57
+ reply = @provider.channel(current_channel).last_sent_message
58
+ raise 'NoPreviousMessageToReply' if reply.nil?
59
+
60
+ attachments = Adapter::DebugMessage::DebugFile.new(attachments) if attachments
61
+ msg = Adapter::DebugMessage.new(msg, @default_channel_id, attachments)
62
+ msg.replied_message = reply
63
+ provider.last_message = msg
64
+ executor.process_message(msg)
65
+ end
42
66
  end
43
67
  end
44
68
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Kybus
4
4
  module Bot
5
- VERSION = '0.11.1'
5
+ VERSION = '0.11.3'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kybus-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.11.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilberto Vargas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-09 00:00:00.000000000 Z
11
+ date: 2024-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kybus-core