lita-custom-meme 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 517ac2f590506ec8533b3d19a246d9c1c6e5cbbf
4
- data.tar.gz: 739e1a1d9869d75c0826664000db4d7da73907d6
3
+ metadata.gz: c65fcd6160131eb838b2c35c01d4541ad8f15130
4
+ data.tar.gz: 39fa06a70c7c3149b847fab06545d5dd8e35b2a0
5
5
  SHA512:
6
- metadata.gz: 261dbfad4f20c2b74cf4f193473fad965da4e060af2a526b87a4fbbec1b48511db5ca6abefca1fa20e3ea626e255188d393c7c94017778357fe2e72734f91b7a
7
- data.tar.gz: 92835f5a8a04c03d66c7262b89212574b80cdf361d0a02e2e8852b1047c59e3c5c4dbf46078f550b5fdce8d9543e1814ad076fb9e8a7c8e4245aac5ebc1640f9
6
+ metadata.gz: 3ae3be5f3b4dc53ec4ad2470d40c47446c76b0f61d23510588ae55319f16b87984144f632af8898325408f28693066136106e933d527549308385a98476de984
7
+ data.tar.gz: 0e7218789cfaa9fcdd190c1d3b0a59c0e5d783c38e4d3a1097ff77ff60bdd85c41d33a71ddc9c72dfd3fcfc194ea6266a4a452d5c6fd40f11976f5d423dc8150
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # lita-custom-meme
2
2
 
3
- This plugin responds with images for a set of user defined memes contained within parenthesis
3
+ [![Gem](http://img.shields.io/gem/v/lita-custom-meme.svg)](http://rubygems.org/gems/lita-custom-meme)
4
+ [![Build Status](https://travis-ci.org/webdestroya/lita-custom-meme.svg)](https://travis-ci.org/webdestroya/lita-custom-meme)
5
+ [![Coverage Status](http://img.shields.io/coveralls/webdestroya/lita-custom-meme.svg)](https://coveralls.io/r/webdestroya/lita-custom-meme)
6
+
7
+ **lita-custom-meme** is a handler for [Lita](http://lita.io/) that responds with images for a user defined set of "memes".
4
8
 
5
9
  ## Installation
6
10
 
@@ -15,17 +19,56 @@ gem "lita-custom-meme"
15
19
 
16
20
  ``` ruby
17
21
  Lita.configure do |config|
18
- config.handlers.custom_meme.memes = [
19
- 'something' => "http://image"
20
- ]
22
+ # Set to true if you wish to restrict the custom memes
23
+ # to only be given when a direct command is given to Lita
24
+ config.handlers.custom_meme.command_only = false
21
25
  end
22
26
  ```
23
27
 
24
28
  ## Usage
25
29
 
26
- * `blah blah (something)`
27
- * `http://image`
30
+ Any meme found in general room chat wrapped in parenthesis will cause Lita to respond with the URL to the corresponding image.
31
+
32
+ ```text
33
+ You: blah blah (something)
34
+ Lita: http://image
35
+ ```
36
+
37
+ *Note:* If you do not wish to have this functionality, then you can set the `command_only` configuration option to `true`.
38
+
39
+ ### Command Usage
40
+
41
+ To view the image associated with a specific meme:
42
+
43
+ ```text
44
+ Lita: meme show NAME
45
+ > http://image/xxxxx.jpg
46
+ ```
47
+
48
+ To get a list of all available memes:
49
+
50
+ ```text
51
+ Lita: meme list
52
+ > Available memes: troll, rage, tableflip
53
+ ```
54
+
55
+ ## Admin Commands
56
+
57
+ To add or update a meme:
58
+
59
+ ```text
60
+ Lita: meme add NAME IMAGE
61
+ ```
62
+
63
+ To delete a meme from the list:
64
+
65
+ ```text
66
+ Lita: meme delete NAME
67
+ ```
68
+
69
+ *Note:* The above admin commands require that the user be a member of the `:custom_meme_admins` authorization group.
70
+
28
71
 
29
72
  ## License
30
73
 
31
- [MIT](http://opensource.org/licenses/MIT)
74
+ [MIT](http://opensource.org/licenses/MIT)
@@ -4,46 +4,98 @@ require 'lita'
4
4
  module Lita
5
5
  module Handlers
6
6
  class CustomMeme < Handler
7
- on :loaded, :define_routes
7
+ REDIS_KEY = "cmeme"
8
8
 
9
- route %r{^meme (.+)$}i, :meme_image, command: true, help: {'meme NAME' => "Displays the image for the specified meme."}
9
+ # The global handler
10
+ route %r{\(([\w\._]+)\)}i, :meme_image, command: false
10
11
 
11
- def self.default_config(config)
12
- config.memes = {}
13
- config.command_only = false
14
- end
12
+ route %r{^meme show ([\w\._]+)$}i, :meme_image,
13
+ command: true,
14
+ help: {'meme show NAME' => "Displays the image for the specified meme."}
15
15
 
16
- def define_routes(payload)
17
- return if Lita.config.handlers.custom_meme.memes.empty?
16
+ route %r{^meme list$}i, :meme_list,
17
+ command: true,
18
+ help: {'meme list' => "Displays a list of available memes."}
18
19
 
19
- memelist = Lita.config.handlers.custom_meme.memes.keys.join("|")
20
+ # Admin commands
21
+ route %r{^meme add ([\w\._]+) (http.+)$}i, :meme_add,
22
+ command: true,
23
+ restrict_to: :custom_meme_admins,
24
+ help: {'meme add NAME IMAGE' => "Adds a meme to the list"}
20
25
 
21
- # This picks up the images in chat
22
- unless Lita.config.handlers.custom_meme.command_only
23
- self.class.route(
24
- %r{\((#{memelist})\)}i,
25
- :meme_image
26
- )
27
- end
26
+ route %r{^meme delete ([\w\._]+)$}i, :meme_delete,
27
+ command: true,
28
+ restrict_to: :custom_meme_admins,
29
+ help: {'meme delete NAME' => "Deletes a meme from the list"}
30
+
31
+
32
+ def self.default_config(config)
33
+ config.command_only = false
28
34
  end
29
35
 
30
36
  def meme_image(response)
37
+ # If command only is desired, then bail if general message
38
+ return if config.command_only && !response.message.command?
39
+
31
40
  output = []
32
41
 
33
42
  response.matches.each do |match|
34
- term = match[0].downcase
43
+ term = normalize_key(match[0])
35
44
 
36
- if Lita.config.handlers.custom_meme.memes[term]
37
- output << Lita.config.handlers.custom_meme.memes[term]
45
+ image = redis.hget(REDIS_KEY, term)
46
+
47
+ if image
48
+ output << image
38
49
  end
39
50
 
40
51
  end
41
52
 
42
53
  if output.size > 0
43
54
  response.reply *output
55
+ elsif response.message.command?
56
+ response.reply "Meme not found"
44
57
  end
45
58
  end
46
59
 
60
+ def meme_add(response)
61
+ name, image = response.matches.first
62
+
63
+ name = normalize_key(name)
64
+ redis.hset(REDIS_KEY, name, image)
65
+
66
+ response.reply "Meme '#{name}' has been added."
67
+ end
68
+
69
+ def meme_delete(response)
70
+ key = normalize_key(response.matches.first.first)
71
+
72
+ if redis.hdel(REDIS_KEY, key) >= 1
73
+ response.reply "Deleted meme '#{key}'."
74
+ else
75
+ response.reply "Meme '#{key}' was not found."
76
+ end
77
+ end
78
+
79
+ def meme_list(response)
80
+ keys = redis.hkeys(REDIS_KEY)
81
+
82
+ if keys.empty?
83
+ response.reply "No memes have been added"
84
+ else
85
+ response.reply "Available memes: #{keys.sort.join(', ')}"
86
+ end
87
+ end
88
+
89
+ private
90
+
91
+ def config
92
+ Lita.config.handlers.custom_meme
93
+ end
94
+
95
+ def normalize_key(key)
96
+ key.to_s.downcase.strip
97
+ end
98
+
47
99
  end
48
100
 
49
101
  Lita.register_handler(CustomMeme)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-custom-meme"
3
- spec.version = "0.0.1"
3
+ spec.version = "0.1.0"
4
4
  spec.authors = ["Mitch Dempsey"]
5
5
  spec.email = ["mrdempsey@gmail.com"]
6
6
  spec.description = %q{A Lita handler that lets the user specify custom meme images}
@@ -1,34 +1,74 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Lita::Handlers::CustomMeme, lita_handler: true do
4
- let(:payload) { double("payload") }
5
4
  before do
6
- Lita.config.handlers.custom_meme.memes = {
7
- 'something' => 'blah',
8
- 'rarara' => "nathan"
9
- }
10
- described_class.routes.clear
11
- subject.define_routes(payload)
5
+ allow(Lita::Authorization).to receive(:user_in_group?).and_return(true)
12
6
  end
13
7
 
14
- it { routes("blah blah (something) blah").to(:meme_image) }
8
+ it { routes_command("meme show something").to(:meme_image) }
9
+
10
+ it { routes_command("meme list").to(:meme_list) }
11
+
12
+ it { routes_command("meme add name http://image.com/img.jpg").to(:meme_add) }
13
+ it { routes_command("meme delete name").to(:meme_delete) }
14
+
15
+ it 'sets and shows memes' do
16
+ send_command("meme add testing http://blah.com")
17
+ expect(replies.last).to eq("Meme 'testing' has been added.")
18
+
19
+ send_command("meme show testing")
20
+ expect(replies.last).to eq("http://blah.com")
21
+ end
22
+
23
+ describe '#meme_delete' do
24
+ it 'indicates meme not found' do
25
+ send_command("meme delete deltest")
26
+ expect(replies.last).to eq("Meme 'deltest' was not found.")
27
+ end
28
+
29
+ it 'deletes meme' do
30
+ send_command("meme add deltest http://blah.com")
31
+ send_command("meme delete deltest")
32
+ send_command("meme show deltest")
33
+ expect(replies.last).to eq("Meme not found")
34
+ end
35
+ end
36
+
37
+ describe '#meme_list' do
38
+ it 'indicates there are no memes' do
39
+ send_command("meme list")
40
+ expect(replies.last).to eq("No memes have been added")
41
+ end
42
+
43
+ it 'lists available memes' do
44
+ send_command("meme add test1 http://blah")
45
+ send_command("meme add test2 http://blah")
46
+
47
+ send_command("meme list")
48
+ expect(replies.last).to eq("Available memes: test1, test2")
49
+ end
50
+ end
15
51
 
16
52
  describe "#meme_image" do
17
- it "responds with single image" do
18
- send_message("blah blah (rarara)")
19
- expect(replies.last).to eq("nathan")
53
+
54
+ it 'responds to show command when not found' do
55
+ send_command("meme show fake")
56
+ expect(replies.last).to eq("Meme not found")
20
57
  end
21
58
 
22
- it "responds with multiple images" do
23
- send_message("blah blah (rarara) and (something)")
24
- expect(replies.size).to eq(2)
25
- expect(replies).to include("nathan")
26
- expect(replies).to include("blah")
59
+ it 'does not respond to show in general chat' do
60
+ send_message("(fake)")
61
+ expect(replies.last).to be_nil
27
62
  end
28
63
 
29
- it "does not respond when no meme" do
30
- send_message("some random text")
31
- expect(replies.size).to eq(0)
64
+ it "responds with multiple images" do
65
+ send_command("meme add rarara http://nathan.com")
66
+ send_command("meme add rage http://mitch.com")
67
+ replies.clear
68
+ send_message("blah blah (rarara) and (rage)")
69
+ expect(replies.size).to eq(2)
70
+ expect(replies).to include("http://nathan.com")
71
+ expect(replies).to include("http://mitch.com")
32
72
  end
33
73
 
34
74
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-custom-meme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitch Dempsey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-25 00:00:00.000000000 Z
11
+ date: 2014-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  version: '0'
135
135
  requirements: []
136
136
  rubyforge_project:
137
- rubygems_version: 2.0.14
137
+ rubygems_version: 2.2.2
138
138
  signing_key:
139
139
  specification_version: 4
140
140
  summary: A Lita handler that lets the user specify custom meme images