slatan 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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +4 -0
  5. data/CODE_OF_CONDUCT.md +13 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +59 -0
  9. data/Rakefile +26 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +6 -0
  12. data/lib/slatan/affiliation/channels.rb +36 -0
  13. data/lib/slatan/affiliation/self.rb +17 -0
  14. data/lib/slatan/affiliation/sub_teams.rb +17 -0
  15. data/lib/slatan/affiliation/team.rb +17 -0
  16. data/lib/slatan/affiliation/users.rb +54 -0
  17. data/lib/slatan/affiliation.rb +15 -0
  18. data/lib/slatan/buttocks.rb +75 -0
  19. data/lib/slatan/ear.rb +27 -0
  20. data/lib/slatan/heart.rb +88 -0
  21. data/lib/slatan/mouth/api.rb +19 -0
  22. data/lib/slatan/mouth/auth.rb +19 -0
  23. data/lib/slatan/mouth/channels.rb +57 -0
  24. data/lib/slatan/mouth/chat.rb +39 -0
  25. data/lib/slatan/mouth/emoji.rb +19 -0
  26. data/lib/slatan/mouth/ext/chat.rb +68 -0
  27. data/lib/slatan/mouth/ext.rb +10 -0
  28. data/lib/slatan/mouth/files.rb +19 -0
  29. data/lib/slatan/mouth/groups.rb +71 -0
  30. data/lib/slatan/mouth/im.rb +48 -0
  31. data/lib/slatan/mouth/mpim.rb +42 -0
  32. data/lib/slatan/mouth/pins.rb +28 -0
  33. data/lib/slatan/mouth/reactions.rb +38 -0
  34. data/lib/slatan/mouth/rtm.rb +19 -0
  35. data/lib/slatan/mouth/stars.rb +24 -0
  36. data/lib/slatan/mouth/team.rb +19 -0
  37. data/lib/slatan/mouth/users.rb +40 -0
  38. data/lib/slatan/mouth.rb +73 -0
  39. data/lib/slatan/spirit.rb +34 -0
  40. data/lib/slatan/template/concern.rb.erb +14 -0
  41. data/lib/slatan/utils/integer_ex.rb +17 -0
  42. data/lib/slatan/utils/string_ex.rb +11 -0
  43. data/lib/slatan/version.rb +3 -0
  44. data/lib/slatan.rb +44 -0
  45. data/slatan.gemspec +29 -0
  46. metadata +172 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 762a507d89d22855482240d036c69e467bf28f41
4
+ data.tar.gz: 1b914cd1ac32ea6681d160421190a40db5a230c2
5
+ SHA512:
6
+ metadata.gz: ea1474306a99a0a8fcbf8f208cfa1402e2d022be4675f7710ec6be5c6f85eeae705eeace8ea6941bb524f08769913c2ae773e5104ee93a6bad0494ad63848a2e
7
+ data.tar.gz: 1ca277268fc5caf17644bfe8334bcb1b651b3689e44c6465ac162297e99f1ee004e04827bdaff8e159be8bbca430b702ff91547efead1a901a6060ea087a506f
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
11
+ .DS_Store
12
+ /concerns/
13
+ config.json
14
+ /exe/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slatan.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 kudohamu
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Slatan
2
+
3
+ Ruby gem to create bot for Slack easily and flexibly.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'slatan'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install slatan
20
+
21
+ ## Usage
22
+
23
+ #### 1. Add 'Bot' integration and cory API token on Slack.
24
+
25
+ #### 2. Generate event subscriber class.
26
+
27
+ ```
28
+ $ rake slatan:generate['/path/to/your/preference/test.rb']
29
+ ```
30
+
31
+ #### 3. Write code to running slatan.
32
+
33
+ ```ruby
34
+ require 'slatan'
35
+ require 'path/to/your/preference/test.rb'
36
+
37
+ Slatan::Spirit.slack_token = 'xxxx-xxxxxx-xxxxxxxxxx' #or `export SLACK_TOKEN=xxxxx-xxxx-xxxxxxxxxx`
38
+
39
+ Slatan::Ear.register(Test.new) #register event subscriber
40
+
41
+ Slatan.run #write 'Slatan.run({ daemonize: true })' if you want to run daemonize mode.
42
+ ```
43
+
44
+ #### 4. type 'ping' on Slack
45
+
46
+ ![slatan example](https://dl.dropboxusercontent.com/u/74925506/slatan_invitation.png)
47
+
48
+ ##Documentation
49
+ Under construction...
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kudohamu/slatan.
54
+
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
59
+
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require "bundler/gem_tasks"
2
+ require 'erb'
3
+ require 'active_support/core_ext/object'
4
+ require 'slatan/utils/string_ex'
5
+
6
+ using Slatan::Utils::StringEx
7
+ namespace :slatan do
8
+ desc "Kill slatan daemon process."
9
+ task :stop do
10
+ path = ENV['SLATAN_PID_FILE_PATH'].presence || '/tmp/slatan.pid'
11
+ sh "kill `cat #{path}`"
12
+ end
13
+
14
+ desc "Generate event subscriber of slack message."
15
+ task :generate, [:file_path] do |task, args|
16
+ file_name = File.basename(args.file_path, '.*')
17
+ class_name = file_name.camelize
18
+ dir_path = File.dirname(args.file_path)
19
+ File.open('./lib/slatan/template/concern.rb.erb') do |file|
20
+ erb = ERB.new(file.read).result(binding)
21
+ File.open(File.join(dir_path, "#{file_name}.rb"), 'w') do |file|
22
+ file << erb
23
+ end
24
+ end
25
+ end
26
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "slatan"
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,6 @@
1
+ #!/bin/bash
2
+
3
+ set -euo pipefail
4
+ IFS=$'\n\t'
5
+
6
+ bundle install
@@ -0,0 +1,36 @@
1
+ module Slatan
2
+ module Affiliation
3
+ module Channels
4
+ @channels = {}
5
+
6
+ class << self
7
+ def get()
8
+ @channels
9
+ end
10
+
11
+ def set(channels)
12
+ @channels = channels
13
+ end
14
+
15
+ ## return id of channel name
16
+ # @param name: channel name
17
+ def get_id(name)
18
+ @channels.each do |channel|
19
+ return channel[:id] if channel[:name] == name
20
+ end
21
+ nil
22
+ end
23
+
24
+ ## return name of channel id
25
+ # @param id: channel id
26
+ def get_name(id)
27
+ @channels.each do |channel|
28
+ return channel[:name] if channel[:id] == id
29
+ end
30
+ nil
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ module Slatan
2
+ module Affiliation
3
+ module Self
4
+ @self = {}
5
+
6
+ class << self
7
+ def get()
8
+ @self
9
+ end
10
+
11
+ def set(celf)
12
+ @self = celf
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Slatan
2
+ module Affiliation
3
+ module SubTeams
4
+ @sub_teams = {}
5
+
6
+ class << self
7
+ def get()
8
+ @sub_teams
9
+ end
10
+
11
+ def set(sub_teams)
12
+ @sub_teams = sub_teams
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Slatan
2
+ module Affiliation
3
+ module Team
4
+ @team = {}
5
+
6
+ class << self
7
+ def get()
8
+ @team
9
+ end
10
+
11
+ def set(team)
12
+ @team = team
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,54 @@
1
+ module Slatan
2
+ module Affiliation
3
+ module Users
4
+ @users = {}
5
+
6
+ class << self
7
+ def get()
8
+ @users
9
+ end
10
+
11
+ def set(users)
12
+ @users = users
13
+ end
14
+
15
+ ## return id of user name
16
+ # @param name: user name
17
+ def get_id(name)
18
+ @users.each do |user|
19
+ return user[:id] if user[:name] == name
20
+ end
21
+ nil
22
+ end
23
+
24
+ ## return name of user id
25
+ # @param id: user id
26
+ def get_name(id)
27
+ @users.each do |user|
28
+ return user[:name] if user[:id] == id
29
+ end
30
+ nil
31
+ end
32
+
33
+ ## check bot or human
34
+ # @return bool true: bot, false: human
35
+ def is_bot?(ident)
36
+ key = if is_id?(ident) then :id else :name end
37
+ @users.each do |user|
38
+ if user[key] == ident
39
+ return user[:is_bot]
40
+ end
41
+ end
42
+
43
+ return false
44
+ end
45
+
46
+ ## check id or name
47
+ # @return bool true: id, false: name
48
+ def is_id?(key)
49
+ key =~ /^[A-Z0-9]+$/
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,15 @@
1
+ require 'slatan/affiliation/self'
2
+ require 'slatan/affiliation/team'
3
+ require 'slatan/affiliation/channels'
4
+ require 'slatan/affiliation/sub_teams'
5
+ require 'slatan/affiliation/users'
6
+
7
+ module Slatan
8
+ module Affiliation
9
+ include Self
10
+ include Team
11
+ include Channels
12
+ include SubTeams
13
+ include Users
14
+ end
15
+ end
@@ -0,0 +1,75 @@
1
+ require 'slatan/spirit'
2
+ require 'logger'
3
+
4
+ module Slatan
5
+ ## Wrapper class of Logger
6
+ class Buttocks
7
+ @@use_log = Spirit.use_log
8
+
9
+ @@color = {
10
+ reset: 0,
11
+ fatal: 31, #red
12
+ error: 31, #red
13
+ warn: 33, #yellow
14
+ info: 32, #green
15
+ debug: 37 #white
16
+ }
17
+
18
+ class << self
19
+ def init()
20
+ if @@use_log
21
+ path = Spirit.log_file_path
22
+ @@log = Logger.new(path)
23
+
24
+ @@log.level = const_level(Spirit.log_level)
25
+ end
26
+ end
27
+
28
+ def debug(msg)
29
+ @@log.debug(msg) if @@use_log
30
+ stdoutput('debug', msg)
31
+ end
32
+
33
+ def info(msg)
34
+ @@log.info(msg) if @@use_log
35
+ stdoutput('info', msg)
36
+ end
37
+
38
+ def warn(msg)
39
+ @@log.warn(msg) if @@use_log
40
+ stdoutput('warn', msg)
41
+ end
42
+
43
+ def error(msg)
44
+ @@log.error(msg) if @@use_log
45
+ stdoutput('error', msg)
46
+ end
47
+
48
+ def fatal(msg)
49
+ @@log.fatal(msg) if @@use_log
50
+ stdoutput('fatal', msg)
51
+ end
52
+
53
+ private
54
+ def const_level(level)
55
+ case level
56
+ when 'debug'
57
+ Logger::DEBUG
58
+ when 'info'
59
+ Logger::INFO
60
+ when 'warn'
61
+ Logger::WARN
62
+ when 'error'
63
+ Logger::ERROR
64
+ else
65
+ Logger::FATAL
66
+ end
67
+ end
68
+
69
+ def stdoutput(level, msg)
70
+ puts "\e[#{@@color[level.to_sym]}m[#{level}] #{DateTime.now.strftime('%Y-%m-%d %H:%M:%S')}: #{msg}"
71
+ print "\e[#{@@color[:reset]}m"
72
+ end
73
+ end
74
+ end
75
+ end
data/lib/slatan/ear.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'active_support/core_ext/object/blank'
2
+
3
+ module Slatan
4
+ ## class to Event Dispatching for concerns
5
+ module Ear
6
+ @concerns = []
7
+
8
+ class << self
9
+ ## register subscriber
10
+ # @param concern subscriber
11
+ # @param options option
12
+ # cond: <Kernel.#lambda> call hear method of concern if cond.call(msg) is true
13
+ def register(concern, options={})
14
+ @concerns << [concern, options[:cond]]
15
+ end
16
+
17
+ ## publish to subscribers
18
+ def hear(msg)
19
+ @concerns.each do |concern, cond|
20
+ if cond.blank? || cond.call(msg)
21
+ concern.hear(msg)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,88 @@
1
+ require 'slatan/spirit'
2
+ require 'slatan/buttocks'
3
+ require 'slatan/affiliation'
4
+
5
+ require 'faye/websocket'
6
+ require 'eventmachine'
7
+ require 'json'
8
+
9
+ module Slatan
10
+ class Heart
11
+ def beat()
12
+ Signal.trap(:INT) { EM.stop }
13
+ Signal.trap(:TERM) { EM.stop }
14
+
15
+ req = Net::HTTP.post_form URI("#{Spirit.slack_api_url}/rtm.start"), token: Spirit.slack_token
16
+ parsed_body = JSON.parse(req.body, symbolize_names: true)
17
+ Buttocks.fatal "failed rtm.start (#{parsed_body[:error]})" if !parsed_body[:ok]
18
+ set_affiliation parsed_body
19
+ ws_url = parsed_body[:url]
20
+ EM.run {
21
+ @ws = Faye::WebSocket::Client.new(ws_url)
22
+
23
+ @ws.on :open do |event|
24
+ Buttocks.info('open')
25
+ end
26
+
27
+ @ws.on :message do |event|
28
+ Buttocks.debug("message: #{event.data}")
29
+ data = JSON.parse(event.data, symbolize_names: true)
30
+ Ear.hear(data) if enable_dispatch data
31
+ end
32
+
33
+ @ws.on :close do |event|
34
+ Buttocks.warn("close: #{event.code}(#{event.reason})")
35
+ @ws = nil
36
+ EM.stop
37
+ end
38
+ }
39
+ end
40
+
41
+ private
42
+
43
+ def set_affiliation(affiliation)
44
+ Affiliation::Self.set affiliation[:self]
45
+ Affiliation::Team.set affiliation[:team]
46
+ Affiliation::SubTeams.set affiliation[:sub_teams]
47
+ Affiliation::Channels.set affiliation[:channels]
48
+ Affiliation::Users.set affiliation[:users]
49
+ end
50
+
51
+ # check whether data enable dispatching
52
+ # @param [hash] received data
53
+ def enable_dispatch(data)
54
+ if Spirit.ignore_bot_reply && is_bot_reply?(data)
55
+ return false
56
+ end
57
+
58
+ if Spirit.ignore_self_message && is_self_message?(data)
59
+ return false
60
+ end
61
+
62
+ true
63
+ end
64
+
65
+ # check whether message is bot retry
66
+ # @return [bool]
67
+ def is_bot_reply?(data)
68
+ if data[:type] != 'message'
69
+ return false
70
+ end
71
+
72
+ if data[:text] =~ /^\s*<@#{Affiliation::Self.get[:id]}>:?.*$/ && Affiliation::Users.is_bot?(data[:user])
73
+ return true
74
+ end
75
+
76
+ false
77
+ end
78
+
79
+ # check whether self message
80
+ # @return [bool]
81
+ def is_self_message?(data)
82
+ if data[:type] == 'message' && data[:user] == Affiliation::Self.get[:id]
83
+ return true
84
+ end
85
+ false
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,19 @@
1
+ module Slatan
2
+ module Mouth
3
+ module Api
4
+ @category = 'api'
5
+
6
+ class << self
7
+ ## @see https://api.slack.com/methods/api.test
8
+ def test(options={})
9
+ send('test', options)
10
+ end
11
+
12
+ private
13
+ def send(method, msg)
14
+ Mouth.send(@category, method, msg)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module Slatan
2
+ module Mouth
3
+ module Auth
4
+ @category = 'auth'
5
+
6
+ class << self
7
+ ## @see https://api.slack.com/methods/auth.test
8
+ def test(options={})
9
+ send('test', options)
10
+ end
11
+
12
+ private
13
+ def send(method, msg)
14
+ Mouth.send(@category, method, msg)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,57 @@
1
+ module Slatan
2
+ module Mouth
3
+ module Channels
4
+ @category = 'channels'
5
+
6
+ class << self
7
+ ## @see https://api.slack.com/methods/channels.history
8
+ def history(channel, options={})
9
+ send('history', {
10
+ channel: channel
11
+ }.merge(options))
12
+ end
13
+
14
+ ## @see https://api.slack.com/methods/channels.info
15
+ def info(channel, options={})
16
+ send('info', {
17
+ channel: channel
18
+ }.merge(options))
19
+ end
20
+
21
+ ## @see https://api.slack.com/methods/channels.list
22
+ def list(options={})
23
+ send('list', options)
24
+ end
25
+
26
+ ## @see https://api.slack.com/methods/channels.mark
27
+ def mark(channel, ts, options={})
28
+ send('mark', {
29
+ channel: channel,
30
+ ts: ts
31
+ }.merge(options))
32
+ end
33
+
34
+ ## @see https://api.slack.com/methods/channels.setPurpose
35
+ def set_purpose(channel, purpose, options={})
36
+ send('setPurpose', {
37
+ channel: channel,
38
+ purpose: purpose
39
+ }.merge(options))
40
+ end
41
+
42
+ ## @see https://api.slack.com/methods/channels.setTopic
43
+ def set_topic(channel, topic, options={})
44
+ send('setTopic', {
45
+ channel: channel,
46
+ topic: topic
47
+ }.merge(options))
48
+ end
49
+
50
+ private
51
+ def send(method, msg)
52
+ Mouth.send(@category, method, msg)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,39 @@
1
+ module Slatan
2
+ module Mouth
3
+ module Chat
4
+ @category = 'chat'
5
+
6
+ class << self
7
+ ## @see https://api.slack.com/methods/chat.postMessage
8
+ def post_message(channel, text, options={})
9
+ send('postMessage', {
10
+ channel: channel,
11
+ text: text
12
+ }.merge(options))
13
+ end
14
+
15
+ ## @see https://api.slack.com/methods/chat.update
16
+ def update(ts, channel, text, options={})
17
+ send('update', {
18
+ ts: ts,
19
+ channel: channel,
20
+ text: text
21
+ }.merge(options))
22
+ end
23
+
24
+ ## @see https://api.slack.com/methods/chat.delete
25
+ def delete(channel, ts, options={})
26
+ send('delete', {
27
+ ts: ts,
28
+ channel: channel
29
+ }.merge(options))
30
+ end
31
+
32
+ private
33
+ def send(method, msg)
34
+ Mouth.send(@category, method, msg)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end