simple_slack 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38ba1647c531367aaa2c74a63c524420e07630dd
4
- data.tar.gz: fb2b7c204be97041cea682716821bef548b8bb9c
3
+ metadata.gz: 13b40759d9819966ef20fb9327dbaf0cf1b89702
4
+ data.tar.gz: 91baadd84d1e5e3be391c356165d65ddb1557ab9
5
5
  SHA512:
6
- metadata.gz: 383abb6063d7a6ec7b822718223eaa0be311183f880832c94a217791e94114ca624ea5a09821f9f591a7fe65684da702f5c829c2a7fc7213037186439af03e88
7
- data.tar.gz: 961fcf8277697f99f18e7eeff34a3bc0fe99662e352d9c01473101a46f7b1a3a254c9946b24bbae48c5b958c0494e0b4c0f3611960f10a69c0c90b1e90f04c0d
6
+ metadata.gz: 55e423c0b8a876d36fe36ecfafc210081a0e2e5f973ac19511c620eaff181fd45f8cc42ceabca3245a1ca6822d6441cfe99da83121325f840626fabd6d7ffb4c
7
+ data.tar.gz: bfb07c56c760a97ad2c22e06e7f8317a1382c86fc1d442baacf2de388cff8687d63ad50eef7790f28dbef228dc9ecd97405f6666ef116c9c840865b2d6432d9d
@@ -1,137 +1,138 @@
1
- class SimpleSlack::Botter
2
- def initialize(token, client)
3
- Slack.configure {|c| c.token = token }
4
- @r_client = Slack.realtime
5
- @client = client
6
- @channel, @text, @user = ["all"] * 3
7
- @responce_channel, @responce_text, @responce_user = nil, nil, "slacker"
8
- end
1
+ module SimpleSlack
2
+ class Botter
3
+ def initialize(token, client)
4
+ Slack.configure {|c| c.token = token }
5
+ @r_client = Slack.realtime
6
+ @client = client
7
+ @channel, @text, @user = ["all"] * 3
8
+ @responce_channel, @responce_text, @responce_user = nil, nil, "slacker"
9
+ end
9
10
 
10
- def set_condition(channel: nil, text: nil, user: nil)
11
- @channel = set_condition_channel(channel) if channel
12
- @user = set_condition_user(user) if user
13
- @text = set_condition_text(text) if text
14
- { channel: @channel, user: @user, text: @text }
15
- end
11
+ def set_condition(channel: nil, text: nil, user: nil)
12
+ @channel = set_condition_channel(channel) if channel
13
+ @user = set_condition_user(user) if user
14
+ @text = set_condition_text(text) if text
15
+ { channel: @channel, user: @user, text: @text }
16
+ end
16
17
 
17
- def set_responce(channel: nil , text: nil, user: nil)
18
- @responce_channel = channel if channel
19
- @responce_text = text if text
20
- @responce_user = user if user
21
- if block_given?
22
- @responce_block = Proc.new(&yield)
18
+ def set_responce(channel: nil , text: nil, user: nil)
19
+ @responce_channel = channel if channel
20
+ @responce_text = text if text
21
+ @responce_user = user if user
22
+ if block_given?
23
+ @responce_block = Proc.new(&yield)
24
+ end
25
+ { channel: @responce_channel, user: @responce_user, text: @responce_text }
23
26
  end
24
- { channel: @responce_channel, user: @responce_user, text: @responce_text }
25
- end
26
27
 
27
- def status
28
- variables = instance_variables.map {|v| v.to_s }
29
- variables.map {|v| { v.to_s => instance_variable_get(v) } }
30
- end
28
+ def status
29
+ variables = instance_variables.map {|v| v.to_s }
30
+ variables.map {|v| { v.to_s => instance_variable_get(v) } }
31
+ end
31
32
 
32
- def start
33
- return p "not set params. need set_responce(channel: , text: , user: )" unless valid_params
34
- @r_client.on :message do |data|
35
- if fit_condition?(data)
36
- send_responce(data)
33
+ def start
34
+ return p "not set params. need set_responce(channel: , text: , user: )" unless valid_params
35
+ @r_client.on :message do |data|
36
+ if fit_condition?(data)
37
+ send_responce(data)
38
+ end
37
39
  end
40
+ puts "client start!"
41
+ @r_client.start
38
42
  end
39
- puts "client start!"
40
- @r_client.start
41
- end
42
43
 
43
- private
44
+ private
44
45
 
45
- def send_responce(data)
46
- if @responce_block.nil?
47
- @client.post.channel(to: @responce_channel, text: @responce_text, name: @responce_user)
48
- else
49
- responce = {}.tap do |res|
50
- res[:user] = @client.get.user(data["user"])[:name] rescue "unknown"
51
- res[:channel] = @client.get.channel(data["channel"])[:name] rescue "unknown"
52
- res[:text] = data["text"]
46
+ def send_responce(data)
47
+ if @responce_block.nil?
48
+ @client.post.channel(to: @responce_channel, text: @responce_text, name: @responce_user)
49
+ else
50
+ responce = {}.tap do |res|
51
+ res[:user] = @client.get.user(data["user"])[:name] rescue "unknown"
52
+ res[:channel] = @client.get.channel(data["channel"])[:name] rescue "unknown"
53
+ res[:text] = data["text"]
54
+ end
55
+ @responce_block.call(data, responce)
53
56
  end
54
- @responce_block.call(data, responce)
55
57
  end
56
- end
57
58
 
58
- def fit_condition?(data)
59
- fit_params?(@channel, data["channel"]) && fit_params?(@user, data["user"]) && fit_params?(@text, data["text"])
60
- end
59
+ def fit_condition?(data)
60
+ fit_params?(@channel, data["channel"]) && fit_params?(@user, data["user"]) && fit_params?(@text, data["text"])
61
+ end
61
62
 
62
- def fit_params?(set_params, res_param)
63
- return true if set_params == "all"
64
- case set_params
65
- when String
66
- set_params == res_param
67
- when Array
68
- set_params.any? {|param| fit_params?(param, res_param) }
69
- when Regexp
70
- set_params =~ res_param
71
- when Hash
72
- set_params[:id] == res_param
73
- else
74
- false
63
+ def fit_params?(set_params, res_param)
64
+ return true if set_params == "all"
65
+ case set_params
66
+ when String
67
+ set_params == res_param
68
+ when Array
69
+ set_params.any? {|param| fit_params?(param, res_param) }
70
+ when Regexp
71
+ set_params =~ res_param
72
+ when Hash
73
+ set_params[:id] == res_param
74
+ else
75
+ false
76
+ end
75
77
  end
76
- end
77
78
 
78
- def set_condition_channel(channel)
79
- return nil if channel.nil?
80
- return "all" if channel.to_s == "all" rescue false
81
- case channel
82
- when String
83
- @client.get.channel(channel)
84
- when Array
85
- channel.map {|ch| @client.get.channel(ch.to_s) }
86
- when Hash
87
- channel
88
- when Symbol
89
- @client.get.channel(channel.to_s)
90
- else
91
- "error: invalid Object"
79
+ def set_condition_channel(channel)
80
+ return nil if channel.nil?
81
+ return "all" if channel.to_s == "all" rescue false
82
+ case channel
83
+ when String
84
+ @client.get.channel(channel)
85
+ when Array
86
+ channel.map {|ch| @client.get.channel(ch.to_s) }
87
+ when Hash
88
+ channel
89
+ when Symbol
90
+ @client.get.channel(channel.to_s)
91
+ else
92
+ "error: invalid Object"
93
+ end
92
94
  end
93
- end
94
95
 
95
- def set_condition_user(user)
96
- return nil if user.nil?
97
- return "all" if user.to_s == "all" rescue false
98
- case user
99
- when String
100
- @client.get.user(user)
101
- when Array
102
- user.map {|u| set_condition_user(u) }
103
- when Hash
104
- user
105
- when Symbol
106
- @client.get.user(user.to_s)
107
- else
108
- "error: invalid Object"
96
+ def set_condition_user(user)
97
+ return nil if user.nil?
98
+ return "all" if user.to_s == "all" rescue false
99
+ case user
100
+ when String
101
+ @client.get.user(user)
102
+ when Array
103
+ user.map {|u| set_condition_user(u) }
104
+ when Hash
105
+ user
106
+ when Symbol
107
+ @client.get.user(user.to_s)
108
+ else
109
+ "error: invalid Object"
110
+ end
109
111
  end
110
- end
111
112
 
112
- def set_condition_text(text)
113
- return nil if text.nil?
114
- return "all" if text.to_s == "all" rescue false
115
- case text
116
- when String
117
- text.gsub(/\p{blank}/,"\s").strip.split("\s")
118
- when Array
119
- text.map {|t| set_condition_text(t) }
120
- when Regexp
121
- text
122
- else
123
- text.to_s rescue nil
113
+ def set_condition_text(text)
114
+ return nil if text.nil?
115
+ return "all" if text.to_s == "all" rescue false
116
+ case text
117
+ when String
118
+ text.gsub(/\p{blank}/,"\s").strip.split("\s")
119
+ when Array
120
+ text.map {|t| set_condition_text(t) }
121
+ when Regexp
122
+ text
123
+ else
124
+ text.to_s rescue nil
125
+ end
124
126
  end
125
- end
126
127
 
127
- def set_responce_channel(channel)
128
- set_condition_channel(channel)
129
- end
128
+ def set_responce_channel(channel)
129
+ set_condition_channel(channel)
130
+ end
130
131
 
131
- def valid_params
132
- return true if @responce_block
133
- variables = ["@responce_channel", "@responce_text"]
134
- variables.none? {|v| instance_variable_get(v).nil? }
132
+ def valid_params
133
+ return true if @responce_block
134
+ variables = ["@responce_channel", "@responce_text"]
135
+ variables.none? {|v| instance_variable_get(v).nil? }
136
+ end
135
137
  end
136
-
137
138
  end
@@ -4,26 +4,28 @@ require 'simple_slack/poster'
4
4
  require 'simple_slack/botter'
5
5
  require 'simple_slack/toggl'
6
6
 
7
- class SimpleSlack::Client
8
- def initialize(token = ENV['SLACK_API_TOKEN'])
9
- @token = token
10
- Slack.configure { |config| config.token = @token }
11
- @slack = Slack.client
12
- end
7
+ module SimpleSlack
8
+ class Client
9
+ def initialize(token = ENV['SLACK_API_TOKEN'])
10
+ @token = token
11
+ Slack.configure { |config| config.token = @token }
12
+ @slack = Slack.client
13
+ end
13
14
 
14
- def get
15
- @getter ||= SimpleSlack::Getter.new(@slack)
16
- end
15
+ def get
16
+ @getter ||= SimpleSlack::Getter.new(@slack)
17
+ end
17
18
 
18
- def post
19
- @poster ||= SimpleSlack::Poster.new(@slack, self)
20
- end
19
+ def post
20
+ @poster ||= SimpleSlack::Poster.new(@slack, self)
21
+ end
21
22
 
22
- def bot
23
- @botter ||= SimpleSlack::Botter.new(@token, self)
24
- end
23
+ def bot
24
+ @botter ||= SimpleSlack::Botter.new(@token, self)
25
+ end
25
26
 
26
- def toggl(toggl_api_token = ENV['TOGGL_API_TOKEN'])
27
- @toggl ||= SimpleSlack::Toggl.new(toggl_api_token, self)
27
+ def toggl(toggl_api_token = ENV['TOGGL_API_TOKEN'])
28
+ @toggl ||= SimpleSlack::Toggl.new(toggl_api_token, self)
29
+ end
28
30
  end
29
31
  end
@@ -1,111 +1,111 @@
1
- class SimpleSlack::Getter
2
- def initialize(slack)
3
- @slack = slack
4
- end
5
-
6
- # use options for
7
- # :is_channel, :creator, :members, :topic, :purpose, :num_members, etc
8
- def channels(options = [])
9
- channels = @slack.channels_list
10
- channels["channels"].map do |channel|
11
- add_params = options.empty? ? {} : options_to_hash(options, channel)
12
- { id: channel["id"], name: channel["name"] }.merge(add_params)
13
- end.sort_by {|ch| ch[:name] }
14
- end
15
-
16
- def channel(key, options = [])
17
- key.to_s =~ /\AC.{8}\Z/ ? key_id = key : key_name = key
18
- channel_list = channels(options)
19
- if key_id
20
- channel_list.find{|ch| ch[:id] == key_id }
21
- elsif key_name
22
- channel_list.find{|ch| ch[:name] == key_name }
1
+ module SimpleSlack
2
+ class Getter
3
+ def initialize(slack)
4
+ @slack = slack
23
5
  end
24
- end
25
6
 
26
- # use options for
27
- # :real_name, :is_admin, :is_bot, etc...
28
- def users(options = [])
29
- users = @slack.users_list
30
- users["members"].map do |user|
31
- add_params = options.empty? ? {} : options_to_hash(options, user)
32
- { id: user["id"], name: user["name"] }.merge(add_params)
33
- end.sort_by {|ch| ch[:name] }
34
- end
7
+ # use options for
8
+ # :is_channel, :creator, :members, :topic, :purpose, :num_members, etc
9
+ def channels(options = [])
10
+ channels = @slack.channels_list
11
+ channels["channels"].map do |channel|
12
+ add_params = options.empty? ? {} : options_to_hash(options, channel)
13
+ { id: channel["id"], name: channel["name"] }.merge(add_params)
14
+ end.sort_by {|ch| ch[:name] }
15
+ end
35
16
 
36
- def user(key, options = [])
37
- key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
38
- user_list = users(options)
39
- if key_id
40
- user_list.find{|user| user[:id] == key_id }
41
- elsif key_name
42
- user_list.find{|user| user[:name] == key_name }
17
+ def channel(key, options = [])
18
+ key.to_s =~ /\AC.{8}\Z/ ? key_id = key : key_name = key
19
+ channel_list = channels(options)
20
+ if key_id
21
+ channel_list.find{|ch| ch[:id] == key_id }
22
+ elsif key_name
23
+ channel_list.find{|ch| ch[:name] == key_name }
24
+ end
43
25
  end
44
- end
45
26
 
46
- # use options for
47
- # :image_24, :image_32, :image_48, image_72, etc...
48
- def images(options = [])
49
- users = @slack.users_list
50
- users["members"].map do |user|
51
- add_params = options.empty? ? {} : options_to_hash(options, user["profile"])
52
- { id: user["id"], name: user["name"], image: user["profile"]["image_24"] }.merge(add_params)
27
+ # use options for
28
+ # :real_name, :is_admin, :is_bot, etc...
29
+ def users(options = [])
30
+ users = @slack.users_list
31
+ users["members"].map do |user|
32
+ add_params = options.empty? ? {} : options_to_hash(options, user)
33
+ { id: user["id"], name: user["name"] }.merge(add_params)
34
+ end.sort_by {|ch| ch[:name] }
53
35
  end
54
- end
55
36
 
56
- def image(key, options = [])
57
- key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
58
- image_list = images(options)
59
- if key_id
60
- image_list.find{|user| user[:id] == key_id }
61
- elsif key_name
62
- image_list.find{|user| user[:name] == key_name }
37
+ def user(key, options = [])
38
+ key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
39
+ user_list = users(options)
40
+ if key_id
41
+ user_list.find{|user| user[:id] == key_id }
42
+ elsif key_name
43
+ user_list.find{|user| user[:name] == key_name }
44
+ end
63
45
  end
64
- end
65
46
 
66
- # use options for
67
- # :created, :is_im, :is_org_shared, :is_user_deleted
68
- def ims(options = [])
69
- im_list = @slack.im_list
70
- im_list["ims"].map do |im|
71
- im_user = if im["user"] == "USLACKBOT"
72
- { id: im["user"], name: "slackbot" }
73
- else
74
- user(im["user"])
75
- end
76
- add_params = options.empty? ? {} : options_to_hash(options, im)
77
- { id: im["id"], user: im_user }.merge(add_params)
47
+ # use options for
48
+ # :image_24, :image_32, :image_48, image_72, etc...
49
+ def images(options = [])
50
+ users = @slack.users_list
51
+ users["members"].map do |user|
52
+ add_params = options.empty? ? {} : options_to_hash(options, user["profile"])
53
+ { id: user["id"], name: user["name"], image: user["profile"]["image_24"] }.merge(add_params)
54
+ end
78
55
  end
79
- end
80
56
 
81
- def im(key, options = [])
82
- key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
83
- im_list = ims(options)
84
- if key_id
85
- im_list.find{|im| im[:user][:id] == key_id }
86
- elsif key_name
87
- im_list.find{|im| im[:user][:name] == key_name }
57
+ def image(key, options = [])
58
+ key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
59
+ image_list = images(options)
60
+ if key_id
61
+ image_list.find{|user| user[:id] == key_id }
62
+ elsif key_name
63
+ image_list.find{|user| user[:name] == key_name }
64
+ end
88
65
  end
89
- end
90
66
 
91
- def chats
92
- "yet"
93
- end
67
+ # use options for
68
+ # :created, :is_im, :is_org_shared, :is_user_deleted
69
+ def ims(options = [])
70
+ im_list = @slack.im_list
71
+ im_list["ims"].map do |im|
72
+ im_user = if im["user"] == "USLACKBOT"
73
+ { id: im["user"], name: "slackbot" }
74
+ else
75
+ user(im["user"])
76
+ end
77
+ add_params = options.empty? ? {} : options_to_hash(options, im)
78
+ { id: im["id"], user: im_user }.merge(add_params)
79
+ end
80
+ end
94
81
 
95
- def chat
96
- "yet"
97
- end
82
+ def im(key, options = [])
83
+ key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
84
+ im_list = ims(options)
85
+ if key_id
86
+ im_list.find{|im| im[:user][:id] == key_id }
87
+ elsif key_name
88
+ im_list.find{|im| im[:user][:name] == key_name }
89
+ end
90
+ end
98
91
 
99
- private
92
+ def chats
93
+ "yet"
94
+ end
100
95
 
101
- def options_to_hash(options, type = {})
102
- marged_option = {}
103
- options.each do |op|
104
- marged_option.merge!({ op.to_sym => type[op.to_s] })
96
+ def chat
97
+ "yet"
105
98
  end
106
99
 
107
- marged_option
108
- end
100
+ private
109
101
 
102
+ def options_to_hash(options, type = {})
103
+ marged_option = {}
104
+ options.each do |op|
105
+ marged_option.merge!({ op.to_sym => type[op.to_s] })
106
+ end
110
107
 
108
+ marged_option
109
+ end
110
+ end
111
111
  end
@@ -1,67 +1,69 @@
1
- class SimpleSlack::Poster
2
- def initialize(slack, simple_slack)
3
- @slack = slack
4
- @simple_slack = simple_slack
5
- end
1
+ module SimpleSlack
2
+ class Poster
3
+ def initialize(slack, simple_slack)
4
+ @slack = slack
5
+ @simple_slack = simple_slack
6
+ end
6
7
 
7
- def channels(to: , text: , name: "slacker", **options)
8
- to.all? do |t|
9
- id = convert_channel_to_id(t.to_s)
8
+ def channels(to: , text: , name: "slacker", **options)
9
+ to.all? do |t|
10
+ id = convert_channel_to_id(t.to_s)
11
+ send_chat({ username: name, channel: id, text: text }.merge(options))
12
+ end
13
+ end
14
+
15
+ def channel(to: , text: , name: "slacker", **options)
16
+ id = convert_channel_to_id(to.to_s)
10
17
  send_chat({ username: name, channel: id, text: text }.merge(options))
11
18
  end
12
- end
13
19
 
14
- def channel(to: , text: , name: "slacker", **options)
15
- id = convert_channel_to_id(to.to_s)
16
- send_chat({ username: name, channel: id, text: text }.merge(options))
17
- end
20
+ def user(to: , text: , name: "slacker")
21
+ "yet"
22
+ end
18
23
 
19
- def user(to: , text: , name: "slacker")
20
- "yet"
21
- end
24
+ def chat(channel: nil, user: nil, text: , name: "slacker", **options)
25
+ if channel
26
+ self.channel({ to: channel, text: text, name: name }.merge(options))
27
+ elsif user
28
+ "yet"
29
+ end
30
+ end
22
31
 
23
- def chat(channel: nil, user: nil, text: , name: "slacker", **options)
24
- if channel
25
- self.channel({ to: channel, text: text, name: name }.merge(options))
26
- elsif user
27
- "yet"
32
+ def ims(to: , text: , name: "slacker", **options)
33
+ to.all? do |t|
34
+ id = convert_im_to_id(t.to_s)
35
+ send_chat({ username: name, channel: id, text: text }.merge(options))
36
+ end
28
37
  end
29
- end
30
38
 
31
- def ims(to: , text: , name: "slacker", **options)
32
- to.all? do |t|
33
- id = convert_im_to_id(t.to_s)
39
+ def im(to: , text: , name: "slacker", **options)
40
+ id = convert_im_to_id(to.to_s)
34
41
  send_chat({ username: name, channel: id, text: text }.merge(options))
35
42
  end
36
- end
37
43
 
38
- def im(to: , text: , name: "slacker", **options)
39
- id = convert_im_to_id(to.to_s)
40
- send_chat({ username: name, channel: id, text: text }.merge(options))
41
- end
44
+ private
42
45
 
43
- private
44
-
45
- def send_chat(username: , channel: , text: , icon_emoji: ":ghost:", **options)
46
- result = @slack.chat_postMessage({ username: username, channel: channel, text: text, icon_emoji: icon_emoji }.merge(options))
47
- result["ok"]
48
- end
46
+ def send_chat(username: , channel: , text: , icon_emoji: ":ghost:", **options)
47
+ result = @slack.chat_postMessage({ username: username, channel: channel, text: text, icon_emoji: icon_emoji }.merge(options))
48
+ result["ok"]
49
+ end
49
50
 
50
- def convert_channel_to_id(key)
51
- channel = @simple_slack.get.channel(key)
52
- if channel
53
- channel[:id]
54
- else
55
- raise "チャンネルが見つかりませんでした。"
51
+ def convert_channel_to_id(key)
52
+ channel = @simple_slack.get.channel(key)
53
+ if channel
54
+ channel[:id]
55
+ else
56
+ raise "チャンネルが見つかりませんでした。"
57
+ end
56
58
  end
57
- end
58
59
 
59
- def convert_im_to_id(key)
60
- im = @simple_slack.get.im(key)
61
- if im
62
- im[:id]
63
- else
64
- raise "IMが見つかりませんでした。"
60
+ def convert_im_to_id(key)
61
+ im = @simple_slack.get.im(key)
62
+ if im
63
+ im[:id]
64
+ else
65
+ raise "IMが見つかりませんでした。"
66
+ end
65
67
  end
66
68
  end
67
69
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleSlack
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_slack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - toririn