lita-regexcellent 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -2
- data/lib/lita/handlers/regexcellent.rb +34 -9
- data/lita-regexcellent.gemspec +3 -3
- data/spec/lita/handlers/regexcellent_spec.rb +65 -0
- metadata +6 -6
- data/spec/lita/handlers/count_spec.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78dae87531be14359f26b8838bf2260e8af630b8
|
4
|
+
data.tar.gz: 2c2ec53083d467762f99af4d7f8d3740e86c6f36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc396ae6248ab04ae2262b712bc40930cfafd73a1d51026a827f3e688f19d3f529e1c859c749dc5b43f49badbb9bec1ab5ae203ea6c6a10815a6f92beedc8b25
|
7
|
+
data.tar.gz: bb3d7c923cde3bbdc724aaf28532e0c9d9f9ccaf2c7285cbb9bef7288bea76c97579903a94dc4af83eca5311da5a8961c5a894d957aad64b4886c3e9c51e699d
|
data/README.md
CHANGED
@@ -13,10 +13,12 @@ To use, issue the command (where `lita` is your robots name):
|
|
13
13
|
|
14
14
|
```
|
15
15
|
lita count /regex/ since:1_week_ago until:now
|
16
|
-
=> 12 results
|
16
|
+
=> Found 12 results for /regex/ since 1 week ago until now.
|
17
17
|
```
|
18
18
|
|
19
|
-
`since` and `until` are both optional
|
19
|
+
`since` and `until` are both optional and default to the listed values.
|
20
|
+
|
21
|
+
The search will also skip over previous queries (`lita count /regex/`) and previous bot responses (`Found 12 results for...`).
|
20
22
|
|
21
23
|
## Running tests
|
22
24
|
|
@@ -8,6 +8,10 @@ module Lita
|
|
8
8
|
since: "1 week ago",
|
9
9
|
until: "now"
|
10
10
|
}
|
11
|
+
MESSAGES = {
|
12
|
+
found: "Found %{count} results for */%{regex_string}/* since *%{oldest}* until *%{latest}*.",
|
13
|
+
invalid_time_format: "Couldn't understand `since:%{oldest} until:%{latest}`."
|
14
|
+
}
|
11
15
|
|
12
16
|
route(
|
13
17
|
/^count\s+(\S+)(.*)/i,
|
@@ -19,19 +23,25 @@ module Lita
|
|
19
23
|
)
|
20
24
|
|
21
25
|
def count(response)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
26
|
+
data = {
|
27
|
+
oldest: time_string_for('since', response) || DEFAULT_RANGE[:since],
|
28
|
+
latest: time_string_for('until', response) || DEFAULT_RANGE[:until],
|
29
|
+
regex_string: response.matches.first.first.tr('/', '')
|
30
|
+
}
|
27
31
|
|
28
32
|
begin
|
29
|
-
messages = fetch_slack_message_history(response.room.id, oldest, latest)
|
30
|
-
|
31
|
-
|
33
|
+
messages = fetch_slack_message_history(response.room.id, data[:oldest], data[:latest])
|
34
|
+
regex = Regexp.new data[:regex_string]
|
35
|
+
reply = MESSAGES[:found] % data.merge({
|
36
|
+
count: count_messages(messages, regex)
|
37
|
+
})
|
32
38
|
rescue InvalidTimeFormatError
|
33
|
-
|
39
|
+
reply = MESSAGES[:invalid_time_format] % {
|
40
|
+
oldest: data[:oldest].tr(" ", "_"),
|
41
|
+
latest: data[:latest].tr(" ", "_")
|
42
|
+
}
|
34
43
|
end
|
44
|
+
response.reply(reply)
|
35
45
|
end
|
36
46
|
|
37
47
|
protected
|
@@ -59,6 +69,14 @@ module Lita
|
|
59
69
|
messages
|
60
70
|
end
|
61
71
|
|
72
|
+
def count_messages(messages, regex)
|
73
|
+
messages.count do |message|
|
74
|
+
next if message.user == robot_user_id # skip messages from self
|
75
|
+
next if message.text.match(/^@?#{robot.mention_name}\scount.*/i) # skip `bot count` queries
|
76
|
+
message.text.match regex
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
62
80
|
def time_string_for(type, response)
|
63
81
|
raise "unknown time string type" unless %w(since until).include? type
|
64
82
|
options_string = response.matches.last.last
|
@@ -75,6 +93,13 @@ module Lita
|
|
75
93
|
sprintf("%0.06f", parsed_string.utc.to_f)
|
76
94
|
end
|
77
95
|
|
96
|
+
# couldn't find a direct way to get robot user id (e.g. robot.user_id)
|
97
|
+
# so instead we use the method that `lita users find [name]` uses:
|
98
|
+
# https://github.com/litaio/lita-default-handlers/blob/master/lib/lita/handlers/users.rb
|
99
|
+
def robot_user_id
|
100
|
+
robot_id = User.fuzzy_find(robot.mention_name).id
|
101
|
+
end
|
102
|
+
|
78
103
|
def slack_client
|
79
104
|
@slack_client ||= ::Slack::Web::Client.new
|
80
105
|
end
|
data/lita-regexcellent.gemspec
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "lita-regexcellent"
|
3
|
-
spec.version = "0.1.
|
3
|
+
spec.version = "0.1.1"
|
4
4
|
spec.authors = ["Eric Yang"]
|
5
5
|
spec.email = ["eyang232@gmail.com"]
|
6
|
-
spec.description = "
|
7
|
-
spec.summary = "
|
6
|
+
spec.description = "Search and parse through Slack channel history with regex"
|
7
|
+
spec.summary = "Search and parse through Slack channel history with regex"
|
8
8
|
spec.homepage = "https://github.com/yangez/lita-regexcellent"
|
9
9
|
spec.license = "MIT"
|
10
10
|
spec.metadata = { "lita_plugin_type" => "handler" }
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
RSpec.describe Lita::Handlers::Regexcellent, :lita_handler => true do
|
4
|
+
let(:robot) { Lita::Robot.new(registry) }
|
5
|
+
|
6
|
+
let(:robot_id) { "STUBBED_ID" }
|
7
|
+
let(:message_stub) { Struct.new(:text, :user) }
|
8
|
+
|
9
|
+
subject { described_class.new(robot) }
|
10
|
+
|
11
|
+
describe "#count" do
|
12
|
+
let(:room) { double(Lita::Room, id: 1)}
|
13
|
+
let(:messages) { [] }
|
14
|
+
|
15
|
+
|
16
|
+
before do
|
17
|
+
allow_any_instance_of(described_class).to receive(:fetch_slack_message_history).and_return(messages)
|
18
|
+
allow_any_instance_of(Lita::Response).to receive(:room).and_return(room)
|
19
|
+
allow_any_instance_of(described_class).to receive(:robot_user_id).and_return(robot_id)
|
20
|
+
end
|
21
|
+
|
22
|
+
it { is_expected.to route("@lita count regex") }
|
23
|
+
it { is_expected.to route("lita count regex") }
|
24
|
+
it { is_expected.to route("lita count regex since:yesterday until:today") }
|
25
|
+
|
26
|
+
it "responds with a count" do
|
27
|
+
send_message("Lita count regex")
|
28
|
+
expect(replies.last).to match /Found 0 results for \*\/regex\/\* since \*1 week ago\* until \*now\*\./
|
29
|
+
end
|
30
|
+
|
31
|
+
context "when there are messages from itself" do
|
32
|
+
let(:self_message) { message_stub.new("hi world", robot_id) }
|
33
|
+
let(:valid_message) { message_stub.new("hi world", "REGULAR_USER_ID") }
|
34
|
+
let(:messages) { [self_message, valid_message] }
|
35
|
+
|
36
|
+
it "does not count self messages" do
|
37
|
+
send_message("Lita count world")
|
38
|
+
expect(replies.last).to match /^Found 1 result.*/
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when there are existing messages querying the bot" do
|
43
|
+
let(:query_message) { message_stub.new("lita count so cool", "REGULAR_USER_ID") }
|
44
|
+
let(:query_message2) { message_stub.new("@lita count so cool", "REGULAR_USER_ID") }
|
45
|
+
let(:valid_message) { message_stub.new("so cool indeed", "REGULAR_USER_ID") }
|
46
|
+
let(:messages) { [query_message, query_message2, valid_message] }
|
47
|
+
|
48
|
+
it "does not count querying messages" do
|
49
|
+
send_message("Lita count cool")
|
50
|
+
expect(replies.last).to match /^Found 1 result.*/
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when regex is formatted as /regex/" do
|
55
|
+
it "responds with a count" do
|
56
|
+
send_message("lita count /regex/")
|
57
|
+
expect(replies.last).to match /Found 0 results for \*\/regex\/\* since \*1 week ago\* until \*now\*\./
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "#fetch_slack_message_history" do
|
63
|
+
it "will be tested"
|
64
|
+
end
|
65
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lita-regexcellent
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Yang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lita
|
@@ -122,7 +122,7 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: 3.0.0
|
125
|
-
description:
|
125
|
+
description: Search and parse through Slack channel history with regex
|
126
126
|
email:
|
127
127
|
- eyang232@gmail.com
|
128
128
|
executables: []
|
@@ -138,7 +138,7 @@ files:
|
|
138
138
|
- lib/lita/handlers/regexcellent.rb
|
139
139
|
- lita-regexcellent.gemspec
|
140
140
|
- locales/en.yml
|
141
|
-
- spec/lita/handlers/
|
141
|
+
- spec/lita/handlers/regexcellent_spec.rb
|
142
142
|
- spec/spec_helper.rb
|
143
143
|
- templates/.gitkeep
|
144
144
|
homepage: https://github.com/yangez/lita-regexcellent
|
@@ -165,7 +165,7 @@ rubyforge_project:
|
|
165
165
|
rubygems_version: 2.6.10
|
166
166
|
signing_key:
|
167
167
|
specification_version: 4
|
168
|
-
summary:
|
168
|
+
summary: Search and parse through Slack channel history with regex
|
169
169
|
test_files:
|
170
|
-
- spec/lita/handlers/
|
170
|
+
- spec/lita/handlers/regexcellent_spec.rb
|
171
171
|
- spec/spec_helper.rb
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
RSpec.describe Lita::Handlers::Regexcellent, :lita_handler => true do
|
4
|
-
let(:robot) { Lita::Robot.new(registry) }
|
5
|
-
|
6
|
-
|
7
|
-
subject { described_class.new(robot) }
|
8
|
-
|
9
|
-
describe "#count" do
|
10
|
-
let(:room) { double(Lita::Room, id: 1)}
|
11
|
-
before do
|
12
|
-
allow_any_instance_of(described_class).to receive(:fetch_slack_message_history).and_return([])
|
13
|
-
allow_any_instance_of(Lita::Response).to receive(:room).and_return(room)
|
14
|
-
end
|
15
|
-
|
16
|
-
it { is_expected.to route("Lita count regex") }
|
17
|
-
it { is_expected.to route("Lita count regex since:yesterday until:today") }
|
18
|
-
|
19
|
-
it "responds with a count" do
|
20
|
-
send_message("Lita count regex")
|
21
|
-
expect(replies.last).to match /Found 0 results for \*\/regex\/\* since \*1 week ago\* until \*now\*\./
|
22
|
-
end
|
23
|
-
|
24
|
-
context "when regex is formatted as /regex/" do
|
25
|
-
it "responds with a count" do
|
26
|
-
send_message("Lita count /regex/")
|
27
|
-
expect(replies.last).to match /Found 0 results for \*\/regex\/\* since \*1 week ago\* until \*now\*\./
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "#fetch_slack_message_history" do
|
33
|
-
it "will be tested"
|
34
|
-
end
|
35
|
-
end
|