lita-words-counter 0.0.1 → 0.2.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: ab69e3f4501af117b2737014cc644961183c17e2
4
- data.tar.gz: 4ce8333a99fbe7059e0e4bae5223fd84207e8e89
3
+ metadata.gz: 9656a5acea13ffbcfce6a6415a56cb9147b0e16d
4
+ data.tar.gz: 5e4d4166b95dd7c667e1e7b63343f79f4dfc0edc
5
5
  SHA512:
6
- metadata.gz: 3e67bd4ba8c5920b71568df872e526fab44b2e78a5e5e74dc2e7503ee3d7555e2ee7a56e7eec0c75c19ffea3b91deb1e149b54a0cfb52b75bfb1cbe111bd1f73
7
- data.tar.gz: cc29b68a4744cd8be87925c284216f10e77dff3f5623d39d5ded613fade9925fdbdbc97102a16d048b6bc271508eeba4a23ba2ded5dceb2866f92b3f8a20713b
6
+ metadata.gz: c0b1fa780aada86c16436494b8459bf2025507633954b24375adce3ccb0cd1379e5a4af9d3d362af5db3132c5dab1671871e997e5c3c837eb0b8f658ef035fb4
7
+ data.tar.gz: 87caf1dca2118025b235e0f0564a8079a936d810e1b3e48231b53f442c369afcd8e7bfd3e8d0a8c67709134bb93f759c2806f4b04318b3e5581cb6f686f8f3c1
data/README.md CHANGED
@@ -7,7 +7,7 @@ TODO: Add a description of the plugin.
7
7
  Add lita-words-counter to your Lita instance's Gemfile:
8
8
 
9
9
  ``` ruby
10
- gem "lita-words-counter", github: 'hlcfan/lita-words-counter'
10
+ gem "lita-words-counter"
11
11
  ```
12
12
 
13
13
 
@@ -17,8 +17,12 @@ TODO: Describe any configuration attributes the plugin exposes.
17
17
 
18
18
  ## Usage
19
19
 
20
- TODO: Describe the plugin's features and how to use them.
20
+ #### show how many words alex said
21
+ `lita count alex`
22
+
23
+ #### reset words count
24
+ `lita recount`
21
25
 
22
26
  ## License
23
27
 
24
- [MIT](http://opensource.org/licenses/MIT)
28
+ [MIT](http://opensource.org/licenses/MIT)
@@ -3,29 +3,55 @@ require "lita"
3
3
  module Lita
4
4
  module Handlers
5
5
  class Counter < Handler
6
+ REDIS_SET_KEY = "counts"
7
+
6
8
  route /.*/, :counter
7
- route /\Acount\s?(.*)?\z/i, :count, command: true
9
+ route /\Acount\s+(.*+)\z/i, :count, command: true
8
10
  route /\Arecount\s?(.*)?\z/i, :recount, command: true
11
+ route /^count\s+top\s+(\d+)/, :list_top, command: true
9
12
 
10
13
  def counter response
11
- redis.incr(user_key(response.user.name))
14
+ redis.zincrby REDIS_SET_KEY, 1, user_key(response.user.name)
12
15
  end
13
16
 
14
17
  def count response
15
- response.reply(redis.get(user_key(response.matches[0][0])) || '0')
18
+ username = response.matches[0][0]
19
+ scores = find_scores_by_username username
20
+ response.reply generate_lines *scores
16
21
  end
17
22
 
18
23
  def recount response
19
- keys = redis.keys "user-*"
20
- redis.del keys if keys.any?
21
- response.reply '**recount done!**'
24
+ redis.del REDIS_SET_KEY
25
+ response.reply '*recount done!*'
26
+ end
27
+
28
+ def list_top response
29
+ top_n = response.matches[0][0].to_i - 1
30
+ scores = redis.zrevrange REDIS_SET_KEY, 0, top_n, with_scores: true
31
+ response.reply generate_lines(scores)
22
32
  end
23
33
 
24
34
  private
25
35
 
26
- def user_key user_name
27
- "user-#{user_name}"
36
+ def user_key username
37
+ "user-#{username}".downcase
38
+ end
39
+
40
+ def unuser_key username
41
+ username.gsub('user-', '')
28
42
  end
43
+
44
+ def find_scores_by_username name
45
+ scores = redis.zscan REDIS_SET_KEY, 0, { match: "#{user_key(name)}*" }
46
+ scores[1..-1]
47
+ end
48
+
49
+ def generate_lines scores
50
+ scores.map do |score|
51
+ "#{unuser_key(score[0])} said #{score[1].to_i} lines"
52
+ end.join("\n")
53
+ end
54
+
29
55
  end
30
56
 
31
57
  Lita.register_handler(Counter)
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-words-counter"
3
- spec.version = "0.0.1"
3
+ spec.version = "0.2.0"
4
4
  spec.authors = ["Alex S"]
5
5
  spec.email = ["hlcfan.yan@gmail.com.com"]
6
6
  spec.description = 'count user words'
7
- spec.summary = 'count user words'
7
+ spec.summary = 'user words counter plugin for lita'
8
8
  spec.homepage = "https://github.com/hlcfan/lita-words-counter"
9
9
  spec.license = "MIT"
10
10
  spec.metadata = { "lita_plugin_type" => "handler" }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-words-counter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex S
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-23 00:00:00.000000000 Z
11
+ date: 2015-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -107,6 +107,6 @@ rubyforge_project:
107
107
  rubygems_version: 2.4.5
108
108
  signing_key:
109
109
  specification_version: 4
110
- summary: count user words
110
+ summary: user words counter plugin for lita
111
111
  test_files: []
112
112
  has_rdoc: