lita-alias 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8c6cff61317785a819e6de3a05e32dc81b8acadf
4
+ data.tar.gz: f14b459f19d00a13fe52a0d6e7c9d5ac7d631a93
5
+ SHA512:
6
+ metadata.gz: 93bcfd2996ee35f2100537b62464c7ffb743c6bc97b3e5df49dcd29adb8d66e2592cc30ce368f48c8e407b0b352ad0925a1ef2eba46fcfa79cf871ed20107a5b
7
+ data.tar.gz: 2d15a6b4eb3ec000959e6f324eac4d5616c0527ef9cda744a80bb708720281fd0c114dcecad77a52c1b5d8603c3c8fa1371f03469b6696c81520fd4111ce204c
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor
@@ -0,0 +1,2 @@
1
+ Metrics/LineLength:
2
+ Max: 120
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ script: bundle exec rake
5
+ before_install:
6
+ - gem update --system
7
+ services:
8
+ - redis-server
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Alex Soto
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,62 @@
1
+ # lita-alias
2
+
3
+ [![Build Status](https://travis-ci.org/apsoto/lita-alias.png?branch=master)](https://travis-ci.org/apsoto/lita-alias)
4
+ [![Coverage Status](https://coveralls.io/repos/apsoto/lita-alias/badge.png)](https://coveralls.io/r/apsoto/lita-alias)
5
+
6
+ If you utilize some plugins that have long complicated commands because they accept arguments and you would like to alias into shorter commands, this plugin can help you.
7
+
8
+ For example, if you have a command to run a process on a heroku app to check stats that you like to run periodically:
9
+
10
+ /hk run my-company-production ./bin/check_stats
11
+
12
+ That's a bit verbose for, so you could alias as:
13
+
14
+ /alias add stats_prod hk run my-company-production ./bin/refresh_cache
15
+
16
+ Then you simply have to type:
17
+
18
+ /stats_prod
19
+
20
+ See [Usage](#usage) for more details.
21
+
22
+ ## Installation
23
+
24
+ Add lita-alias to your Lita instance's Gemfile:
25
+
26
+ ``` ruby
27
+ gem "lita-alias"
28
+ ```
29
+
30
+ ## Requirements
31
+ The plugin requires access to redis for storing aliases
32
+
33
+ ## Configuration
34
+
35
+ none yet
36
+
37
+ ## Usage
38
+
39
+ ### add
40
+
41
+ /alias add ALIAS COMMAND
42
+
43
+ Adds an alias so when you type the command `/ALIAS` the COMMAND is sent instead as if you had typed it yourself. Do not put the robot alias character ('/' for example) in the COMMAND argument. When the ALIAS is triggered, the chatbot will receive the COMMAND as a chat message with your robot's alias character prepended.
44
+
45
+ Example: `/alias add stats_prod hk run my-company-production ./bin/refresh_cache`
46
+
47
+ ### delete
48
+
49
+ /alias delete ALIAS
50
+
51
+ Deletes the ALIAS.
52
+
53
+ Example: `/alias delete stats_prod`
54
+
55
+ ### list
56
+
57
+ /alias list
58
+
59
+ Shows all the aliases that have been saved.
60
+
61
+ ## Notes
62
+ Some COMMANDS maybe triggered when you alias them if the handler's route is very loose with where it may appear in a chat message.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ # rubocop:disable Style/FileName
2
+ require 'lita'
3
+
4
+ Lita.load_locales Dir[File.expand_path(
5
+ File.join('..', '..', 'locales', '*.yml'),
6
+ __FILE__
7
+ )]
8
+
9
+ require 'lita/alias/chat_handler'
10
+
11
+ Lita::Alias::ChatHandler.template_root File.expand_path(
12
+ File.join('..', '..', 'templates'),
13
+ __FILE__
14
+ )
@@ -0,0 +1,53 @@
1
+ module Lita
2
+ module Alias
3
+ # Repository of aliases.
4
+ # Stores the aliases in a hash in redis keyed by the alias name
5
+ class AliasStore
6
+ STORE_KEY = 'aliases'
7
+ attr_reader :redis
8
+
9
+ def initialize(redis)
10
+ @redis = redis
11
+ end
12
+
13
+ # Delete the alias with *name*
14
+ def delete(name)
15
+ fail AliasNotFoundException, "No alias with name #{name} exists" unless @redis.hexists(STORE_KEY, name)
16
+
17
+ command = @redis.hget(STORE_KEY, name)
18
+ @redis.hdel(STORE_KEY, name)
19
+ AliasedCommand.new(name, command).freeze
20
+ end
21
+
22
+ # Store the alias.
23
+ # @raises ArgumentError if the alias is not valid
24
+ # @return AliasedCommand The saved alias
25
+ def add(aliased_command)
26
+ fail AliasAddException, "The alias is not valid" unless aliased_command.valid?
27
+
28
+ @redis.hset(STORE_KEY, aliased_command.name, aliased_command.command)
29
+ end
30
+
31
+ # Return a list of all aliases
32
+ # @return Array<AliasedCommand>
33
+ def list
34
+ @redis.hgetall(STORE_KEY).sort.map do |name, command|
35
+ AliasedCommand.new(name, command)
36
+ end
37
+ end
38
+
39
+ def lookup(name)
40
+ command = @redis.hget(STORE_KEY, name)
41
+ AliasedCommand.new(name, command)
42
+ end
43
+
44
+ def exists?(name)
45
+ @redis.hexists(STORE_KEY, name)
46
+ end
47
+
48
+ # Exceptions
49
+ class AliasNotFoundException < StandardError; end
50
+ class AliasAddException < StandardError; end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,20 @@
1
+ module Lita
2
+ module Alias
3
+ # A single aliased command
4
+ class AliasedCommand
5
+ attr_accessor :name, :command
6
+
7
+ def initialize(name = nil, command = nil)
8
+ @name = name
9
+ @command = command
10
+ end
11
+
12
+ # Returns true if the object is valid. False otherwise
13
+ # @return Boolean
14
+ def valid?
15
+ # for now, just validate the name and command are not nil
16
+ @name && @command
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,128 @@
1
+ require_relative './aliased_command'
2
+ require_relative './alias_store'
3
+
4
+ # main Lita module
5
+ module Lita
6
+ # namespace for plugin handlers
7
+ module Alias
8
+ # Chat handler for lita-alias
9
+ class ChatHandler < Handler
10
+ #########
11
+ # Events
12
+ on :loaded, :load_aliases
13
+
14
+ #########
15
+ # Routes
16
+ route(/alias\s+add\s+(\w+)\s+(.+)/,
17
+ :add,
18
+ command: true,
19
+ help: { 'alias add NAME COMMAND' => 'Alias for sending COMMAND when NAME typed' }
20
+ )
21
+ route(/alias\s+list/,
22
+ :list,
23
+ command: true,
24
+ help: { 'alias list' => 'List all the saved aliases' }
25
+ )
26
+ route(/alias\s+delete\s+(\w+)/,
27
+ :delete,
28
+ command: true,
29
+ help: { 'alias delete NAME' => 'Delete the alias with NAME' }
30
+ )
31
+ route(/alias\s+test/, :test_alias, command: true)
32
+ route(/alias\s+foo/, :foo, command: true)
33
+
34
+ ##########################
35
+ # Event Handlers
36
+ def load_aliases(payload)
37
+ log.debug("Loading aliases")
38
+ alias_store.list.each do |ac|
39
+ add_alias_route(ac)
40
+ end
41
+ end
42
+
43
+ ##########################
44
+ # Route Handlers
45
+
46
+ def add(response)
47
+ name = response.match_data[1]
48
+ command = response.match_data[2]
49
+
50
+ # TODO: support multiple commands
51
+ ac = AliasedCommand.new(name, command)
52
+ add_alias_route(ac)
53
+ alias_store.add(ac)
54
+
55
+ response.reply "Added alias '#{name}' for '#{command}'"
56
+ rescue AliasStore::AliasAddException => e
57
+ response.reply(e.message)
58
+ end
59
+
60
+ def trigger_alias(response)
61
+ ac = alias_store.lookup(response.match_data[1])
62
+ message = Lita::Message.new(robot, "#{robot.alias}#{ac.command}", response.message.source)
63
+ robot.receive(message)
64
+ end
65
+
66
+ def list(response)
67
+ aliases = alias_store.list
68
+
69
+ if aliases.empty?
70
+ message = 'No aliases have been saved'
71
+ else
72
+ message = aliases.map do |ac|
73
+ "#{ac.name} => #{ac.command}"
74
+ end
75
+ end
76
+
77
+ response.reply message
78
+ end
79
+
80
+ def delete(response)
81
+ name = response.match_data[1]
82
+ ac = alias_store.delete(name)
83
+ delete_alias_route(ac)
84
+ response.reply("Deleted alias '#{ac.name}' => '#{ac.command}'")
85
+ rescue AliasStore::AliasNotFoundException
86
+ response.reply("Alias '#{name}' does not exist")
87
+ end
88
+
89
+ # The repository where aliases are persisted
90
+ # @return AliasStore
91
+ def alias_store
92
+ @alias_store ||= AliasStore.new(redis)
93
+ end
94
+
95
+ def test_alias(response)
96
+ command = redis.hgetall(REDIS_ALIASES_KEY).first.last
97
+ message = Lita::Message.new(robot, command, response.message.source)
98
+ after(5) do
99
+ response.reply "Sending #{command}"
100
+ robot.receive(message)
101
+ end
102
+ end
103
+
104
+ def foo(response)
105
+ response.reply 'foo'
106
+ end
107
+
108
+ private
109
+
110
+ def add_alias_route(aliased_command)
111
+ return if alias_route_exists?(aliased_command)
112
+
113
+ self.class.route(/^(#{aliased_command.name})/, :trigger_alias, command: true)
114
+ log.debug("Added route for alias '#{aliased_command.name}'")
115
+ end
116
+
117
+ def delete_alias_route(aliased_command)
118
+ self.class.routes.delete_if {|route| route.pattern.match(aliased_command.name) }
119
+ end
120
+
121
+ def alias_route_exists?(aliased_command)
122
+ self.class.routes.any? {|route| route.pattern.match(aliased_command.name) }
123
+ end
124
+ end
125
+
126
+ Lita.register_handler(ChatHandler)
127
+ end
128
+ end
@@ -0,0 +1,27 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-alias"
3
+ spec.version = "0.0.1"
4
+ spec.authors = ["Alex Soto"]
5
+ spec.email = ["apsoto@gmail.com"]
6
+ spec.description = "A Lita Chatbot plugin to alias commands."
7
+ spec.summary = "A Lita Chatbot plugin to alias commands."
8
+ spec.homepage = "https://github.com/apsoto/lita-alias"
9
+ spec.license = "MIT"
10
+ spec.metadata = { "lita_plugin_type" => "handler" }
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_runtime_dependency "lita", ">= 4.3"
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "pry-byebug"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rack-test"
23
+ spec.add_development_dependency "rspec", ">= 3.0.0"
24
+ spec.add_development_dependency "simplecov"
25
+ spec.add_development_dependency "coveralls"
26
+ spec.add_development_dependency "rubocop"
27
+ end
@@ -0,0 +1,4 @@
1
+ en:
2
+ lita:
3
+ handlers:
4
+ alias:
@@ -0,0 +1,4 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Alias::ChatHandler, lita_handler: true do
4
+ end
@@ -0,0 +1,14 @@
1
+ require "simplecov"
2
+ require "coveralls"
3
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
+ SimpleCov::Formatter::HTMLFormatter,
5
+ Coveralls::SimpleCov::Formatter
6
+ ]
7
+ SimpleCov.start { add_filter "/spec/" }
8
+
9
+ require "lita-alias"
10
+ require "lita/rspec"
11
+
12
+ # A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
13
+ # was generated with Lita 4, the compatibility mode should be left disabled.
14
+ Lita.version_3_compatibility_mode = false
File without changes
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-alias
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Soto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.3'
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.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: A Lita Chatbot plugin to alias commands.
140
+ email:
141
+ - apsoto@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rubocop.yml"
148
+ - ".travis.yml"
149
+ - Gemfile
150
+ - LICENSE
151
+ - README.md
152
+ - Rakefile
153
+ - lib/lita-alias.rb
154
+ - lib/lita/alias/alias_store.rb
155
+ - lib/lita/alias/aliased_command.rb
156
+ - lib/lita/alias/chat_handler.rb
157
+ - lita-alias.gemspec
158
+ - locales/en.yml
159
+ - spec/lita/alias/chat_handler_spec.rb
160
+ - spec/spec_helper.rb
161
+ - templates/.gitkeep
162
+ homepage: https://github.com/apsoto/lita-alias
163
+ licenses:
164
+ - MIT
165
+ metadata:
166
+ lita_plugin_type: handler
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.4.6
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: A Lita Chatbot plugin to alias commands.
187
+ test_files:
188
+ - spec/lita/alias/chat_handler_spec.rb
189
+ - spec/spec_helper.rb