walle 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +3 -0
- data/.travis.yml +4 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +170 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/walle.rb +6 -0
- data/lib/walle/controller.rb +26 -0
- data/lib/walle/middlewares/builder.rb +46 -0
- data/lib/walle/middlewares/helper.rb +16 -0
- data/lib/walle/middlewares/logger.rb +17 -0
- data/lib/walle/robot.rb +56 -0
- data/lib/walle/robot/definitions.rb +76 -0
- data/lib/walle/robot/router.rb +41 -0
- data/lib/walle/robot/router/builder.rb +65 -0
- data/lib/walle/robot/router/route.rb +78 -0
- data/lib/walle/version.rb +3 -0
- data/walle.gemspec +27 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: acfc85300f6cc74758be0fb2219e21f2e0beebce
|
4
|
+
data.tar.gz: d5d5d4eb5cd483999b3195d6cd124e4012130c04
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4de7e44960484c935ef8afd8f12ede6cdbe856b8cc923868fd5b8628c616c0bf66e4e585f574c73a399985ce1c3255a0b7a80dbe8938338f62bdef575f8a53e9
|
7
|
+
data.tar.gz: 077f7233901f8ce310e3f695b654bcee973f2fbad4e13c23e4d2ef8af6e2a20eedc22c786a93ca7d88f8f274d5a61ffbe2922828b29ff0ccd18b809448fd33f6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 undr
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
# Walle
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/undr/walle.svg?branch=master)](https://travis-ci.org/undr/walle)
|
4
|
+
|
5
|
+
Simple DSL for building Slack bots.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'walle'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
```
|
18
|
+
$ bundle
|
19
|
+
```
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
```
|
24
|
+
$ gem install walle
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Example bot class:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class Robot < Walle::Robot
|
33
|
+
# Use middleware for all events
|
34
|
+
use Walle::Middlewares::Logger, Slack.config.logger
|
35
|
+
|
36
|
+
# catch `hello` events
|
37
|
+
hello { |env| }
|
38
|
+
|
39
|
+
# catch `start` events
|
40
|
+
start { |env| }
|
41
|
+
|
42
|
+
# catch `message` events
|
43
|
+
message { |env| }
|
44
|
+
|
45
|
+
# catch `close` events
|
46
|
+
close { |env| }
|
47
|
+
|
48
|
+
# catch `closed` events
|
49
|
+
closed { |env| }
|
50
|
+
|
51
|
+
routes do
|
52
|
+
# Use middleware ONLY for routes
|
53
|
+
use SomeCustomMidleware, 'arg1', 'arg2'
|
54
|
+
|
55
|
+
# Equals to match(/(?<command>convert|c)\s+(?<amount>\d+)\s*(?<from>[A-Z]{3})\s+into\s+(?<to>[A-Z]{3})/, controller: CurrencyConvertor)
|
56
|
+
command 'convert', 'c', amount: /\d+/, from: /[A-Z]{3}/, into: 'into', to: /[A-Z]{3}/, delimiter: /\s+/, controller: CurrencyConvertor
|
57
|
+
|
58
|
+
# Prefix all matches with bot name, eg. <@U2EU6KZDW>
|
59
|
+
direct do
|
60
|
+
# Matches string like "<@U2EU6KZDW> convert 10000 RUB into EUR"
|
61
|
+
match /convert\s+(?<amount>\d+)\s*(?<from>[A-Z]{3})\s+into\s+(?<to>[A-Z]{3})/ do |env|
|
62
|
+
# env.event - :message
|
63
|
+
# env.client - Slack::RealTime::Client
|
64
|
+
# env.data - Event data (it can contain extra data that can be added in middlewares)
|
65
|
+
# - type: "message"
|
66
|
+
# - channel: "V2T4X87M5"
|
67
|
+
# - user: "Z005UR62C"
|
68
|
+
# - text: "<@U2EU6KZDW> convert 10000 RUB into EUR"
|
69
|
+
# - ts: "1474580776.000003"
|
70
|
+
# - team: "T020WVADD"
|
71
|
+
# - ...
|
72
|
+
# env.matches - MatchData object with captured values:
|
73
|
+
# - amount: 10000
|
74
|
+
# - from: RUB
|
75
|
+
# - to: EUR
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Add prefix to all routes
|
80
|
+
prefix 'Hi (?<name>\w+),\s+' do
|
81
|
+
# Matches string like "Hi Dave, blah blah blah" and "Dave" will be captured in env.matches[:name]
|
82
|
+
match /.*/, controller: GreetingsController
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
Configure Slack client:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
Slack.configure do |c|
|
92
|
+
c.token = 'xxxx-9295867...'
|
93
|
+
c.logger = Logger.new(STDOUT)
|
94
|
+
c.logger.level = Logger::INFO
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
And run the bot: `Robot.run`, `Robot.run(async: true)`.
|
99
|
+
|
100
|
+
Also you can manually create the bot and give to him a prepared client:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
client = Slack::RealTime::Client.new(options)
|
104
|
+
robot = Robot.new(client, async: true)
|
105
|
+
robot.run
|
106
|
+
```
|
107
|
+
|
108
|
+
### Routes
|
109
|
+
|
110
|
+
There are three methods for creating routes: `match`, `command` and `default`:
|
111
|
+
|
112
|
+
- `match(regexp, options, &block)` - Add route based on regular expression.
|
113
|
+
- `regexp` - Regular expression.
|
114
|
+
- `options` - Options that adjust route behavior.
|
115
|
+
- `prefix` - Add prefix for regular expression, default: `/.*/`. If you want to delete prefix you should use `prefix: false`. `prefix: nil` resets prefix to default value.
|
116
|
+
- `direct` - Uses to create direct route. This’s route that directly address to bot. Messages without `<@botname>` at the start of command will be rejected.
|
117
|
+
- `controller` - Any class or object that has `call` method. It can be instance or class method.
|
118
|
+
- `block` - It will be used as controller when block is defined.
|
119
|
+
|
120
|
+
|
121
|
+
- `command(*commands, options, block)` - Construct a regular expression using command names and add route using `match`. Check examples.
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
# Equals to: match(/(?<command>convert|c)\s+(?<amount>\d+)\s*(?<from>[A-Z]{3})\s+into\s+(?<to>[A-Z]{3})/) { |env| }
|
125
|
+
command 'convert', 'c', amount: /\d+/, from: /[A-Z]{3}/, into: 'into', to: /[A-Z]{3}/, delimiter: /\s+/ do |env|
|
126
|
+
# "convert 10000 RUB into EUR"
|
127
|
+
# `env.matches` is:
|
128
|
+
# - command: "convert"
|
129
|
+
# - amount: 10000
|
130
|
+
# - from: "RUB"
|
131
|
+
# - to: "EUR"
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
Example:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
command 'add', 'subtract', 'multiply', 'divide', first: /\d+/, second: /\d+/ do |env|
|
139
|
+
result = case env.matches[:command]
|
140
|
+
when 'add'
|
141
|
+
env.matches[:first].to_i + env.matches[:second].to_i
|
142
|
+
when 'subtract'
|
143
|
+
env.matches[:first].to_i - env.matches[:second].to_i
|
144
|
+
when 'multiply'
|
145
|
+
env.matches[:first].to_i * env.matches[:second].to_i
|
146
|
+
when 'divide'
|
147
|
+
env.matches[:first].to_i / env.matches[:second].to_i
|
148
|
+
end
|
149
|
+
|
150
|
+
env.client.message(channel: env.data.channel, text: result.to_s)
|
151
|
+
end
|
152
|
+
```
|
153
|
+
|
154
|
+
- `default(options, block)` - Create default route. It will be used if no one route was matched.
|
155
|
+
|
156
|
+
### Controllers
|
157
|
+
|
158
|
+
TODO:
|
159
|
+
|
160
|
+
### Inheritance
|
161
|
+
|
162
|
+
All routes and event handlers from parent class and subclass will be merged when we inherit one bot from another one.
|
163
|
+
|
164
|
+
## Contributing
|
165
|
+
|
166
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/undr/walle.
|
167
|
+
|
168
|
+
## License
|
169
|
+
|
170
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "walle"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/walle.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Walle
|
2
|
+
class Controller
|
3
|
+
attr_reader :env
|
4
|
+
|
5
|
+
def initialize(env)
|
6
|
+
@env = env
|
7
|
+
end
|
8
|
+
|
9
|
+
def call
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
delegate :data, :client, :matches, to: :env
|
15
|
+
|
16
|
+
def message(options)
|
17
|
+
options[:channel] ||= data.channel
|
18
|
+
client.message(options)
|
19
|
+
end
|
20
|
+
|
21
|
+
def typing(options = {})
|
22
|
+
options[:channel] ||= data.channel
|
23
|
+
client.typing(options)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Walle
|
2
|
+
module Middlewares
|
3
|
+
class Builder
|
4
|
+
class Proxy
|
5
|
+
attr_reader :target_class, :app, :args, :block
|
6
|
+
|
7
|
+
def initialize(target_class, app, *args, &block)
|
8
|
+
@target_class, @app, @args, @block = target_class, app, args, block
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
with_env(env) do |target|
|
13
|
+
target.before if target.respond_to?(:before)
|
14
|
+
app.call(env)
|
15
|
+
target.after if target.respond_to?(:after)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def with_env(env)
|
22
|
+
yield target_class.new(env, *args, &block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(default_app = nil, &block)
|
27
|
+
@use, @run = [], default_app
|
28
|
+
instance_eval(&block) if block_given?
|
29
|
+
end
|
30
|
+
|
31
|
+
def use(middleware, *args, &block)
|
32
|
+
@use << ->(app){ Proxy.new(middleware, app, *args, &block) }
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_app(app)
|
36
|
+
app ||= @run
|
37
|
+
fail 'missing run statement' unless app
|
38
|
+
@use.reverse.inject(app){|a, e| e[a] }
|
39
|
+
end
|
40
|
+
|
41
|
+
def call(env)
|
42
|
+
to_app.call(env)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Walle
|
2
|
+
module Middlewares
|
3
|
+
module Helper
|
4
|
+
def use(middleware, *args)
|
5
|
+
@middlewares ||= Builder.new
|
6
|
+
@middlewares.use(middleware, *args)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def run_middlewares(env, &block)
|
12
|
+
@middlewares.to_app(block).call(env)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Walle
|
2
|
+
module Middlewares
|
3
|
+
class Logger
|
4
|
+
attr_reader :env, :logger, :options
|
5
|
+
|
6
|
+
def initialize(env, logger, options = {})
|
7
|
+
@env = env
|
8
|
+
@logger = logger
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
|
12
|
+
def before
|
13
|
+
logger.info("[#{env.event}] - #{env.data.to_json}")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/walle/robot.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'walle/middlewares/helper'
|
2
|
+
require 'walle/middlewares/builder'
|
3
|
+
require 'walle/middlewares/logger'
|
4
|
+
require 'walle/robot/router'
|
5
|
+
require 'walle/robot/definitions'
|
6
|
+
require 'walle/controller'
|
7
|
+
|
8
|
+
module Walle
|
9
|
+
class Robot
|
10
|
+
Environment = Struct.new(:event, :client, :data, :matches)
|
11
|
+
|
12
|
+
class_attribute :_definitions, instance_writer: false
|
13
|
+
self._definitions = Definitions::Stack.new
|
14
|
+
|
15
|
+
class << self
|
16
|
+
delegate *Definitions::METHODS, to: :_definitions
|
17
|
+
|
18
|
+
def inherited(subclass)
|
19
|
+
subclass._definitions.push(Definitions.new)
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
23
|
+
def run(options = {}, &block)
|
24
|
+
client = Slack::RealTime::Client.new(options)
|
25
|
+
instance = new(client, options)
|
26
|
+
instance.run(&block)
|
27
|
+
instance
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
attr_reader :client
|
32
|
+
|
33
|
+
def initialize(client, options = {})
|
34
|
+
@client = client
|
35
|
+
@async = !!options.delete(:async)
|
36
|
+
@options = options
|
37
|
+
end
|
38
|
+
|
39
|
+
def run(&block)
|
40
|
+
_definitions.apply_to(client)
|
41
|
+
async? ? client.start_async(&block) : client.start!(&block)
|
42
|
+
end
|
43
|
+
|
44
|
+
def stop
|
45
|
+
client.stop!
|
46
|
+
end
|
47
|
+
|
48
|
+
def started?
|
49
|
+
client.started?
|
50
|
+
end
|
51
|
+
|
52
|
+
def async?
|
53
|
+
@async
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Walle
|
2
|
+
class Robot
|
3
|
+
class Definitions
|
4
|
+
EVENTS = %i{start hello close closed message}
|
5
|
+
METHODS = EVENTS + %i{use routes pattern}
|
6
|
+
|
7
|
+
class Stack
|
8
|
+
delegate *Definitions::METHODS, to: :current
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@entities = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def push(definitions)
|
15
|
+
@entities << definitions
|
16
|
+
end
|
17
|
+
|
18
|
+
def current
|
19
|
+
@entities.last
|
20
|
+
end
|
21
|
+
|
22
|
+
def apply_to(client)
|
23
|
+
@entities.each do |definitions|
|
24
|
+
definitions.apply_to(client)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
include ::Walle::Middlewares::Helper
|
30
|
+
|
31
|
+
attr_reader :all_handlers
|
32
|
+
|
33
|
+
def initialize
|
34
|
+
@all_handlers = Hash.new { |h, k| h[k] = [] }
|
35
|
+
end
|
36
|
+
|
37
|
+
EVENTS.each do |event|
|
38
|
+
define_method(event) do |&handler|
|
39
|
+
handlers(event) << handler
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def routes(&block)
|
44
|
+
router = Router.new(&block)
|
45
|
+
message { |env| router.call(env) }
|
46
|
+
end
|
47
|
+
|
48
|
+
def apply_to(client)
|
49
|
+
all_handlers.each do |event, event_handlers|
|
50
|
+
event_handlers.each do |handler|
|
51
|
+
client.on(event) do |data|
|
52
|
+
handle_event(environment(event, client, data), handler)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def handlers(event = nil)
|
59
|
+
return all_handlers[event] if event
|
60
|
+
all_handlers.values.flatten.uniq
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def environment(event, client, data)
|
66
|
+
Environment.new(event, client, data)
|
67
|
+
end
|
68
|
+
|
69
|
+
def handle_event(env, handler)
|
70
|
+
run_middlewares(env) do |env|
|
71
|
+
handler.call(env)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'walle/robot/router/route'
|
2
|
+
require 'walle/robot/router/builder'
|
3
|
+
|
4
|
+
module Walle
|
5
|
+
class Robot
|
6
|
+
class Router
|
7
|
+
include ::Walle::Middlewares::Helper
|
8
|
+
|
9
|
+
DEFAULT_CONTROLLER = -> (*_) {}
|
10
|
+
DEFAULT_ROUTER = Route.new(regexp: /.*/, controller: DEFAULT_CONTROLLER)
|
11
|
+
|
12
|
+
attr_reader :routes, :middlewares, :default
|
13
|
+
|
14
|
+
def initialize(&block)
|
15
|
+
@routes = []
|
16
|
+
@default = { true => DEFAULT_ROUTER, false => DEFAULT_ROUTER }
|
17
|
+
run_builder!(block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def call(env)
|
21
|
+
run_middlewares(env) do |env|
|
22
|
+
lookup_route(env).call(env)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def all_routes
|
29
|
+
routes + default.values.uniq
|
30
|
+
end
|
31
|
+
|
32
|
+
def lookup_route(env)
|
33
|
+
all_routes.find { |route| route.match?(env) } || DEFAULT_ROUTER
|
34
|
+
end
|
35
|
+
|
36
|
+
def run_builder!(block)
|
37
|
+
Builder.new(self).build(block)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Walle
|
2
|
+
class Robot
|
3
|
+
class Router
|
4
|
+
class Builder
|
5
|
+
ROUTE_OPTIONS = %i{controller prefix direct delimiter}
|
6
|
+
|
7
|
+
instance_methods.each do |method|
|
8
|
+
undef_method(method) if method !~ /^(__|instance_eval|class|object_id|with_options|singleton_class|inspect)/
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :router
|
12
|
+
|
13
|
+
def initialize(router)
|
14
|
+
@router = router
|
15
|
+
end
|
16
|
+
|
17
|
+
def pattern(*patterns)
|
18
|
+
end
|
19
|
+
|
20
|
+
def use(middleware, *args)
|
21
|
+
router.use(middleware, *args)
|
22
|
+
end
|
23
|
+
|
24
|
+
def command(*commands, &block)
|
25
|
+
arguments = commands.extract_options!
|
26
|
+
options = arguments.extract!(*ROUTE_OPTIONS)
|
27
|
+
|
28
|
+
regexp = { command: /#{commands.join(?|)}/ }.merge(arguments).map do |name, regexp|
|
29
|
+
regexp.is_a?(Regexp) ? "(?<#{name}>#{regexp.source})" : "#{regexp}"
|
30
|
+
end.join(options[:delimiter].try(:source) || '\s+')
|
31
|
+
|
32
|
+
match(Regexp.new(regexp), options, &block)
|
33
|
+
end
|
34
|
+
|
35
|
+
def direct(&block)
|
36
|
+
with_options(direct: true, &block)
|
37
|
+
end
|
38
|
+
|
39
|
+
def prefix(value, &block)
|
40
|
+
with_options(prefix: value, &block)
|
41
|
+
end
|
42
|
+
|
43
|
+
def match(regexp, options = {}, &block)
|
44
|
+
controller = extract_controller(options, &block)
|
45
|
+
router.routes << Route.new(options.slice(*ROUTE_OPTIONS).merge(controller: controller, regexp: regexp))
|
46
|
+
end
|
47
|
+
|
48
|
+
def default(options = {}, &block)
|
49
|
+
controller = extract_controller(options, &block)
|
50
|
+
router.default[!!options[:direct]] = Route.new(options.slice(*ROUTE_OPTIONS).merge(controller: controller))
|
51
|
+
end
|
52
|
+
|
53
|
+
def build(block)
|
54
|
+
instance_eval(&block)
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def extract_controller(options, &block)
|
60
|
+
block_given? ? block : options[:controller].presence || DEFAULT_CONTROLLER
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# TODO: Add support of this: https://github.com/andrewberls/regularity
|
2
|
+
module Walle
|
3
|
+
class Robot
|
4
|
+
class Router
|
5
|
+
class Route
|
6
|
+
attr_reader :controller, :regexp, :options
|
7
|
+
|
8
|
+
class Mutator
|
9
|
+
attr_reader :env, :regexp, :options
|
10
|
+
|
11
|
+
def initialize(env, regexp, options = {})
|
12
|
+
@env = env
|
13
|
+
@regexp = regexp
|
14
|
+
@options = options
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Prefix < Mutator
|
19
|
+
def mutate
|
20
|
+
return regexp unless prefix
|
21
|
+
|
22
|
+
if regexp
|
23
|
+
Regexp.new("#{prefix}#{regexp.source}")
|
24
|
+
else
|
25
|
+
Regexp.new(prefix)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def prefix
|
30
|
+
return if options[:prefix] === false
|
31
|
+
options[:prefix] || '.*'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Direct < Mutator
|
36
|
+
def mutate
|
37
|
+
return regexp unless direct?
|
38
|
+
|
39
|
+
robot_name = env.client.self.id
|
40
|
+
expr = regexp || /.*/
|
41
|
+
Regexp.new("<@#{robot_name}>\\s+#{expr.source}")
|
42
|
+
end
|
43
|
+
|
44
|
+
def direct?
|
45
|
+
options[:direct]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
MUTATORS = [Direct, Prefix]
|
50
|
+
|
51
|
+
def initialize(controller:, regexp: nil, **options)
|
52
|
+
@controller = controller
|
53
|
+
@options = options
|
54
|
+
@regexp = regexp
|
55
|
+
end
|
56
|
+
|
57
|
+
def match?(env)
|
58
|
+
expr = final_regexp(env)
|
59
|
+
!!expr.match(env.data.text)
|
60
|
+
end
|
61
|
+
|
62
|
+
def call(env)
|
63
|
+
expr = final_regexp(env)
|
64
|
+
env.matches = expr.match(env.data.text) if expr
|
65
|
+
cntrl.respond_to?(:call) ? cntrl.call(env) : cntrl.new(env).call
|
66
|
+
end
|
67
|
+
|
68
|
+
alias :cntrl :controller
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def final_regexp(env)
|
73
|
+
MUTATORS.reduce(regexp) { |result, mutator| mutator.new(env, result, options).mutate }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/walle.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'walle/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'walle'
|
8
|
+
spec.version = Walle::VERSION
|
9
|
+
spec.authors = ['undr']
|
10
|
+
spec.email = ['undr@yandex.ru']
|
11
|
+
|
12
|
+
spec.summary = %q{Simple DSL for building Slack bots.}
|
13
|
+
spec.description = %q{Simple DSL for building Slack bots.}
|
14
|
+
spec.homepage = 'https://github.com/undr/walle'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'slack-ruby-client'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: walle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- undr
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: slack-ruby-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Simple DSL for building Slack bots.
|
70
|
+
email:
|
71
|
+
- undr@yandex.ru
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/walle.rb
|
86
|
+
- lib/walle/controller.rb
|
87
|
+
- lib/walle/middlewares/builder.rb
|
88
|
+
- lib/walle/middlewares/helper.rb
|
89
|
+
- lib/walle/middlewares/logger.rb
|
90
|
+
- lib/walle/robot.rb
|
91
|
+
- lib/walle/robot/definitions.rb
|
92
|
+
- lib/walle/robot/router.rb
|
93
|
+
- lib/walle/robot/router/builder.rb
|
94
|
+
- lib/walle/robot/router/route.rb
|
95
|
+
- lib/walle/version.rb
|
96
|
+
- walle.gemspec
|
97
|
+
homepage: https://github.com/undr/walle
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.4.8
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Simple DSL for building Slack bots.
|
121
|
+
test_files: []
|