cogy 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +20 -0
- data/README.md +140 -52
- data/Rakefile +4 -14
- data/app/controllers/cogy/cogy_controller.rb +26 -5
- data/app/views/cogy/error.text.erb +1 -1
- data/lib/cogy/command.rb +33 -16
- data/lib/cogy/context.rb +52 -0
- data/lib/cogy/engine.rb +2 -8
- data/lib/cogy/invocation.rb +37 -0
- data/lib/cogy/version.rb +1 -1
- data/lib/cogy.rb +111 -20
- data/lib/generators/cogy/config_generator.rb +15 -0
- data/lib/generators/cogy/install_generator.rb +25 -0
- data/lib/generators/cogy/templates/cogy_config.rb +8 -0
- data/lib/generators/cogy/templates/cogy_folder_readme.md +4 -0
- data/lib/generators/cogy/templates/command_file.rb +8 -0
- data/test/dummy/cogy/foo.rb +24 -0
- data/test/dummy/cogy/fully_fledged.rb +14 -0
- data/test/dummy/config/application.rb +3 -4
- data/test/dummy/config/environments/development.rb +4 -4
- data/test/dummy/config/environments/test.rb +1 -1
- data/test/dummy/config/initializers/cogy.rb +10 -0
- data/test/dummy/config/routes.rb +2 -1
- data/test/dummy/log/development.log +84 -0
- data/test/dummy/log/test.log +18184 -0
- data/test/integration/command_test.rb +47 -0
- data/test/integration/helpers_test.rb +19 -0
- data/test/integration/inventory_test.rb +54 -0
- data/test/support/helpers.rb +30 -0
- data/test/test_helper.rb +4 -10
- metadata +71 -13
- data/lib/cogy/handler.rb +0 -14
- data/test/cogy_test.rb +0 -7
- data/test/dummy/app/assets/javascripts/application.js +0 -13
- data/test/dummy/app/assets/stylesheets/application.css +0 -15
- data/test/integration/navigation_test.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef283e5c6e479be874048248d27dcfa5acbc6dea
|
4
|
+
data.tar.gz: e64669954bea500e59b959a9b17071f8ed35ceaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7214c918cbd28eb24aa93e40ad389fdf2e1adff0ec93ba8322563b74ceea081638592f359d07d56676760f4e661a40e54faab26721ce718850bfb9843a563ea
|
7
|
+
data.tar.gz: 36f69dad0e051b6a5501df3c40dec962847922229911781b229b715e7286f1b128584a0f70b3fbe79bf53a771aca982dc2240857f8f451ce8892ab395fbe3b1e
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Skroutz S.A.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,75 +1,144 @@
|
|
1
1
|
# Cogy
|
2
2
|
|
3
|
+
[![Build Status](https://api.travis-ci.org/skroutz/cogy.svg?branch=master)](https://travis-ci.org/skroutz/cogy)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/cogy.svg)](https://badge.fury.io/rb/cogy)
|
5
|
+
[![Inline docs](http://inch-ci.org/github/skroutz/cogy.svg)](http://inch-ci.org/github/skroutz/cogy)
|
6
|
+
|
3
7
|
Cogy integrates [Cog](https://operable.io/) with Rails
|
4
|
-
in a way that
|
8
|
+
in a way that writing & deploying commands from your application is a breeze.
|
9
|
+
|
10
|
+
See the API documentation [here](http://www.rubydoc.info/github/skroutz/cogy).
|
5
11
|
|
6
12
|
## Status
|
7
13
|
|
8
|
-
|
9
|
-
is under heavy development.
|
14
|
+
Cogy is still in public alpha.
|
10
15
|
|
11
|
-
|
12
|
-
|
16
|
+
While we use it in production, it's still under heavy development.
|
17
|
+
This means that there are a few rough edges and some important bits are missing.
|
13
18
|
|
14
|
-
However
|
19
|
+
However we'd love any [feedback, suggestions or ideas](https://github.com/skroutz/cogy/issues/new).
|
15
20
|
|
16
21
|
## Why
|
17
22
|
|
18
|
-
Creating a
|
23
|
+
Creating a ChatOps command that talks with a Rails app typically involves writing
|
19
24
|
a route, maybe a controller, an action and code to handle the command arguments
|
20
25
|
and options.
|
21
26
|
|
22
|
-
This is a tedious and repetitive task and involves
|
27
|
+
This is a tedious and repetitive task and involves a lot of boilerplate
|
23
28
|
code each time someone wants to add a new command.
|
24
29
|
|
25
30
|
Cogy is an opinionated library that provides a way to get rid of all the
|
26
|
-
repetitive work
|
31
|
+
repetitive work.
|
27
32
|
|
28
|
-
|
29
|
-
to a file in your application:
|
33
|
+
Writing a new command and deploying it is as simple as:
|
30
34
|
|
31
35
|
```ruby
|
32
36
|
# in cogy/my_commands.rb
|
33
37
|
|
34
|
-
on "foo", desc: "Echo a foo bar back at you!" do
|
35
|
-
"@#{
|
38
|
+
on "foo", desc: "Echo a foo bar back at you!" do
|
39
|
+
"@#{handle}: foo bar"
|
36
40
|
end
|
37
41
|
```
|
38
42
|
|
39
|
-
...and deploying!
|
43
|
+
...and deploying! After a second or so, the command is ready to be used.
|
40
44
|
|
41
45
|
## How it works
|
42
46
|
|
43
47
|
Cogy is essentially three things:
|
44
48
|
|
45
49
|
1. An opinionated way to write, manage & ship commands: All Cogy commands are
|
46
|
-
defined in your Rails app and end up invoking a single executable within the
|
47
|
-
Relay
|
48
|
-
bundle config, which is also served by your Rails
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
requests to their
|
50
|
+
defined in your Rails app and end up invoking a [single executable](https://github.com/skroutz/cogy-bundle/blob/master/commands/cogy) within the
|
51
|
+
Relay. Cogy also provides bundle versioning and dynamically generates the
|
52
|
+
installable bundle config, which is also served by your Rails application
|
53
|
+
and consumed by the [`cogy:install`](https://github.com/skroutz/cogy-bundle)
|
54
|
+
command that installs the new Cogy-generated bundle when you deploy your
|
55
|
+
application.
|
56
|
+
2. A library that provides the API for defining the commands. This library
|
57
|
+
is integrated in your application via a Rails Engine that routes the incoming
|
58
|
+
requests to their respective handlers. It also creates the `/inventory`
|
55
59
|
endpoint, which serves the installable bundle configuration in YAML and can be
|
56
|
-
consumed directly by the `
|
57
|
-
3.
|
58
|
-
|
60
|
+
consumed directly by the [`cogy:install`](https://github.com/skroutz/cogy-bundle) command.
|
61
|
+
3. A [Cog bundle](https://github.com/skroutz/cogy-bundle) that contains the
|
62
|
+
[executable](https://github.com/skroutz/cogy-bundle/blob/master/commands/cogy)
|
63
|
+
that all the commands end up invoking.
|
64
|
+
It is placed inside the Relays and performs the requests to your application
|
59
65
|
when a user invokes a command in the chat. It then posts the result back
|
60
|
-
to the user.
|
66
|
+
to the user. It also contains the `cogy:install` command for automating
|
67
|
+
the task of installing the new bundle when a command is added/modified.
|
68
|
+
|
69
|
+
Take a look at the relevant [diagrams](diagrams/) for an illustration of how
|
70
|
+
Cogy works.
|
71
|
+
|
72
|
+
## Requirements
|
61
73
|
|
62
|
-
|
74
|
+
* [cogy bundle](https://github.com/skroutz/cogy-bundle)
|
75
|
+
* Ruby 2.1+
|
76
|
+
* Tested with Rails 4.2
|
63
77
|
|
64
|
-
|
78
|
+
## Install
|
79
|
+
|
80
|
+
Add it to your Gemfile:
|
65
81
|
|
66
82
|
```ruby
|
67
83
|
gem "cogy"
|
68
84
|
```
|
69
85
|
|
86
|
+
Then run `bundle install`
|
87
|
+
|
88
|
+
Next, run the generator:
|
89
|
+
|
90
|
+
```shell
|
91
|
+
$ bin/rails g cogy:install
|
92
|
+
```
|
93
|
+
|
94
|
+
This will create a sample command, mount the engine and add a sample
|
95
|
+
configuration initializer in your application.
|
96
|
+
|
97
|
+
## Usage
|
98
|
+
|
99
|
+
Defining a new command:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
# in cogy/commands.rb
|
103
|
+
|
104
|
+
on "foo", desc: "Echo a bar" do
|
105
|
+
"bar"
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
This will print "bar" back to the user who calls `!foo` in Slack, for example.
|
110
|
+
|
111
|
+
Inside the block there are the following pre-defined helpers available:
|
112
|
+
|
113
|
+
* `#args`: an array containing the arguments passed to the command
|
114
|
+
* `#opts`: a hash containing the options passed to the command
|
115
|
+
* `#handle`: the chat handle of the user who called the command
|
116
|
+
* `#env`: a hash containing the Cogy environment, that is, every environment variable
|
117
|
+
starting with 'COGY_' and set in the Relay
|
118
|
+
|
119
|
+
For instructions on defining your own helpers, see [Helpers](#helpers).
|
120
|
+
|
121
|
+
A more complete example:
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
# in cogy/commands.rb
|
125
|
+
on "calc",
|
126
|
+
args: [:a, :b],
|
127
|
+
opts: { op: { type: "string" } },
|
128
|
+
desc: "Performs a calculation between numbers <a> and <b>",
|
129
|
+
examples: "myapp:calc sum 1 2" do
|
130
|
+
op = opts[:op].to_sym
|
131
|
+
result = args.map(&:to_i).inject(&op)
|
132
|
+
"Hello @#{user}, the result is: #{result}"
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
For more examples see the [test commands](https://github.com/skroutz/cogy/tree/master/test/dummy/cogy).
|
137
|
+
|
138
|
+
|
70
139
|
## Configuration
|
71
140
|
|
72
|
-
The options provided are the following:
|
141
|
+
The configuration options provided are the following:
|
73
142
|
|
74
143
|
```ruby
|
75
144
|
# in config/initializers/cogy.rb
|
@@ -86,10 +155,10 @@ Cogy.configure do |config|
|
|
86
155
|
config.bundle_description = "myapp-generated commands from Cogy"
|
87
156
|
|
88
157
|
# Can be either a string or an object that responds to `#call` and returns
|
89
|
-
# a string.
|
158
|
+
# a string.
|
90
159
|
config.bundle_version = "0.0.1"
|
91
160
|
|
92
|
-
#
|
161
|
+
# if you used a callable object, it will be evaluated each time the inventory
|
93
162
|
# is called. This can be useful if you want the version to change dynamically
|
94
163
|
# when it's needed.
|
95
164
|
#
|
@@ -101,7 +170,6 @@ Cogy.configure do |config|
|
|
101
170
|
}
|
102
171
|
|
103
172
|
# The path in the Relay where the cogy command executable is located at.
|
104
|
-
# Must be set explicitly.
|
105
173
|
config.executable_path = "/cogcmd/cogy"
|
106
174
|
|
107
175
|
# Paths in your application where the files that define the commands live in.
|
@@ -114,38 +182,59 @@ end
|
|
114
182
|
|
115
183
|
```
|
116
184
|
|
117
|
-
|
185
|
+
You can use the generator to quickly create a config initializer in your app:
|
186
|
+
|
187
|
+
```shell
|
188
|
+
$ bin/rails g cogy:config
|
189
|
+
```
|
190
|
+
|
191
|
+
### Helpers
|
118
192
|
|
119
|
-
|
193
|
+
It is possible to define helpers that can be used throughout commands. They
|
194
|
+
can be defined during configuration and can accept a variable number of
|
195
|
+
arguments, or no arguments at all.
|
196
|
+
|
197
|
+
Let's define a helper that fetches a `Shops` address of the user who called the
|
198
|
+
command:
|
120
199
|
|
121
200
|
```ruby
|
122
|
-
|
201
|
+
Cogy.configure do |c|
|
202
|
+
c.helper(:shop_address) { Shop.find_by(owner: handle).address }
|
203
|
+
end
|
204
|
+
```
|
123
205
|
|
124
|
-
|
125
|
-
|
206
|
+
*(Note that custom helpers also have access to the default helpers like
|
207
|
+
`handle`, `args` etc.)*
|
208
|
+
|
209
|
+
Then we could have a command like this:
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
on "shop_address", desc: "Returns the user's Shop address" do
|
213
|
+
"@#{handle}: Your shop's address is #{shop_address}"
|
126
214
|
end
|
127
215
|
```
|
128
216
|
|
129
|
-
|
217
|
+
We can also define helpers that accept arguments:
|
130
218
|
|
131
|
-
|
219
|
+
```ruby
|
220
|
+
Cogy.configure do |c|
|
221
|
+
c.helper(:format) { |answer| answer.titleize }
|
222
|
+
end
|
223
|
+
```
|
224
|
+
|
225
|
+
Then in our command we could call it like so:
|
132
226
|
|
133
227
|
```ruby
|
134
|
-
|
135
|
-
|
136
|
-
args: [:a, :b],
|
137
|
-
opts: { op: { type: "string", required: true, short_flag: "o" } },
|
138
|
-
desc: "Performs a calculation between numbers <a> and <b>",
|
139
|
-
examples: "!myapp:calc sum 1 2" do |req_args, req_opts, user|
|
140
|
-
op = req_opts[:op].to_sym
|
141
|
-
result = req_args.map(&:to_i).inject(&op)
|
142
|
-
"Hello @#{user}, the result is: #{result}"
|
228
|
+
on "foo", desc: "Nothing special" do
|
229
|
+
format "hello there, how are you today?"
|
143
230
|
end
|
144
231
|
```
|
145
232
|
|
233
|
+
Rails' URL helpers are also available inside the commands.
|
234
|
+
|
146
235
|
## Error template
|
147
236
|
|
148
|
-
When a command throws an error
|
237
|
+
When a command throws an error the [default error template](https://github.com/skroutz/cogy/blob/master/app/views/cogy/error.text.erb) is rendered, which
|
149
238
|
is the following:
|
150
239
|
|
151
240
|
@<%= @user %>: Command '<%= @cmd %>' returned an error.
|
@@ -154,7 +243,7 @@ is the following:
|
|
154
243
|
<%= @exception.class %>:<%= @exception.message %>
|
155
244
|
```
|
156
245
|
|
157
|
-
|
246
|
+
It can be overriden in the application by creating a view in
|
158
247
|
`app/views/cogy/error.text.erb`.
|
159
248
|
|
160
249
|
## Authors
|
@@ -165,4 +254,3 @@ However it can be overriden in the application by creating a view in
|
|
165
254
|
## License
|
166
255
|
|
167
256
|
Cogy is licensed under MIT. See [LICENSE](LICENSE).
|
168
|
-
|
data/Rakefile
CHANGED
@@ -4,24 +4,10 @@ rescue LoadError
|
|
4
4
|
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
5
|
end
|
6
6
|
|
7
|
-
require 'rdoc/task'
|
8
|
-
|
9
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
-
rdoc.rdoc_dir = 'rdoc'
|
11
|
-
rdoc.title = 'Cogy'
|
12
|
-
rdoc.options << '--line-numbers'
|
13
|
-
rdoc.rdoc_files.include('README.rdoc')
|
14
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
-
end
|
16
|
-
|
17
7
|
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
18
8
|
load 'rails/tasks/engine.rake'
|
19
|
-
|
20
|
-
|
21
9
|
load 'rails/tasks/statistics.rake'
|
22
10
|
|
23
|
-
|
24
|
-
|
25
11
|
Bundler::GemHelper.install_tasks
|
26
12
|
|
27
13
|
require 'rake/testtask'
|
@@ -33,5 +19,9 @@ Rake::TestTask.new(:test) do |t|
|
|
33
19
|
t.verbose = false
|
34
20
|
end
|
35
21
|
|
22
|
+
require 'yard'
|
23
|
+
YARD::Rake::YardocTask.new do |t|
|
24
|
+
t.files = ['app/**/*.rb', 'config/**/*.rb', 'lib/**/*.rb']
|
25
|
+
end
|
36
26
|
|
37
27
|
task default: :test
|
@@ -3,26 +3,47 @@ require_dependency "cogy/application_controller"
|
|
3
3
|
module Cogy
|
4
4
|
class CogyController < ApplicationController
|
5
5
|
# GET <mount_path>/cmd/:cmd/:user
|
6
|
+
#
|
7
|
+
# The command endpoint is the one that the cogy executable (see
|
8
|
+
# https://github.com/skroutz/cogy-bundle hits. It executes the requested
|
9
|
+
# {Command} and responds back the result, which is then printed to the user
|
10
|
+
# by the cogy executable.
|
11
|
+
#
|
12
|
+
# See https://github.com/skroutz/cogy-bundle.
|
6
13
|
def command
|
7
14
|
cmd = params[:cmd]
|
8
|
-
args = request.query_parameters.select { |k,_| k
|
15
|
+
args = request.query_parameters.select { |k,_| k =~ /\Acog_argv_/ }.values
|
9
16
|
opts = request.query_parameters.select { |k,_| k =~ /\Acog_opt_/ }
|
10
17
|
.transform_keys { |k| k.sub("cog_opt_", "") }
|
18
|
+
cogy_env = request.query_parameters.select { |k,_| k =~ /\Acogy_/ }
|
11
19
|
user = params[:user]
|
12
20
|
|
13
21
|
begin
|
14
|
-
|
22
|
+
if (command = Cogy.commands[cmd])
|
23
|
+
context = Context.new(args, opts, user, cogy_env)
|
24
|
+
render text: context.run!(command)
|
25
|
+
else
|
26
|
+
render status: 404, text: "The command '#{cmd}' does not exist."
|
27
|
+
end
|
15
28
|
rescue => e
|
16
29
|
@user = user
|
17
30
|
@cmd = cmd
|
18
31
|
@exception = e
|
19
|
-
|
32
|
+
respond_to do |format|
|
33
|
+
format.any do
|
34
|
+
render "/cogy/error.text.erb", content_type: "text/plain", status: 500
|
35
|
+
end
|
36
|
+
end
|
20
37
|
end
|
21
38
|
end
|
22
39
|
|
23
|
-
# GET
|
40
|
+
# GET <mount_path>/inventory
|
41
|
+
#
|
42
|
+
# The inventory endpoint, is essentially the bundle config in YAML format,
|
43
|
+
# which is installable by Cog. It is typically installed by the
|
44
|
+
# `cogy:install` command (see https://github.com/skroutz/cogy-bundle).
|
24
45
|
def inventory
|
25
|
-
render text: Cogy.bundle_config.to_yaml
|
46
|
+
render text: Cogy.bundle_config.to_yaml, content_type: "application/x-yaml"
|
26
47
|
end
|
27
48
|
end
|
28
49
|
end
|
data/lib/cogy/command.rb
CHANGED
@@ -1,9 +1,26 @@
|
|
1
1
|
module Cogy
|
2
|
+
# {Command} represents a user-defined registered command that can be used
|
3
|
+
# in the chat. It contains the Cog-related stuff (ie. everything that
|
4
|
+
# needs to be in the bundle config) and a block that will run and return
|
5
|
+
# the result (ie. handler).
|
2
6
|
class Command
|
3
|
-
|
7
|
+
# The name of the command. Also used in {Cogy.bundle_config}.
|
8
|
+
#
|
9
|
+
# @return [String]
|
10
|
+
attr :name
|
11
|
+
|
12
|
+
# The code that will run when the command is invoked
|
13
|
+
#
|
14
|
+
# @return [Proc]
|
15
|
+
attr :handler
|
4
16
|
|
5
|
-
|
6
|
-
|
17
|
+
# Attributes related to the bundle config in Cog
|
18
|
+
attr :args, :opts, :desc, :long_desc, :examples, :rules
|
19
|
+
|
20
|
+
# See {Cogy.on}
|
21
|
+
def initialize(name, handler, args: [], opts: {}, desc:, long_desc: nil, examples: nil, rules: nil)
|
22
|
+
@name = name.to_s
|
23
|
+
@handler = handler
|
7
24
|
@args = [args].flatten.map!(&:to_s)
|
8
25
|
@opts = opts.with_indifferent_access
|
9
26
|
@desc = desc
|
@@ -12,31 +29,31 @@ module Cogy
|
|
12
29
|
@rules = rules || ["allow"]
|
13
30
|
end
|
14
31
|
|
15
|
-
|
32
|
+
# Registers a command.
|
33
|
+
#
|
34
|
+
# @raise [StandardError] if a command with the same name is already
|
35
|
+
# registered
|
36
|
+
#
|
37
|
+
# @return [self]
|
38
|
+
def register!
|
16
39
|
if Cogy.commands[name]
|
17
40
|
raise "A command with the name #{name} is already registered"
|
18
41
|
end
|
19
42
|
|
20
|
-
@handler = handler
|
21
|
-
@handler.command = self
|
22
|
-
|
23
43
|
Cogy.commands[name] = self
|
24
44
|
end
|
25
45
|
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
# Suitable for bundle config display
|
46
|
+
# @return [String] the command arguments suitable for conversion to YAML
|
47
|
+
# for displaying in a bundle config.
|
31
48
|
def formatted_args
|
32
49
|
args.map { |a| "<#{a}>" }.join(" ")
|
33
50
|
end
|
34
51
|
|
35
|
-
#
|
36
|
-
#
|
37
|
-
# Get rid of HashWithIndifferentAccess, otherwise the resulting YAML
|
38
|
-
# will contain garbage.
|
52
|
+
# @return [Hash] the command options suitable for conversion to YAML
|
53
|
+
# for displaying in a bundle config
|
39
54
|
def formatted_opts
|
55
|
+
# Convert to Hash in order to get rid of HashWithIndifferentAccess,
|
56
|
+
# otherwise the resulting YAML will contain garbage.
|
40
57
|
opts.to_hash
|
41
58
|
end
|
42
59
|
end
|
data/lib/cogy/context.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
module Cogy
|
2
|
+
# {Context} represents a particular invocation request of a {Command}
|
3
|
+
# performed by a user. It holds state like the command arguments, options etc.
|
4
|
+
# In other words, it provides the context in which a {Command} should be
|
5
|
+
# invoked.
|
6
|
+
#
|
7
|
+
# A {Context} essentially is an HTTP request performed by the `cogy:cogy`
|
8
|
+
# command (https://github.com/skroutz/cogy-bundle) on behalf of the user.
|
9
|
+
# You can think of it as the equivalent of the ActionPack's `Request` class.
|
10
|
+
class Context
|
11
|
+
# @return [Array] The Cog command arguments as passed by the user who
|
12
|
+
# invoked the command.
|
13
|
+
#
|
14
|
+
# @see https://cog-book.operable.io/#_arguments
|
15
|
+
attr :args
|
16
|
+
|
17
|
+
# @return [Hash] The Cog command options as provided by the user who
|
18
|
+
# invoked the command
|
19
|
+
#
|
20
|
+
# @see https://cog-book.operable.io/#_options
|
21
|
+
attr :opts
|
22
|
+
|
23
|
+
# @return [String] The chat handle of the user who invoked the command
|
24
|
+
#
|
25
|
+
# @see https://cog-book.operable.io/#_general_metadata
|
26
|
+
attr :handle
|
27
|
+
|
28
|
+
# @return [Hash] The Cogy environment (ie. all environment variables in
|
29
|
+
# the Relay executable that start with 'COGY_')
|
30
|
+
#
|
31
|
+
# @see https://github.com/skroutz/cogy-bundle/blob/master/commands/cogy
|
32
|
+
attr :env
|
33
|
+
|
34
|
+
def initialize(args, opts, handle, env)
|
35
|
+
@args = args
|
36
|
+
@opts = opts
|
37
|
+
@handle = handle
|
38
|
+
@env = env
|
39
|
+
end
|
40
|
+
|
41
|
+
# Executes a {Command} in the context of {self}.
|
42
|
+
#
|
43
|
+
# @param [Command] cmd the {Command} to be executed
|
44
|
+
#
|
45
|
+
# @return [String] the result of the command. This is what will get printed
|
46
|
+
# back to the user that invoked the command and is effectively the return
|
47
|
+
# value of the command body.
|
48
|
+
def run!(cmd)
|
49
|
+
instance_eval(&cmd.handler)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/cogy/engine.rb
CHANGED
@@ -3,18 +3,12 @@ module Cogy
|
|
3
3
|
isolate_namespace Cogy
|
4
4
|
|
5
5
|
config.after_initialize do
|
6
|
-
if Cogy.bundle_version.nil?
|
7
|
-
raise "You must set the :bundle_version setting"
|
8
|
-
end
|
9
|
-
|
10
|
-
if Cogy.executable_path.nil?
|
11
|
-
raise "You must set the :executable_path setting"
|
12
|
-
end
|
13
|
-
|
14
6
|
Cogy.command_load_paths.each do |path|
|
15
7
|
files = Dir[Rails.root.join(path,"*.rb")]
|
16
8
|
files.each { |f| Cogy.module_eval(File.read(f)) }
|
17
9
|
end
|
10
|
+
|
11
|
+
Context.include(Rails.application.routes.url_helpers)
|
18
12
|
end
|
19
13
|
end
|
20
14
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Cogy
|
2
|
+
# {Context} represents a particular invocation request of a {Command}
|
3
|
+
# performed by a user. It holds state like the command arguments, options etc.
|
4
|
+
# In other words, it provides the context in which a {Command} should be
|
5
|
+
# invoked.
|
6
|
+
#
|
7
|
+
# A {Context} essentially is an HTTP request performed by the `cogy:cogy`
|
8
|
+
# command (https://github.com/skroutz/cogy-bundle) on behalf of the user.
|
9
|
+
# You can think of it as the equivalent of the ActionPack's `Request` class.
|
10
|
+
class Context
|
11
|
+
# The Cog command arguments as provided by the user who invoked the command.
|
12
|
+
#
|
13
|
+
# See https://cog-book.operable.io/#_arguments
|
14
|
+
attr :args
|
15
|
+
|
16
|
+
# The Cog command options as provided by the user who invoked the command
|
17
|
+
#
|
18
|
+
# See https://cog-book.operable.io/#_options
|
19
|
+
attr :opts
|
20
|
+
|
21
|
+
# The chat handle of the user who invoked the command
|
22
|
+
#
|
23
|
+
# See https://cog-book.operable.io/#_general_metadata
|
24
|
+
attr :user
|
25
|
+
|
26
|
+
# The Cogy environment (ie. all environment variables in the Relay
|
27
|
+
# executable that start with 'COGY_')
|
28
|
+
attr :env
|
29
|
+
|
30
|
+
def initialize(args, opts, user, env)
|
31
|
+
@args = args
|
32
|
+
@opts = opts
|
33
|
+
@user = user
|
34
|
+
@env = env
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/cogy/version.rb
CHANGED