lita-regexcellent 0.1.0

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: b683b6842bef7d8c7bc645382bc5045e171d9d2e
4
+ data.tar.gz: 48098fe940cca746b0f8f120da8266d04fdbdff9
5
+ SHA512:
6
+ metadata.gz: e60dcff01bb238161b2dd5f35afdd1367e5b13483724add44f195e26845af9e2869faa6d22667e6a490b56accc9a8bbf08e11a25212650b3066511e314f86549
7
+ data.tar.gz: ef61572ff091fbe6b198880da6342033130583be39d588dc071c14c95010d945cb987f44eb9136df01e8eefde832b368d4d2cf51308a2413c60192fd64a21a0a
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Eric Yang
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,24 @@
1
+ # lita-regexcellent
2
+
3
+ Counts the number of Slack messages that matches a regular expression in the current channel. Only compatible with the Slack adapter.
4
+
5
+ ## Installation
6
+
7
+ 1. Add gem to your Lita instance: `gem "lita-regexcellent"`
8
+ 2. Set SLACK_TOKEN if not already set: `heroku config:set SLACK_TOKEN=$token`
9
+
10
+ ## Usage
11
+
12
+ To use, issue the command (where `lita` is your robots name):
13
+
14
+ ```
15
+ lita count /regex/ since:1_week_ago until:now
16
+ => 12 results found.
17
+ ```
18
+
19
+ `since` and `until` are both optional. They default to the listed values.
20
+
21
+ ## Running tests
22
+
23
+ 1. Make sure Redis is running locally
24
+ 2. `rspec spec`
@@ -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,18 @@
1
+ require "lita"
2
+ require "chronic"
3
+ require "slack-ruby-client"
4
+
5
+ Lita.load_locales Dir[File.expand_path(
6
+ File.join("..", "..", "locales", "*.yml"), __FILE__
7
+ )]
8
+
9
+ require "lita/handlers/regexcellent"
10
+
11
+ Lita::Handlers::Regexcellent.template_root File.expand_path(
12
+ File.join("..", "..", "templates"),
13
+ __FILE__
14
+ )
15
+
16
+ Slack.configure do |config|
17
+ config.token = ENV['SLACK_TOKEN']
18
+ end
@@ -0,0 +1,84 @@
1
+ module Lita
2
+ module Handlers
3
+ class Regexcellent < Handler
4
+ Lita.register_handler(self)
5
+ class InvalidTimeFormatError < StandardError; end
6
+
7
+ DEFAULT_RANGE = {
8
+ since: "1 week ago",
9
+ until: "now"
10
+ }
11
+
12
+ route(
13
+ /^count\s+(\S+)(.*)/i,
14
+ :count,
15
+ :command => true,
16
+ :help => {
17
+ "count REGEX since:1_week_ago until:now" => "Counts number of regex matches in channel. 'since' and 'until' are optional (defaults shown)"
18
+ }
19
+ )
20
+
21
+ 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
27
+
28
+ 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}*.")
32
+ rescue InvalidTimeFormatError
33
+ response.reply("Couldn't understand `since:#{oldest.tr(" ", "_")} until:#{latest.tr(" ", "_")}`.")
34
+ end
35
+ end
36
+
37
+ protected
38
+
39
+ def fetch_slack_message_history(room_id, oldest, latest)
40
+ messages = []
41
+
42
+ loop do
43
+ options = {
44
+ channel: room_id,
45
+ count: 1000,
46
+ inclusive: 0,
47
+ oldest: string_to_timestamp(oldest),
48
+ latest: string_to_timestamp(latest)
49
+ }
50
+ history = slack_client.channels_history(options)
51
+ messages.push(history.messages).flatten!
52
+
53
+ if history.has_more
54
+ latest = history.messages.last.ts
55
+ else
56
+ break
57
+ end
58
+ end
59
+ messages
60
+ end
61
+
62
+ def time_string_for(type, response)
63
+ raise "unknown time string type" unless %w(since until).include? type
64
+ options_string = response.matches.last.last
65
+ return unless options_string && options_string.length > 0
66
+ time_string = options_string.match(/#{type}:(\S+)/)
67
+ time_string.captures.first.try(:tr, "_", " ") if time_string
68
+ end
69
+
70
+ def string_to_timestamp(time_string)
71
+ parsed_string = Chronic.parse(time_string)
72
+ fail InvalidTimeFormatError unless parsed_string
73
+
74
+ # slack API can only handle 6 digits, otherwise it bugs out
75
+ sprintf("%0.06f", parsed_string.utc.to_f)
76
+ end
77
+
78
+ def slack_client
79
+ @slack_client ||= ::Slack::Web::Client.new
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-regexcellent"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["Eric Yang"]
5
+ spec.email = ["eyang232@gmail.com"]
6
+ spec.description = "Parse through channel history with regex"
7
+ spec.summary = "Parse through channel history with regex"
8
+ spec.homepage = "https://github.com/yangez/lita-regexcellent"
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.6"
18
+ spec.add_runtime_dependency "chronic"
19
+ spec.add_runtime_dependency "slack-ruby-client"
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "pry-byebug"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rack-test"
25
+ spec.add_development_dependency "rspec", ">= 3.0.0"
26
+ end
@@ -0,0 +1,4 @@
1
+ en:
2
+ lita:
3
+ handlers:
4
+ regexcellent:
@@ -0,0 +1,35 @@
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
@@ -0,0 +1,6 @@
1
+ require "lita-regexcellent"
2
+ require "lita/rspec"
3
+
4
+ # A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
5
+ # was generated with Lita 4, the compatibility mode should be left disabled.
6
+ Lita.version_3_compatibility_mode = false
File without changes
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-regexcellent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Yang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-29 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.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: chronic
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: slack-ruby-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
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: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rack-test
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: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 3.0.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 3.0.0
125
+ description: Parse through channel history with regex
126
+ email:
127
+ - eyang232@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - Gemfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - lib/lita-regexcellent.rb
138
+ - lib/lita/handlers/regexcellent.rb
139
+ - lita-regexcellent.gemspec
140
+ - locales/en.yml
141
+ - spec/lita/handlers/count_spec.rb
142
+ - spec/spec_helper.rb
143
+ - templates/.gitkeep
144
+ homepage: https://github.com/yangez/lita-regexcellent
145
+ licenses:
146
+ - MIT
147
+ metadata:
148
+ lita_plugin_type: handler
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.6.10
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Parse through channel history with regex
169
+ test_files:
170
+ - spec/lita/handlers/count_spec.rb
171
+ - spec/spec_helper.rb