cksh_commander 0.1.0 → 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 +30 -8
- data/lib/cksh_commander/command.rb +36 -2
- data/lib/cksh_commander/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc237603bb3d1579fcf9ae3700e17207a2e2f901
|
4
|
+
data.tar.gz: 7a77d47f54af6d498763704a43c8f53a304e0c65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72540f0185bff479bed0aa697be2c02ab9b445e0dd948ac5edf23f9cfb00daec314eab88f79a1dfc2c1694dcc309f7095c677a3066c7cd98088cd77c7d10657e
|
7
|
+
data.tar.gz: a6848cad5216d2afacec0feeb90dd6e0952d56d54920c11126e773d1751d4ef0ec4c8af1081dbba8c338ec698d1d0ee9dfb6b50246580f4de65e9d0486a2d427
|
data/README.md
CHANGED
@@ -5,17 +5,26 @@
|
|
5
5
|
CKSHCommander is a Ruby gem that simplifies the task of processing [Slack slash
|
6
6
|
commands](https://api.slack.com/slash-commands). It provides a class-based
|
7
7
|
convention for authoring command handlers, and it supports basic subcommands and
|
8
|
-
arguments.
|
8
|
+
arguments. Check out the
|
9
|
+
[cksh_command_api](https://github.com/openarcllc/cksh_commander_api) repository to see a dead simple Sinatra API
|
10
|
+
that comes bootstrapped with everything you need to process Slack slash
|
11
|
+
commands using this gem.
|
9
12
|
|
10
|
-
|
13
|
+
### Installation
|
11
14
|
|
12
|
-
You can install `cksh_commander` via RubyGems.
|
15
|
+
You can install `cksh_commander` via RubyGems. In your Gemfile:
|
13
16
|
|
14
17
|
```ruby
|
15
18
|
gem 'cksh_commander'
|
16
19
|
```
|
17
20
|
|
18
|
-
|
21
|
+
Or from a command line:
|
22
|
+
|
23
|
+
```
|
24
|
+
gem install cksh_commander
|
25
|
+
```
|
26
|
+
|
27
|
+
### Usage
|
19
28
|
|
20
29
|
Defining a custom command is easy. First, create a `commands/` directory and tell
|
21
30
|
`CKSHCommander` where it can find your commands. Let's say we've created a `commands/`
|
@@ -62,18 +71,21 @@ module Example
|
|
62
71
|
|
63
72
|
# Subcommand with no arguments
|
64
73
|
# SLACK: /example test0
|
74
|
+
desc "test0", "Subcommand 0 description."
|
65
75
|
def test0
|
66
76
|
set_response_text("Subcommand: test0")
|
67
77
|
end
|
68
78
|
|
69
79
|
# Subcommand with one argument
|
70
80
|
# SLACK: /example test1 text
|
81
|
+
desc "test1 [TEXT]", "Subcommand 1 description."
|
71
82
|
def test1(text)
|
72
83
|
set_response_text("Subcommand: test1; Text: #{text}")
|
73
84
|
end
|
74
85
|
|
75
86
|
# No subcommand with one argument
|
76
87
|
# SLACK: /example text
|
88
|
+
desc "[TEXT]", "Root command description."
|
77
89
|
def ___(text)
|
78
90
|
set_response_text("Root command; Text: #{text}")
|
79
91
|
end
|
@@ -84,8 +96,18 @@ end
|
|
84
96
|
We set our slash command authentication token at the class level, and define
|
85
97
|
methods for processing subcommands. Attachments (which take the form of a hash)
|
86
98
|
can be added using `add_response_attachment(attachment)`. You can also set the
|
87
|
-
response to `'in_channel'` at the method level with `respond_in_channel!`.
|
88
|
-
|
99
|
+
response to `'in_channel'` at the method level with `respond_in_channel!`.
|
100
|
+
|
101
|
+
Similar to [Thor](http://whatisthor.com/), we are able to document our
|
102
|
+
command's API with the class-level `desc` method—as is shown in the example
|
103
|
+
above. CKSHCommander provides a `help` subcommand out of the box, and this will
|
104
|
+
echo the documentation back to the Slack user.
|
105
|
+
|
106
|
+
```
|
107
|
+
/example test0 # Subcommand 0 description.
|
108
|
+
/example test1 [TEXT] # Subcommand 1 description.
|
109
|
+
/example [TEXT] # Root command description.
|
110
|
+
```
|
89
111
|
|
90
112
|
To run a command, we use `CKSHCommander::Runner`. In the example below, we've
|
91
113
|
created a simple Sinatra app to illustrate its usage with the standard Slack
|
@@ -111,12 +133,12 @@ post "/" do
|
|
111
133
|
end
|
112
134
|
```
|
113
135
|
|
114
|
-
|
136
|
+
### Development
|
115
137
|
|
116
138
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
117
139
|
|
118
140
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
119
141
|
|
120
|
-
|
142
|
+
### Contributing
|
121
143
|
|
122
144
|
Bug reports and pull requests are welcome on GitHub at https://github.com/openarcllc/cksh_commander.
|
@@ -4,12 +4,20 @@ module CKSHCommander
|
|
4
4
|
class Command
|
5
5
|
class << self
|
6
6
|
attr_accessor :token
|
7
|
+
attr_reader :docs
|
7
8
|
|
8
9
|
def set(opts)
|
9
10
|
opts.each do |k,v|
|
10
|
-
send("#{k}=", v) if respond_to?(k)
|
11
|
+
send("#{k}=", v) if respond_to?("#{k}=")
|
11
12
|
end
|
12
13
|
end
|
14
|
+
|
15
|
+
# Define command usage/description documentation
|
16
|
+
# for output in 'help' command.
|
17
|
+
def desc(usage, description)
|
18
|
+
@docs ||= []
|
19
|
+
@docs << { usage: usage, desc: description }
|
20
|
+
end
|
13
21
|
end
|
14
22
|
|
15
23
|
attr_accessor :data
|
@@ -62,6 +70,14 @@ module CKSHCommander
|
|
62
70
|
@response
|
63
71
|
end
|
64
72
|
|
73
|
+
def help
|
74
|
+
if self.class.docs.any?
|
75
|
+
set_response_text(help_output)
|
76
|
+
else
|
77
|
+
set_response_text("Add some documentation!")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
65
81
|
def authenticated?
|
66
82
|
self.class.token && self.class.token == data.token
|
67
83
|
end
|
@@ -85,7 +101,25 @@ module CKSHCommander
|
|
85
101
|
private
|
86
102
|
|
87
103
|
def subcommand_methods
|
88
|
-
methods - CKSHCommander::Command.new.methods
|
104
|
+
custom = (methods - CKSHCommander::Command.new.methods)
|
105
|
+
custom.concat([:help])
|
106
|
+
end
|
107
|
+
|
108
|
+
def help_output
|
109
|
+
docs = self.class.docs
|
110
|
+
output = "```\n"
|
111
|
+
|
112
|
+
# Ensure that descriptions are justified properly.
|
113
|
+
just = docs.max_by { |d|
|
114
|
+
d[:usage].size
|
115
|
+
}[:usage].size + @data.command.size + 3
|
116
|
+
|
117
|
+
# Add output for each documented command.
|
118
|
+
docs.each do |d|
|
119
|
+
output += "#{@data.command} #{d[:usage]}".ljust(just) + "# #{d[:desc]}\n"
|
120
|
+
end
|
121
|
+
|
122
|
+
output += "```"
|
89
123
|
end
|
90
124
|
end
|
91
125
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cksh_commander
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Loncar
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|