ruboty-slack_take_turns 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fbecdea07ac49e879c4cb05ad2c25d041a90da2d
4
+ data.tar.gz: 86e6f32d3d1cf351e144b290ec09157f9636ee0f
5
+ SHA512:
6
+ metadata.gz: 647821d34aedd620f87ee9afbeb3850bbee49c4a7fba3744f687c55fc97cb61520de6bcf6d701aadfec5f63a4d2b003f0372e97e00c8999b8e2a9a59f0e4d4f3
7
+ data.tar.gz: e7c2f156a73c6444459e7f79b807b3b3d278881ee86fdb2b623f5ca3d672e40aad13dc9bfa51c41f109c4bbf105ac62a65d941b30e1a4c0aa2032213fa2af211
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruboty-slack_take_turns.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 oharato
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.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Ruboty::SlackTakeTurns
2
+
3
+ Ruboty plugin to manage a duty that members in a slack channel take turns on
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruboty-slack_take_turns'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruboty-slack_take_turns
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruboty-slack_take_turns.
34
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruboty/slack_take_turns"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,68 @@
1
+ module Ruboty
2
+ module Handlers
3
+ # manage a duty that members in a slack channel take turns on
4
+ class SlackTakeTurns < Base
5
+ NAMESPACE = "slack_take_turns"
6
+
7
+ on(
8
+ /members/,
9
+ name: "members",
10
+ description: "member list in a channel(order by slack-user-id asc)"
11
+ )
12
+ on(
13
+ /force (?<user_name>.+?)\z/,
14
+ name: "force",
15
+ description: "make a specified member to be on duty",
16
+ )
17
+ on(
18
+ /current/,
19
+ name: "current",
20
+ description: "show who is on duty currently",
21
+ )
22
+ on(
23
+ /next/,
24
+ name: "next",
25
+ description: "pass on a duty to the next member",
26
+ )
27
+ on(
28
+ /exclude (?<user_name>.+?)\z/,
29
+ name: "exclude",
30
+ description: "make a specified member to not be on duty after this",
31
+ )
32
+ on(
33
+ /(?<keyword>#{ENV['ASSIGN_REGEX']})/m,
34
+ name: "assign",
35
+ description: "When a macthed keyword is posted, the bot assigns a person on duty to a task related the keyword.",
36
+ all: true,
37
+ )
38
+
39
+ env :SLACK_TOKEN, "Browse Apps > Custom Integrations > Bots > API Token"
40
+ env :ASSIGN_REGEX, "e.g. .*http:\/\/github.com.*"
41
+
42
+ def members(message)
43
+ Ruboty::SlackTakeTurns::Actions::Members.new(message).call
44
+ end
45
+
46
+ def force(message)
47
+ Ruboty::SlackTakeTurns::Actions::Force.new(message).call
48
+ end
49
+
50
+ def current(message)
51
+ Ruboty::SlackTakeTurns::Actions::Current.new(message).call
52
+ end
53
+
54
+ def next(message)
55
+ Ruboty::SlackTakeTurns::Actions::Next.new(message).call
56
+ end
57
+
58
+ def exclude(message)
59
+ Ruboty::SlackTakeTurns::Actions::Exclude.new(message).call
60
+ end
61
+
62
+ def assign(message)
63
+ Ruboty::SlackTakeTurns::Actions::Assign.new(message).call
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,17 @@
1
+ require "ruboty"
2
+ require "ruboty/slack_take_turns/slack_client"
3
+ require "ruboty/slack_take_turns/actions/base"
4
+ require "ruboty/slack_take_turns/actions/assign"
5
+ require "ruboty/slack_take_turns/actions/current"
6
+ require "ruboty/slack_take_turns/actions/exclude"
7
+ require "ruboty/slack_take_turns/actions/force"
8
+ require "ruboty/slack_take_turns/actions/members"
9
+ require "ruboty/slack_take_turns/actions/next"
10
+ require "ruboty/slack_take_turns/version"
11
+ require "ruboty/handlers/slack_take_turns"
12
+
13
+ module Ruboty
14
+ module SlackTakeTurns
15
+ # Your code goes here...
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module Ruboty
2
+ module SlackTakeTurns
3
+ module Actions
4
+ class Assign < Base
5
+ def call
6
+ message.reply(assign)
7
+ rescue ActionBaseError => e
8
+ message.reply(e.message)
9
+ Ruboty.logger.info e.to_s
10
+ rescue => e
11
+ message.reply(e.message)
12
+ raise e
13
+ end
14
+
15
+ private
16
+
17
+ def assign
18
+ # avoid self-repeating
19
+ return if message.from_name == message.robot.name
20
+ keyword = message[:keyword]
21
+ <<~"EOF"
22
+ @#{current_user_name}, please deal with the task below.
23
+ #{keyword}
24
+ EOF
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,95 @@
1
+ module Ruboty
2
+ module SlackTakeTurns
3
+ module Actions
4
+ class Base
5
+ NAMESPACE = 'slack_duty'
6
+
7
+ attr_reader :message
8
+
9
+ def initialize(message)
10
+ @message = message
11
+ end
12
+
13
+ def channel
14
+ message.to
15
+ end
16
+
17
+ def sender_id
18
+ message.from
19
+ end
20
+
21
+ def data
22
+ message.robot.brain.data[NAMESPACE] ||= {}
23
+ end
24
+
25
+ def channel_data
26
+ data[channel] ||= {}
27
+ end
28
+
29
+ def current_user_id
30
+ channel_data[:current_user_id]
31
+ end
32
+
33
+ def current_user_id=(id)
34
+ channel_data[:current_user_id] = id
35
+ end
36
+
37
+ def slack_client
38
+ @slack_client ||= Ruboty::SlackTakeTurns::SlackClient.new(channel)
39
+ end
40
+
41
+ def target_user_ids
42
+ slack_client.channel_user_ids - excluded_user_ids
43
+ end
44
+
45
+ def excluded_user_ids
46
+ channel_data[:excluded_user_ids] ||= []
47
+ end
48
+
49
+ def current_user_name
50
+ find_user_by_user_id(current_user_id)['name']
51
+ rescue
52
+ raise CurrentUserNotFound.new(chat_message: message)
53
+ end
54
+
55
+ def find_user_by_user_id(user_id)
56
+ slack_client.all_users_hash[user_id] || raise(UserNotFound.new(user_id: user_id, chat_message: message))
57
+ end
58
+
59
+ def find_user_id_by_user_name(user_name)
60
+ user = slack_client.all_users.find{|u| u['name'] == user_name}
61
+ user ? user['id'] : raise(UserNotFound.new(user_name: user_name, chat_message: message))
62
+ end
63
+
64
+ class ActionBaseError < StandardError
65
+ def initialize(chat_message: nil, user_id: nil, user_name: nil)
66
+ @chat_message = chat_message
67
+ @user_id = user_id
68
+ @user_name = user_name
69
+ end
70
+
71
+ def to_s
72
+ <<~"EOF"
73
+ #{self.class.to_s}
74
+ user_id:#{@user_id}, user_name:#{@user_name}, from:#{@chat_message.from_name}, channel:#{@chat_message.to}, body:#{@chat_message.body}
75
+ #{backtrace.take(4).join("\n")}
76
+ EOF
77
+ end
78
+ end
79
+
80
+ class CurrentUserNotFound < ActionBaseError
81
+ def message
82
+ "not specified who is on duty. use 'force' command"
83
+ end
84
+ end
85
+
86
+ class UserNotFound < ActionBaseError
87
+ def message
88
+ "#{@user_name} is not found. spelling mistake?"
89
+ end
90
+ end
91
+
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,24 @@
1
+ module Ruboty
2
+ module SlackTakeTurns
3
+ module Actions
4
+ class Current < Base
5
+ def call
6
+ message.reply(current)
7
+ rescue ActionBaseError => e
8
+ message.reply(e.message)
9
+ Ruboty.logger.info e.to_s
10
+ rescue => e
11
+ message.reply(e.message)
12
+ raise e
13
+ end
14
+
15
+ private
16
+
17
+ def current
18
+ "@#{current_user_name} is on duty now."
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,29 @@
1
+ module Ruboty
2
+ module SlackTakeTurns
3
+ module Actions
4
+ class Exclude < Base
5
+ def call
6
+ message.reply(exclude)
7
+ rescue ActionBaseError => e
8
+ message.reply(e.message)
9
+ Ruboty.logger.info e.to_s
10
+ rescue => e
11
+ message.reply(e.message)
12
+ raise e
13
+ end
14
+
15
+ private
16
+
17
+ def exclude
18
+ user_name = message[:user_name]
19
+ user_id = find_user_id_by_user_name(user_name)
20
+ unless excluded_user_ids.include? user_id
21
+ excluded_user_ids << user_id
22
+ end
23
+ "made @#{user_name} to not be on duty after this"
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ module Ruboty
2
+ module SlackTakeTurns
3
+ module Actions
4
+ class Force < Base
5
+ def call
6
+ message.reply(force)
7
+ rescue ActionBaseError => e
8
+ message.reply(e.message)
9
+ Ruboty.logger.info e.to_s
10
+ rescue => e
11
+ message.reply(e.message)
12
+ raise e
13
+ end
14
+
15
+ private
16
+
17
+ def force
18
+ user_name = message[:user_name]
19
+ user_id = find_user_id_by_user_name(user_name)
20
+ if target_user_ids.include? user_id
21
+ channel_data[:current_user_id] = user_id
22
+ "made @#{user_name} to take over a duty"
23
+ else
24
+ "#{user_name} is free from a duty"
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ module Ruboty
2
+ module SlackTakeTurns
3
+ module Actions
4
+ class Members < Base
5
+ def call
6
+ message.reply(members)
7
+ rescue ActionBaseError => e
8
+ message.reply(e.message)
9
+ Ruboty.logger.info e.to_s
10
+ rescue => e
11
+ message.reply(e.message)
12
+ raise e
13
+ end
14
+
15
+ private
16
+ def members
17
+ user_names = target_user_ids.map do |id|
18
+ name = find_user_by_user_id(id)['name']
19
+ name = ":triangular_flag_on_post:#{name}" if id == current_user_id
20
+ name
21
+ end
22
+ user_names.join(', ')
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ module Ruboty
2
+ module SlackTakeTurns
3
+ module Actions
4
+ class Next < Base
5
+ def call
6
+ message.reply(_next)
7
+ rescue ActionBaseError => e
8
+ message.reply(e.message)
9
+ Ruboty.logger.info e.to_s
10
+ rescue => e
11
+ message.reply(e.message)
12
+ raise e
13
+ end
14
+
15
+ private
16
+
17
+ def _next
18
+ self.current_user_id = find_next
19
+ "passed on a duty to the next member. @#{current_user_name} is on duty now."
20
+ end
21
+
22
+ def find_next
23
+ raise(CurrentUserNotFound.new(chat_message: message)) unless current_user_id
24
+ idx = target_user_ids.index(current_user_id)
25
+ raise UserNotFound.new(chat_message: message, user_id: current_user_id) unless idx
26
+ if idx == target_user_ids.size - 1
27
+ target_user_ids.first
28
+ else
29
+ target_user_ids[idx+1]
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,57 @@
1
+ module Ruboty
2
+ module SlackTakeTurns
3
+ class SlackClient
4
+
5
+ attr_reader :client, :channel
6
+
7
+ def initialize(channel)
8
+ @client ||= Slack::Client.new(token: ENV['SLACK_TOKEN'])
9
+ @channel = channel
10
+ end
11
+
12
+ def all_users
13
+ @all_users ||= client.users_list['members']
14
+ end
15
+
16
+ # ユーザを取得しやすくするためにユーザIDとユーザオブジェクトのハッシュテーブルを作る
17
+ def all_users_hash
18
+ @all_users_hash ||= {}.tap do |h|
19
+ all_users.each{|u| h[u['id']] = u}
20
+ end
21
+ end
22
+
23
+ # チャンネル内の全ユーザid
24
+ def channel_user_ids
25
+ unless @channel_user_ids&.dig(channel)
26
+ @channel_user_ids = {} unless @channel_user_ids
27
+ ids = private_channel? ? private_channel_user_ids : public_channel_user_ids
28
+ @channel_user_ids[channel] = ids.sort
29
+ end
30
+ @channel_user_ids[channel]
31
+ end
32
+
33
+ def channels_info
34
+ @channels_info ||= client.channels_info(channel: channel)
35
+ end
36
+
37
+ def public_channel_user_ids
38
+ channels_info['channel']['members']
39
+ end
40
+
41
+ def private_channel_user_ids
42
+ private_channels = groups_list
43
+ current_channel = private_channels.find{|c| c['id'] == channel}
44
+ current_channel['members']
45
+ end
46
+
47
+ def groups_list
48
+ @groups_list ||= client.groups_list['groups']
49
+ end
50
+
51
+ def private_channel?
52
+ channels_info['error'] == "channel_not_found"
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module SlackTakeTurns
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruboty/slack_take_turns/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruboty-slack_take_turns"
8
+ spec.version = Ruboty::SlackTakeTurns::VERSION
9
+ spec.authors = ["oharato"]
10
+ spec.email = ["oharato@live.jp"]
11
+
12
+ spec.summary = %q{Ruboty plugin to manage a duty that members in a slack channel take turns on}
13
+ spec.description = %q{Ruboty plugin to manage a duty that members in a slack channel take turns on}
14
+ spec.homepage = "https://github.com/oharato/ruboty-slack_take_turns"
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+
27
+ spec.add_dependency "ruboty-slack_rtm"
28
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-slack_take_turns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - oharato
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruboty-slack_rtm
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
+ description: Ruboty plugin to manage a duty that members in a slack channel take turns
56
+ on
57
+ email:
58
+ - oharato@live.jp
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/ruboty/handlers/slack_take_turns.rb
71
+ - lib/ruboty/slack_take_turns.rb
72
+ - lib/ruboty/slack_take_turns/actions/assign.rb
73
+ - lib/ruboty/slack_take_turns/actions/base.rb
74
+ - lib/ruboty/slack_take_turns/actions/current.rb
75
+ - lib/ruboty/slack_take_turns/actions/exclude.rb
76
+ - lib/ruboty/slack_take_turns/actions/force.rb
77
+ - lib/ruboty/slack_take_turns/actions/members.rb
78
+ - lib/ruboty/slack_take_turns/actions/next.rb
79
+ - lib/ruboty/slack_take_turns/slack_client.rb
80
+ - lib/ruboty/slack_take_turns/version.rb
81
+ - ruboty-slack_take_turns.gemspec
82
+ homepage: https://github.com/oharato/ruboty-slack_take_turns
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.5.1
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Ruboty plugin to manage a duty that members in a slack channel take turns
106
+ on
107
+ test_files: []