lita-regexcellent 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b683b6842bef7d8c7bc645382bc5045e171d9d2e
4
- data.tar.gz: 48098fe940cca746b0f8f120da8266d04fdbdff9
3
+ metadata.gz: 78dae87531be14359f26b8838bf2260e8af630b8
4
+ data.tar.gz: 2c2ec53083d467762f99af4d7f8d3740e86c6f36
5
5
  SHA512:
6
- metadata.gz: e60dcff01bb238161b2dd5f35afdd1367e5b13483724add44f195e26845af9e2869faa6d22667e6a490b56accc9a8bbf08e11a25212650b3066511e314f86549
7
- data.tar.gz: ef61572ff091fbe6b198880da6342033130583be39d588dc071c14c95010d945cb987f44eb9136df01e8eefde832b368d4d2cf51308a2413c60192fd64a21a0a
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 found.
16
+ => Found 12 results for /regex/ since 1 week ago until now.
17
17
  ```
18
18
 
19
- `since` and `until` are both optional. They default to the listed values.
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
- oldest = time_string_for('since', response) || DEFAULT_RANGE[:since]
23
- latest = time_string_for('until', response) || DEFAULT_RANGE[:until]
24
-
25
- regex_string = response.matches.first.first.tr('/', '')
26
- regex = Regexp.new regex_string
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
- count = messages.count{ |message| message.text.match regex }
31
- response.reply("Found #{count} results for */#{regex_string}/* since *#{oldest}* until *#{latest}*.")
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
- response.reply("Couldn't understand `since:#{oldest.tr(" ", "_")} until:#{latest.tr(" ", "_")}`.")
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
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-regexcellent"
3
- spec.version = "0.1.0"
3
+ spec.version = "0.1.1"
4
4
  spec.authors = ["Eric Yang"]
5
5
  spec.email = ["eyang232@gmail.com"]
6
- spec.description = "Parse through channel history with regex"
7
- spec.summary = "Parse through channel history with regex"
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.0
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-01-29 00:00:00.000000000 Z
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: Parse through channel history with regex
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/count_spec.rb
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: Parse through channel history with regex
168
+ summary: Search and parse through Slack channel history with regex
169
169
  test_files:
170
- - spec/lita/handlers/count_spec.rb
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