mobb 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/slack_bot.rb +8 -7
- data/lib/mobb/base.rb +63 -22
- data/lib/mobb/version.rb +1 -1
- data/mobb.gemspec +3 -1
- metadata +32 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37a6be9b2a1986d9bee5a4d1b65db94b9439b43851f9a251573ea979744f28a3
|
4
|
+
data.tar.gz: 4473258177b3a6c6dc409300930281507e44505705a1751981c23aad1882d504
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d39ac2495a95a3aec44d99e6fed4362e177c135f54bf1b0b0ae8d46e164d56bad8bcd919fcd422fe13309f55da71d70b9a417a90ef5e235147e72f6b4ce06ee6
|
7
|
+
data.tar.gz: d0956d5143950ef3ef82e6ccc63c4347baf37b8431b221157b7b924cda36857f2871418b14e6826ced801b59aac9cec2aef341945be22d877a83e20709626783
|
data/examples/slack_bot.rb
CHANGED
@@ -9,15 +9,16 @@ end
|
|
9
9
|
|
10
10
|
# Warning this is bad implementation
|
11
11
|
# inifinity loop happend
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
# TODO: now broken
|
13
|
+
#receive "Yo", laziness: true do
|
14
|
+
# "Yo"
|
15
|
+
#end
|
15
16
|
|
16
17
|
receive /hey (\w+)/ do |someone|
|
17
18
|
"hey #{someone}, waz up?"
|
18
19
|
end
|
19
20
|
|
20
|
-
#
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
# Need dest_to condition to cron/every task
|
22
|
+
every :day, at: '12:30', dest_to: 'times_kinoppyd' do
|
23
|
+
'Stund up daily meeting time!'
|
24
|
+
end
|
data/lib/mobb/base.rb
CHANGED
@@ -1,20 +1,24 @@
|
|
1
1
|
require 'repp'
|
2
|
+
require 'whenever'
|
3
|
+
require 'parse-cron'
|
4
|
+
|
2
5
|
require "mobb/version"
|
3
6
|
|
4
7
|
module Mobb
|
5
8
|
class Matcher
|
6
|
-
def initialize(pattern, options) @pattern, @options = pattern, options; end
|
9
|
+
def initialize(pattern, options = {}) @pattern, @options = pattern, options; end
|
7
10
|
def regexp?; pattern.is_a?(Regexp); end
|
8
11
|
def inspect; "pattern: #{@pattern}, options #{@options}"; end
|
9
|
-
def
|
12
|
+
def cron?; pattern.is_a?(CronParser); end
|
13
|
+
def tick(time = Time.now) @options[:last_tick] = time; end
|
14
|
+
def last_tick; @options[:last_tick] end
|
10
15
|
|
11
16
|
def match?(context)
|
12
17
|
case context
|
13
18
|
when String
|
14
19
|
string_matcher(context)
|
15
20
|
when Time
|
16
|
-
|
17
|
-
false
|
21
|
+
cron_matcher(context)
|
18
22
|
when Array
|
19
23
|
context.all? { |c| match?(c) }
|
20
24
|
else
|
@@ -43,6 +47,13 @@ module Mobb
|
|
43
47
|
false
|
44
48
|
end
|
45
49
|
end
|
50
|
+
|
51
|
+
def cron_matcher(time)
|
52
|
+
last = last_tick
|
53
|
+
tick(time) if !last || time > last
|
54
|
+
return false if time < last
|
55
|
+
pattern.next(time) == pattern.next(last) ? false : true
|
56
|
+
end
|
46
57
|
end
|
47
58
|
|
48
59
|
class Base
|
@@ -50,20 +61,12 @@ module Mobb
|
|
50
61
|
dup.call!(env)
|
51
62
|
end
|
52
63
|
|
53
|
-
def tick(env)
|
54
|
-
dup.tick!(env)
|
55
|
-
end
|
56
|
-
|
57
64
|
def call!(env)
|
58
65
|
@env = env
|
59
66
|
invoke { dispatch! }
|
60
67
|
[@body, @attachments]
|
61
68
|
end
|
62
69
|
|
63
|
-
def tick!(env)
|
64
|
-
fail # TODO: write logic here
|
65
|
-
end
|
66
|
-
|
67
70
|
def dispatch!
|
68
71
|
# TODO: encode input messages
|
69
72
|
|
@@ -92,9 +95,12 @@ module Mobb
|
|
92
95
|
|
93
96
|
def handle_event(base = settings, passed_block = nil)
|
94
97
|
if responds = base.events[@env.event_type]
|
95
|
-
responds.each do |pattern, block,
|
96
|
-
process_event(pattern,
|
97
|
-
event_eval
|
98
|
+
responds.each do |pattern, block, before_conditions, after_conditions|
|
99
|
+
process_event(pattern, before_conditions) do |*args|
|
100
|
+
event_eval do
|
101
|
+
res = block[*args]
|
102
|
+
after_conditions.inject(res) { |acc, c| c.bind(self).call(acc) }
|
103
|
+
end
|
98
104
|
end
|
99
105
|
end
|
100
106
|
end
|
@@ -140,7 +146,8 @@ module Mobb
|
|
140
146
|
|
141
147
|
def reset!
|
142
148
|
@events = {}
|
143
|
-
@
|
149
|
+
@before_conditions = []
|
150
|
+
@after_conditions = []
|
144
151
|
end
|
145
152
|
|
146
153
|
def settings
|
@@ -150,27 +157,44 @@ module Mobb
|
|
150
157
|
def receive(pattern, options = {}, &block) event(:message, pattern, options, &block); end
|
151
158
|
alias :on :receive
|
152
159
|
|
153
|
-
|
160
|
+
def every(pattern, options = {}, &block) event(:ticker, pattern, options, &block); end
|
161
|
+
|
162
|
+
def cron(pattern, options = {}, &block) event(:ticker, pattern, options, &block); end
|
154
163
|
|
155
164
|
def event(type, pattern, options, &block)
|
156
165
|
(@events[type] ||= []) << compile!(type, pattern, options, &block)
|
157
166
|
end
|
158
167
|
|
159
168
|
def compile!(type, pattern, options, &block)
|
169
|
+
at = options.delete(:at)
|
160
170
|
options.each_pair { |option, args| send(option, *args) }
|
161
171
|
|
162
|
-
matcher =
|
172
|
+
matcher = case type
|
173
|
+
when :message
|
174
|
+
compile(pattern, options)
|
175
|
+
when :ticker
|
176
|
+
compile_cron(pattern, at)
|
177
|
+
end
|
163
178
|
unbound_method = generate_method("#{type}", &block)
|
164
|
-
|
179
|
+
before_conditions, @before_conditions = @before_conditions, []
|
180
|
+
after_conditions, @after_conditions = @after_conditions, []
|
165
181
|
wrapper = block.arity != 0 ?
|
166
182
|
proc { |instance, args| unbound_method.bind(instance).call(*args) } :
|
167
183
|
proc { |instance, args| unbound_method.bind(instance).call }
|
168
184
|
|
169
|
-
[ matcher, wrapper,
|
185
|
+
[ matcher, wrapper, before_conditions, after_conditions ]
|
170
186
|
end
|
171
187
|
|
172
188
|
def compile(pattern, options) Matcher.new(pattern, options); end
|
173
189
|
|
190
|
+
def compile_cron(time, at)
|
191
|
+
if String === time
|
192
|
+
Matcher.new(CronParser.new(time))
|
193
|
+
else
|
194
|
+
Matcher.new(CronParser.new(Whenever::Output::Cron.new(time, nil, at).time_in_cron_syntax))
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
174
198
|
def generate_method(name, &block)
|
175
199
|
define_method(name, &block)
|
176
200
|
method = instance_method(name)
|
@@ -224,7 +248,18 @@ module Mobb
|
|
224
248
|
end
|
225
249
|
|
226
250
|
def condition(name = "#{caller.first[/`.*'/]} condition", &block)
|
227
|
-
@
|
251
|
+
@before_conditions << generate_method(name, &block)
|
252
|
+
end
|
253
|
+
alias :before_condition :condition
|
254
|
+
|
255
|
+
def after_condition(name = "#{caller.first[/`.*'/]} condition", &block)
|
256
|
+
@after_conditions << generate_method(name) do |res|
|
257
|
+
if String === res
|
258
|
+
res = [res, {}]
|
259
|
+
end
|
260
|
+
block.call(res)
|
261
|
+
res
|
262
|
+
end
|
228
263
|
end
|
229
264
|
|
230
265
|
def ignore_bot(cond)
|
@@ -239,6 +274,12 @@ module Mobb
|
|
239
274
|
end
|
240
275
|
end
|
241
276
|
|
277
|
+
def dest_to(channel)
|
278
|
+
after_condition do |res|
|
279
|
+
res.last[:dest_channel] = channel
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
242
283
|
def enable(*options) options.each { |option| set(option, true) }; end
|
243
284
|
def disable(*options) options.each { |option| set(option, false) }; end
|
244
285
|
def clear(*options) options.each { |option| set(option, nil) }; end
|
@@ -372,7 +413,7 @@ module Mobb
|
|
372
413
|
end
|
373
414
|
end
|
374
415
|
|
375
|
-
delegate :receive, :on,
|
416
|
+
delegate :receive, :on, :every, :cron,
|
376
417
|
:set, :enable, :disable, :clear,
|
377
418
|
:helpers
|
378
419
|
|
data/lib/mobb/version.rb
CHANGED
data/mobb.gemspec
CHANGED
@@ -19,7 +19,9 @@ Gem::Specification.new do |spec|
|
|
19
19
|
end
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_dependency "repp", "~> 0.
|
22
|
+
spec.add_dependency "repp", "~> 0.3"
|
23
|
+
spec.add_dependency "whenever", "~> 0.10"
|
24
|
+
spec.add_dependency "parse-cron", "~> 0.1"
|
23
25
|
|
24
26
|
spec.add_development_dependency "bundler", "~> 1.16"
|
25
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mobb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kinoppyd
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: repp
|
@@ -16,14 +16,42 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0.
|
19
|
+
version: '0.3'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0.
|
26
|
+
version: '0.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: whenever
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.10'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: parse-cron
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.1'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.1'
|
27
55
|
- !ruby/object:Gem::Dependency
|
28
56
|
name: bundler
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|