pepito 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +41 -0
- data/.rubocop.yml +8 -0
- data/Gemfile +3 -0
- data/README.md +26 -0
- data/Rakefile +11 -0
- data/bin/pepito +5 -0
- data/lib/pepito/adapter.rb +101 -0
- data/lib/pepito/adapters.rb +4 -0
- data/lib/pepito/database.rb +14 -0
- data/lib/pepito/errors/missing_configuration_value_error.rb +15 -0
- data/lib/pepito/errors/required_methods_error.rb +15 -0
- data/lib/pepito/handler/chat_router.rb +36 -0
- data/lib/pepito/handler/http_router.rb +30 -0
- data/lib/pepito/handler.rb +38 -0
- data/lib/pepito/handlers/extensions_catalog.rb +108 -0
- data/lib/pepito/handlers/help.rb +36 -0
- data/lib/pepito/handlers/info.rb +40 -0
- data/lib/pepito/handlers/room.rb +23 -0
- data/lib/pepito/http_api/http_callback.rb +49 -0
- data/lib/pepito/http_api/http_route.rb +48 -0
- data/lib/pepito/http_api/rack_app.rb +27 -0
- data/lib/pepito/message.rb +59 -0
- data/lib/pepito/robot.rb +367 -0
- data/lib/pepito/source.rb +32 -0
- data/lib/pepito/version.rb +4 -0
- data/lib/pepito/web_app/app.rb +41 -0
- data/lib/pepito/web_app/views/index.erb +69 -0
- data/lib/pepito.rb +21 -0
- data/pepito.gemspec +35 -0
- data/test/pepito/handler/test_chat_router.rb +37 -0
- data/test/pepito/handler/test_http_router.rb +20 -0
- data/test/pepito/handlers/test_extensions_catalog.rb +38 -0
- data/test/pepito/handlers/test_help.rb +38 -0
- data/test/pepito/handlers/test_info.rb +32 -0
- data/test/pepito/handlers/test_room.rb +33 -0
- data/test/pepito/test_adapter.rb +71 -0
- data/test/pepito/test_handler.rb +28 -0
- data/test/pepito/test_message.rb +101 -0
- data/test/pepito/test_robot.rb +259 -0
- data/test/pepito/test_source.rb +27 -0
- data/test/test_pepito.rb +8 -0
- metadata +300 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1ee1cd04819c4677c99a67c3b2bed1e4d389ce13
|
4
|
+
data.tar.gz: 6e3e17405867abdfd067fd27a3eeb196169f57c6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bee249611a496e8334bfb1fde82aa8334d2cd18108d05f08fe2208fd5b28314e35602d4b0ec65966eebcea4889a48151d9acbedc46f31827bd4dfa27eda0ecea
|
7
|
+
data.tar.gz: a4f45329a147ef5d609b27e5dd511d46858599a5cee2915ed6f4cda5b946c376ef9885a79597fe61181417a05cfda20ecfcf926e533a3458d6ba0228dea9453e
|
data/.gitignore
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile '~/.gitignore_global'
|
6
|
+
|
7
|
+
# Ignore bundler config.
|
8
|
+
/.bundle
|
9
|
+
|
10
|
+
# Ignore all logfiles and tempfiles.
|
11
|
+
/log/*
|
12
|
+
!/log/.keep
|
13
|
+
/tmp
|
14
|
+
config/database.yml
|
15
|
+
public/assets/
|
16
|
+
.rvmrc
|
17
|
+
/public/cache
|
18
|
+
/public/system/*
|
19
|
+
.DS_Store
|
20
|
+
**/.DS_Store
|
21
|
+
config/application.yml
|
22
|
+
.ruby-gemset
|
23
|
+
.ruby-version
|
24
|
+
dump.rdb
|
25
|
+
Gemfile.lock
|
26
|
+
|
27
|
+
# Documentation
|
28
|
+
doc
|
29
|
+
.yardoc
|
30
|
+
|
31
|
+
# Vim
|
32
|
+
[._]*.s[a-w][a-z]
|
33
|
+
[._]s[a-w][a-z]
|
34
|
+
*.un~
|
35
|
+
Session.vim
|
36
|
+
.netrwhist
|
37
|
+
*~
|
38
|
+
|
39
|
+
# Pepito Lock
|
40
|
+
pepito.gemspec.lock
|
41
|
+
pepito-*.gem
|
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Pepito
|
2
|
+
|
3
|
+
[![Build Status](https://semaphoreci.com/api/v1/projects/a4218e38-25b8-49e4-9bec-c2875845577b/620655/badge.svg)](https://semaphoreci.com/olimart/pepito)
|
4
|
+
|
5
|
+
## Getting started
|
6
|
+
|
7
|
+
### Development
|
8
|
+
* Clone
|
9
|
+
* Make sure all dependencies are installed by running
|
10
|
+
```
|
11
|
+
bundle install
|
12
|
+
```
|
13
|
+
* Create an alias to easily run the command
|
14
|
+
```bash
|
15
|
+
alias devpepito="ruby -I /path/to/pepito/lib /path/to/pepito/bin/pepito"
|
16
|
+
```
|
17
|
+
* To start, start a redis server and afterwards run
|
18
|
+
```
|
19
|
+
devpepito
|
20
|
+
|
21
|
+
or if you want to specify a redis_url do
|
22
|
+
|
23
|
+
REDIS_URL="redis://:p4ssw0rd@10.0.1.1:6380/15" devpepito
|
24
|
+
```
|
25
|
+
* By defaults all handlers and adapters are turned off, go to http://localhost:4567 to start activating them
|
26
|
+
* After activating the Help handler run `pepito help` to see available commands
|
data/Rakefile
ADDED
data/bin/pepito
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
require_relative 'errors/required_methods_error'
|
2
|
+
require_relative 'errors/missing_configuration_value_error'
|
3
|
+
|
4
|
+
module Pepito
|
5
|
+
# Parent class for any adapter
|
6
|
+
# Adapters allow to communicate with the bot from differente platforms.
|
7
|
+
class Adapter
|
8
|
+
class << self
|
9
|
+
# Configs for the adapter, override this method in the adapter
|
10
|
+
# @return [Array<Hash>] Array of strings with the config parameters
|
11
|
+
def configs
|
12
|
+
[{}]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# List of required methods for adapters
|
17
|
+
REQUIRED_METHODS = %i(
|
18
|
+
run
|
19
|
+
send_messages
|
20
|
+
topic
|
21
|
+
stop
|
22
|
+
).freeze
|
23
|
+
|
24
|
+
# @!method run
|
25
|
+
# Runs the adapter
|
26
|
+
# @return [void]
|
27
|
+
# @abstract This should be implemented by the adapter
|
28
|
+
|
29
|
+
# @!method send_messages
|
30
|
+
# Sends a message to the service
|
31
|
+
# @param target [Pepito::Source] The target to send the message to
|
32
|
+
# @param strings [Array<String>] Array of strings to send
|
33
|
+
# @return [void]
|
34
|
+
# @abstract This should be implemented by the adapter
|
35
|
+
|
36
|
+
# @!method topic
|
37
|
+
# Sets the topic of the channel
|
38
|
+
# @param topic [String] the new topic
|
39
|
+
# @return [void]
|
40
|
+
# @abstract This should be implemented by the adapter
|
41
|
+
|
42
|
+
# @!method stop
|
43
|
+
# Stops the adapter
|
44
|
+
# @return [void]
|
45
|
+
# @abstract This should be implemented by the adapter
|
46
|
+
|
47
|
+
# The currently running robot
|
48
|
+
# @return [Pepito::Robot]
|
49
|
+
attr_reader :robot
|
50
|
+
|
51
|
+
# Config hash for the adapter
|
52
|
+
# @return [Hash<String,String>]
|
53
|
+
attr_reader :config
|
54
|
+
|
55
|
+
# Initializes the Adapter class
|
56
|
+
# @param robot [Pepito::Robot] The currently running robot
|
57
|
+
# @param config [Hash<String,String>] The config for the adapter
|
58
|
+
# @return [void]
|
59
|
+
def initialize(robot, config)
|
60
|
+
@robot = robot
|
61
|
+
@config = config
|
62
|
+
|
63
|
+
begin
|
64
|
+
missing_methods
|
65
|
+
missing_configuration_values
|
66
|
+
rescue => e
|
67
|
+
raise e
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
# Check whether all required methods are implemented
|
74
|
+
# Raises a [Pepito::Errors::RequiredMethodsError] if methods are missing
|
75
|
+
# @return [void]
|
76
|
+
def missing_methods
|
77
|
+
methods = []
|
78
|
+
REQUIRED_METHODS.each do |method|
|
79
|
+
methods << method unless self.methods.include?(method)
|
80
|
+
end
|
81
|
+
raise Errors::RequiredMethodsError.new(methods), 'missing required methods' unless methods.empty?
|
82
|
+
end
|
83
|
+
|
84
|
+
# Check whether all required configuration values exist
|
85
|
+
# Raises a [Pepito::Errors::MissingConfigurationValueError] if methods are missing
|
86
|
+
# @return [void]
|
87
|
+
def missing_configuration_values
|
88
|
+
confs = []
|
89
|
+
self.class.configs.each do |c|
|
90
|
+
confs << c[:name] if c[:required] && !configuration_value?(c[:name])
|
91
|
+
end
|
92
|
+
raise Errors::MissingConfigurationValueError.new(confs), 'missing required configuration values' unless confs.empty?
|
93
|
+
end
|
94
|
+
|
95
|
+
# Whether a configuration_value is present or not.
|
96
|
+
# @return [Boolean] True if is present, False otherwise.
|
97
|
+
def configuration_value?(name)
|
98
|
+
!@config.nil? && @config.keys.include?(name) && !@config[name].nil? && !@config[name].empty?
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'redis-namespace'
|
2
|
+
|
3
|
+
module Pepito
|
4
|
+
# Database for pepito configurations
|
5
|
+
class Database < Redis::Namespace
|
6
|
+
attr_reader :redis
|
7
|
+
|
8
|
+
# @return [void]
|
9
|
+
def initialize
|
10
|
+
@redis = Redis.new
|
11
|
+
super(Pepito::REDIS_NAMESPACE, redis: @redis)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Pepito
|
2
|
+
module Errors
|
3
|
+
# An error to return when an adapter is missing required methods
|
4
|
+
class MissingConfigurationValueError < StandardError
|
5
|
+
# @return [Array<String>] Array of missing configuration values.
|
6
|
+
attr_reader :configuration_values
|
7
|
+
|
8
|
+
# @param configuration_values [Array<String>] Array of missing configuration values.
|
9
|
+
# @return [void]
|
10
|
+
def initialize(configuration_values)
|
11
|
+
@configuration_values = configuration_values
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Pepito
|
2
|
+
module Errors
|
3
|
+
# An error to return when an adapter is missing required methods
|
4
|
+
class RequiredMethodsError < StandardError
|
5
|
+
# @return [Array<String>] Array of missing methods.
|
6
|
+
attr_reader :missing_methods
|
7
|
+
|
8
|
+
# @param missing_methods [Array<String>] Array of missing methods.
|
9
|
+
# @return [void]
|
10
|
+
def initialize(missing_methods)
|
11
|
+
@missing_methods = missing_methods
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Pepito
|
2
|
+
class Handler
|
3
|
+
# Router for chat messages.
|
4
|
+
module ChatRouter
|
5
|
+
# A struct representing the route.
|
6
|
+
ChatRoute = Struct.new(
|
7
|
+
:pattern,
|
8
|
+
:func,
|
9
|
+
:command,
|
10
|
+
:help
|
11
|
+
)
|
12
|
+
|
13
|
+
# Add a Pepito::Handler::ChatRoute struct to the chat_routes array.
|
14
|
+
# @param pattern [Regexp] Pattern for the chat route.
|
15
|
+
# @param func [Symbol] The name of the function to call.
|
16
|
+
# @param options [Hash]
|
17
|
+
# command [Boolean] Whether the route is a command or not.
|
18
|
+
# help [String] The string for the help menu.
|
19
|
+
# @return [void]
|
20
|
+
def chat_route(pattern, func, options = {})
|
21
|
+
chat_routes << ChatRoute.new(
|
22
|
+
pattern,
|
23
|
+
func,
|
24
|
+
options.delete(:command) || false,
|
25
|
+
options.delete(:help) || nil
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
# List of chat routes.
|
30
|
+
# @return [Array<Pepito::Handler::ChatRouter::ChatRoute>]
|
31
|
+
def chat_routes
|
32
|
+
@chat_routes ||= []
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'http_router'
|
2
|
+
|
3
|
+
require_relative '../http_api/http_route'
|
4
|
+
|
5
|
+
module Pepito
|
6
|
+
class Handler
|
7
|
+
# Routes for the http api
|
8
|
+
module HTTPRouter
|
9
|
+
# Add an http route to the http_routes array.
|
10
|
+
# @param method [String] HTTP method for this route. Example: "GET".
|
11
|
+
# @param path [String] Path for the http api. Example: "/info".
|
12
|
+
# @param cb [Symbol] Callback function.
|
13
|
+
# @return [void]
|
14
|
+
def http_route(method, path, cb)
|
15
|
+
http_routes << HTTPApi::HTTPRoute.new(
|
16
|
+
@robot,
|
17
|
+
self,
|
18
|
+
method,
|
19
|
+
path,
|
20
|
+
cb)
|
21
|
+
end
|
22
|
+
|
23
|
+
# List of http routes.
|
24
|
+
# @return [Array<Pepito::HTTPApi::HTTPRoute>]
|
25
|
+
def http_routes
|
26
|
+
@http_routes ||= []
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative 'handler/http_router'
|
2
|
+
require_relative 'handler/chat_router'
|
3
|
+
|
4
|
+
module Pepito
|
5
|
+
# Parent class for any handler
|
6
|
+
# Handlers implement the logic for chat and http routes for the robot
|
7
|
+
class Handler
|
8
|
+
include Handler::HTTPRouter
|
9
|
+
include Handler::ChatRouter
|
10
|
+
|
11
|
+
# Currently running robot
|
12
|
+
# @return [Pepito::Robot]
|
13
|
+
attr_reader :robot
|
14
|
+
|
15
|
+
# Initializes the Adapter class
|
16
|
+
# @param robot [Pepito::Robot] The currently running robot
|
17
|
+
# @return [void]
|
18
|
+
def initialize(robot)
|
19
|
+
@robot = robot
|
20
|
+
end
|
21
|
+
|
22
|
+
# Starts the handler
|
23
|
+
# @return [void]
|
24
|
+
def start
|
25
|
+
register_http_routes
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Registers handler's http routes to the http_api
|
31
|
+
# @return [void]
|
32
|
+
def register_http_routes
|
33
|
+
@http_routes.each do |route|
|
34
|
+
@robot.http_api.router.add_route(route.route)
|
35
|
+
end if @http_routes
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
|
3
|
+
require_relative '../handler'
|
4
|
+
|
5
|
+
module Pepito
|
6
|
+
module Handlers
|
7
|
+
# Handler to manage (start, stop, status) the extensions.
|
8
|
+
class ExtensionsCatalog < Handler
|
9
|
+
# Run the extensions handler.
|
10
|
+
# @return [void]
|
11
|
+
def run
|
12
|
+
http_route('GET', '/extensions', :extensions_web)
|
13
|
+
http_route('GET', '/handlers', :handlers_web)
|
14
|
+
http_route('GET', '/adapters', :adapters_web)
|
15
|
+
http_route('GET', '/routes', :routes_web)
|
16
|
+
|
17
|
+
chat_route(/^handlers list$/i, :handlers_chat, command: true, help: 'handlers list -> show list of active handlers')
|
18
|
+
chat_route(/^adapters list$/i, :adapters_chat, command: true, help: 'adapters list -> show list of active adapters')
|
19
|
+
chat_route(/^routes list$/i, :routes_chat, command: true, help: 'routes list -> show list of active api routes')
|
20
|
+
end
|
21
|
+
|
22
|
+
# HTTP API List the running handlers and adapters.
|
23
|
+
# @param _request [Rack::Request]
|
24
|
+
# @param response [Rack::Response]
|
25
|
+
# @return [void]
|
26
|
+
def extensions_web(_request, response)
|
27
|
+
response.headers['Content-Type'] = 'application/json'
|
28
|
+
json = MultiJson.dump(
|
29
|
+
handlers: @robot.handlers.keys,
|
30
|
+
adapters: @robot.adapters.keys
|
31
|
+
)
|
32
|
+
response.write(json)
|
33
|
+
end
|
34
|
+
|
35
|
+
# HTTP API List the running handlers.
|
36
|
+
# @param _request [Rack::Request]
|
37
|
+
# @param response [Rack::Response]
|
38
|
+
# @return [void]
|
39
|
+
def handlers_web(_request, response)
|
40
|
+
response.headers['Content-Type'] = 'application/json'
|
41
|
+
json = MultiJson.dump(
|
42
|
+
handlers: @robot.handlers.keys
|
43
|
+
)
|
44
|
+
response.write(json)
|
45
|
+
end
|
46
|
+
|
47
|
+
# HTTP API List the running adapters.
|
48
|
+
# @param _request [Rack::Request]
|
49
|
+
# @param response [Rack::Response]
|
50
|
+
# @return [void]
|
51
|
+
def adapters_web(_request, response)
|
52
|
+
response.headers['Content-Type'] = 'application/json'
|
53
|
+
json = MultiJson.dump(
|
54
|
+
adapters: @robot.adapters.keys
|
55
|
+
)
|
56
|
+
response.write(json)
|
57
|
+
end
|
58
|
+
|
59
|
+
# HTTP API List the active http routes.
|
60
|
+
# @param _request [Rack::Request]
|
61
|
+
# @param response [Rack::Response]
|
62
|
+
# @return [void]
|
63
|
+
def routes_web(_request, response)
|
64
|
+
response.headers['Content-Type'] = 'application/json'
|
65
|
+
json = MultiJson.dump(
|
66
|
+
routes: api_routes
|
67
|
+
)
|
68
|
+
response.write(json)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Get a list of the currently running handlers.
|
72
|
+
# @param _source [Pepito::Source] Source of the message. Not used.
|
73
|
+
# @param _match_data [MatchData] Match Data. Not used.
|
74
|
+
# @return [Array<String>]
|
75
|
+
def handlers_chat(_source, _match_data)
|
76
|
+
@robot.handlers.keys
|
77
|
+
end
|
78
|
+
|
79
|
+
# Get a list of the currently running adapters.
|
80
|
+
# @param _source [Pepito::Source] Source of the message. Not used.
|
81
|
+
# @param _match_data [MatchData] Match Data. Not used.
|
82
|
+
# @return [Array<String>]
|
83
|
+
def adapters_chat(_source, _match_data)
|
84
|
+
@robot.adapters.keys
|
85
|
+
end
|
86
|
+
|
87
|
+
# Get a list of the currently active http routes.
|
88
|
+
# @param _source [Pepito::Source] Source of the message. Not used.
|
89
|
+
# @param _match_data [MatchData] Match Data. Not used.
|
90
|
+
# @return [Array<String>]
|
91
|
+
def routes_chat(_source, _match_data)
|
92
|
+
api_routes
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
# Get the http routes in the form of an array of strings.
|
98
|
+
# @return [Array<String>]
|
99
|
+
def api_routes
|
100
|
+
routes = []
|
101
|
+
@robot.http_api.router.routes.each do |route|
|
102
|
+
routes << route.path_for_generation
|
103
|
+
end
|
104
|
+
routes
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative '../handler'
|
2
|
+
|
3
|
+
module Pepito
|
4
|
+
module Handlers
|
5
|
+
# Handler to give an help command.
|
6
|
+
class Help < Handler
|
7
|
+
# Run the handler.
|
8
|
+
# @return [void]
|
9
|
+
def run
|
10
|
+
chat_route(/^help$/i, :chat_help, command: true, help: 'help -> show this screen')
|
11
|
+
end
|
12
|
+
|
13
|
+
# Return the help strings.
|
14
|
+
# @param _source [Pepito::Source] Source of the message. Not used.
|
15
|
+
# @param _match_data [MatchData] Match Data. Not used.
|
16
|
+
# @return [Array<String>]
|
17
|
+
def chat_help(_source, _match_data)
|
18
|
+
strings = []
|
19
|
+
@robot.handlers.each do |_, handler|
|
20
|
+
handler.chat_routes.each do |route|
|
21
|
+
next if route.help.nil?
|
22
|
+
strings << help_string(route)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
strings
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def help_string(route)
|
31
|
+
return @robot.name + ' ' + route.help if route.command
|
32
|
+
route.help
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
|
3
|
+
require_relative '../version'
|
4
|
+
require_relative '../handler'
|
5
|
+
|
6
|
+
module Pepito
|
7
|
+
module Handlers
|
8
|
+
# Handler to give the bot's info.
|
9
|
+
class Info < Handler
|
10
|
+
# Run the handler.
|
11
|
+
# @return [void]
|
12
|
+
def run
|
13
|
+
http_route('GET', '/info', :web)
|
14
|
+
chat_route(/^info$/i, :chat, command: true, help: "info -> show bot's info")
|
15
|
+
end
|
16
|
+
|
17
|
+
# Info for the http api.
|
18
|
+
# @param _request [Rack::Request]
|
19
|
+
# @param response [Rack::Response]
|
20
|
+
# @return [void]
|
21
|
+
def web(_request, response)
|
22
|
+
response.headers['Content-Type'] = 'application/json'
|
23
|
+
json = MultiJson.dump(
|
24
|
+
pepito_version: Pepito::VERSION
|
25
|
+
)
|
26
|
+
response.write(json)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Info for the chat
|
30
|
+
# @param _source [Pepito::Source] Source of the message. Not used.
|
31
|
+
# @param _match_data [MatchData] Match Data. Not used.
|
32
|
+
# @return [Array<String>]
|
33
|
+
def chat(_source, _match_data)
|
34
|
+
strings = []
|
35
|
+
strings << 'Pepito Version: ' + Pepito::VERSION
|
36
|
+
strings
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative '../handler'
|
2
|
+
|
3
|
+
module Pepito
|
4
|
+
module Handlers
|
5
|
+
# Handler to manage rooms options
|
6
|
+
class Room < Handler
|
7
|
+
# Run the handler.
|
8
|
+
# @return [void]
|
9
|
+
def run
|
10
|
+
chat_route(/^set topic (?<topic>.*)$/i, :chat_set_topic, command: true, help: "set topic [topic] -> change the channel's topic")
|
11
|
+
end
|
12
|
+
|
13
|
+
# Set topic
|
14
|
+
# @param source [Pepito::Source] Where the message came from
|
15
|
+
# @param match_data [MatchData] Match data for the regexp pattern.
|
16
|
+
# @return [nil]
|
17
|
+
def chat_set_topic(source, match_data)
|
18
|
+
source.adapter.topic(source, match_data['topic'])
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
3
|
+
module Pepito
|
4
|
+
module HTTPApi
|
5
|
+
# Class to help callback functions for the http routes
|
6
|
+
class HTTPCallback
|
7
|
+
# Currently running robot
|
8
|
+
# @return [Pepito::Robot]
|
9
|
+
attr_reader :robot
|
10
|
+
|
11
|
+
# The class to call the function
|
12
|
+
# @return [Pepito::Handler]
|
13
|
+
attr_reader :klass
|
14
|
+
|
15
|
+
# The function to call
|
16
|
+
# @return [Symbol]
|
17
|
+
attr_reader :func
|
18
|
+
|
19
|
+
# @param robot [Pepito::Robot] The currently running robot.
|
20
|
+
# @param klass [Pepito::Handler] The class where to call the function.
|
21
|
+
# @param func [Symbol] The function to call.
|
22
|
+
# @return [void]
|
23
|
+
def initialize(robot, klass, func)
|
24
|
+
@robot = robot
|
25
|
+
@klass = klass
|
26
|
+
@func = func
|
27
|
+
end
|
28
|
+
|
29
|
+
# Call method
|
30
|
+
# @param env [Object] The environment of the request
|
31
|
+
# @return [void]
|
32
|
+
def call(env)
|
33
|
+
request = Rack::Request.new(env)
|
34
|
+
response = Rack::Response.new
|
35
|
+
|
36
|
+
if request.head?
|
37
|
+
response.status = 204
|
38
|
+
else
|
39
|
+
begin
|
40
|
+
@klass.public_send(@func, request, response)
|
41
|
+
response.finish
|
42
|
+
rescue => e
|
43
|
+
puts e
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'http_router'
|
2
|
+
|
3
|
+
require_relative 'http_callback'
|
4
|
+
|
5
|
+
module Pepito
|
6
|
+
module HTTPApi
|
7
|
+
# The http route object.
|
8
|
+
class HTTPRoute
|
9
|
+
# Class to extends the HttpRouter route
|
10
|
+
ExtendedRoute = Class.new(HttpRouter::Route) do
|
11
|
+
include HttpRouter::RouteHelper
|
12
|
+
include HttpRouter::GenerationHelper
|
13
|
+
end
|
14
|
+
|
15
|
+
# The route object.
|
16
|
+
# @return [ExtendedRoute]
|
17
|
+
attr_reader :route
|
18
|
+
|
19
|
+
# @param robot [Pepito::Robot] The currently running robot.
|
20
|
+
# @param klass [Pepito::Handler] The class to call the function from.
|
21
|
+
# @param method [String] The http method (Example: "GET").
|
22
|
+
# @param path [String] The path for the route.
|
23
|
+
# @param func [Symbol] The function to call.
|
24
|
+
# @return [void]
|
25
|
+
def initialize(robot, klass, method, path, func)
|
26
|
+
@route = setup_route(robot, klass, method, path, func)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Setup the extendend route
|
32
|
+
# @param robot [Pepito::Robot] The currently running robot.
|
33
|
+
# @param klass [Pepito::Handler] The class to call the function from.
|
34
|
+
# @param method [String] The http method (Example: "GET").
|
35
|
+
# @param path [String] The path for the route.
|
36
|
+
# @param func [Symbol] The function to call.
|
37
|
+
# @return [ExtendedRoute]
|
38
|
+
def setup_route(robot, klass, method, path, func)
|
39
|
+
route = ExtendedRoute.new
|
40
|
+
route.path = path
|
41
|
+
route.add_request_method(method)
|
42
|
+
route.add_request_method('HEAD') if method == 'GET'
|
43
|
+
route.to(HTTPCallback.new(robot, klass, func))
|
44
|
+
route
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|