mobb 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +55 -1
- data/lib/mobb/base.rb +45 -14
- data/lib/mobb/version.rb +1 -1
- data/mobb.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1786f31311eda789fd50527e7a5cbdbce91c63388668ba248db37f32f5c2578
|
4
|
+
data.tar.gz: c89c89b84d4e6298152dd78113d941bc397dfa2c65ea352bafa084caa5a5d009
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74778071b90f9656e2cd2e29b8b88e98ccf6168c05f33c0af44c82036f1214eac7ac8bc6742cd6658a2039849fa35bc117cf55e515cb409e0945cc80b859b03c
|
7
|
+
data.tar.gz: a0c47902564e323ccb4be959451ec21d433ceedb66f656efa1d56762e1705e0fea316f92ce1c03444bc1100f5ebe45e3381a9bb75e59c9b4ed3bd14db77f4600
|
data/README.md
CHANGED
@@ -32,7 +32,7 @@ Write your logic in `app.rb` like...
|
|
32
32
|
require 'mobb'
|
33
33
|
|
34
34
|
set :name, "example bot"
|
35
|
-
|
35
|
+
|
36
36
|
on "hello" do
|
37
37
|
"hi! i'm #{settings.name}!"
|
38
38
|
end
|
@@ -50,6 +50,60 @@ then the shell will start to wait for your input, so you can type 'hello' and hi
|
|
50
50
|
hi! i'm example bot!"
|
51
51
|
```
|
52
52
|
|
53
|
+
## Helpers
|
54
|
+
|
55
|
+
You can define helper methods like this.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
require 'mobb'
|
59
|
+
|
60
|
+
helpers do
|
61
|
+
def greet(name)
|
62
|
+
"Hi #{name}, what't up"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
on "hello" do
|
67
|
+
greet(@env.user.name)
|
68
|
+
end
|
69
|
+
|
70
|
+
```
|
71
|
+
|
72
|
+
## Conditions
|
73
|
+
|
74
|
+
You can use conditions `ignore_bot` and `reply_to_me`.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
require 'mobb'
|
78
|
+
set :service, 'slack'
|
79
|
+
|
80
|
+
# You must set `ignore_bot` true when response same message
|
81
|
+
on 'Yo', ignore_bot: true do
|
82
|
+
'Yo'
|
83
|
+
end
|
84
|
+
|
85
|
+
on /Hi/, reply_to_me: true do
|
86
|
+
"Hi #{@env.user.name}"
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
And you can define conditions yourself.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
require 'mobb'
|
94
|
+
set :service, 'slack'
|
95
|
+
|
96
|
+
set(:probability) { |value| condition { rand <= value } }
|
97
|
+
|
98
|
+
on /Yo/, reply_to_me: true, probability: 0.1 do
|
99
|
+
"Yo"
|
100
|
+
end
|
101
|
+
|
102
|
+
on /Yo/, reply_to_me: true do
|
103
|
+
"Ha?"
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
53
107
|
# Service handlers
|
54
108
|
|
55
109
|
Mobb is implemented based on [Repp](https://github.com/kinoppyd/repp) Interface.
|
data/lib/mobb/base.rb
CHANGED
@@ -92,8 +92,8 @@ module Mobb
|
|
92
92
|
|
93
93
|
def handle_event(base = settings, passed_block = nil)
|
94
94
|
if responds = base.events[@env.event_type]
|
95
|
-
responds.each do |pattern, block|
|
96
|
-
process_event(pattern) do |*args|
|
95
|
+
responds.each do |pattern, block, conditions|
|
96
|
+
process_event(pattern, conditions) do |*args|
|
97
97
|
event_eval { block[*args] }
|
98
98
|
end
|
99
99
|
end
|
@@ -103,15 +103,19 @@ module Mobb
|
|
103
103
|
nil
|
104
104
|
end
|
105
105
|
|
106
|
-
def process_event(pattern, block = nil, values = [])
|
106
|
+
def process_event(pattern, conditions, block = nil, values = [])
|
107
107
|
res = pattern.match?(@env.body)
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
108
|
+
catch(:pass) do
|
109
|
+
conditions.each { |c| throw :pass unless c.bind(self).call }
|
110
|
+
|
111
|
+
case res
|
112
|
+
when ::Mobb::Matcher::Matched
|
113
|
+
yield(self, *(res.matched))
|
114
|
+
when TrueClass
|
115
|
+
yield self
|
116
|
+
else
|
117
|
+
nil
|
118
|
+
end
|
115
119
|
end
|
116
120
|
end
|
117
121
|
|
@@ -136,6 +140,7 @@ module Mobb
|
|
136
140
|
|
137
141
|
def reset!
|
138
142
|
@events = {}
|
143
|
+
@conditions = []
|
139
144
|
end
|
140
145
|
|
141
146
|
def settings
|
@@ -151,13 +156,17 @@ module Mobb
|
|
151
156
|
(@events[type] ||= []) << compile!(type, pattern, options, &block)
|
152
157
|
end
|
153
158
|
|
154
|
-
def compile!(type, pattern,
|
155
|
-
|
159
|
+
def compile!(type, pattern, options, &block)
|
160
|
+
options.each_pair { |option, args| send(option, *args) }
|
161
|
+
|
162
|
+
matcher = compile(pattern, options)
|
156
163
|
unbound_method = generate_method("#{type}", &block)
|
164
|
+
conditions, @conditions = @conditions, []
|
157
165
|
wrapper = block.arity != 0 ?
|
158
166
|
proc { |instance, args| unbound_method.bind(instance).call(*args) } :
|
159
167
|
proc { |instance, args| unbound_method.bind(instance).call }
|
160
|
-
|
168
|
+
|
169
|
+
[ matcher, wrapper, conditions ]
|
161
170
|
end
|
162
171
|
|
163
172
|
def compile(pattern, options) Matcher.new(pattern, options); end
|
@@ -169,6 +178,11 @@ module Mobb
|
|
169
178
|
method
|
170
179
|
end
|
171
180
|
|
181
|
+
def helpers(*extensions, &block)
|
182
|
+
class_eval(&block) if block_given?
|
183
|
+
include(*extensions) if extensions.any?
|
184
|
+
end
|
185
|
+
|
172
186
|
def development?; environment == :development; end
|
173
187
|
def production?; environment == :production; end
|
174
188
|
def test?; environment == :test; end
|
@@ -209,6 +223,22 @@ module Mobb
|
|
209
223
|
self
|
210
224
|
end
|
211
225
|
|
226
|
+
def condition(name = "#{caller.first[/`.*'/]} condition", &block)
|
227
|
+
@conditions << generate_method(name, &block)
|
228
|
+
end
|
229
|
+
|
230
|
+
def ignore_bot(cond)
|
231
|
+
condition do
|
232
|
+
@env.bot? != cond
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
def reply_to_me(cond)
|
237
|
+
condition do
|
238
|
+
@env.reply_to.include?(settings.name) == cond
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
212
242
|
def enable(*options) options.each { |option| set(option, true) }; end
|
213
243
|
def disable(*options) options.each { |option| set(option, false) }; end
|
214
244
|
def clear(*options) options.each { |option| set(option, nil) }; end
|
@@ -343,7 +373,8 @@ module Mobb
|
|
343
373
|
end
|
344
374
|
|
345
375
|
delegate :receive, :on, #:every,
|
346
|
-
:set, :enable, :disable, :clear
|
376
|
+
:set, :enable, :disable, :clear,
|
377
|
+
:helpers
|
347
378
|
|
348
379
|
class << self
|
349
380
|
attr_accessor :target
|
data/lib/mobb/version.rb
CHANGED
data/mobb.gemspec
CHANGED
@@ -19,7 +19,7 @@ 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.2"
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.16"
|
25
25
|
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.2.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-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: repp
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0.
|
19
|
+
version: '0.2'
|
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.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|