simple_slack 0.2.5 → 0.2.6

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: d8f344b296820615de0402a7f345674d3d84665a
4
- data.tar.gz: 71f732e48ecdcb47064905ba57e5bcb601ff0629
3
+ metadata.gz: ce3b8f3eb317bfeb9a76992379ed8b1f76a3d744
4
+ data.tar.gz: 28a3b0188ba80a3558464ff196311017f7789fa9
5
5
  SHA512:
6
- metadata.gz: d326bce4004b25366cc9cf4d0b357f671b6350a06a55244475112d2327c948f0b2e852b156fdc4b4d962c76ace83da1189c4f999eef57cf5c1cf5df2604ea4ee
7
- data.tar.gz: fca6923f149dda30718fd0f025c732d28e7177eacf91af321295f073c40e63f1a06395ae16b01da75a719ef3cfd28b38d0ecc61c85b147e3909ae7aeb5149d71
6
+ metadata.gz: cf83868cb60a91e183058da8add0743aa948e0a002ee6dcc07fc71f688c12fb5ebd696bd2cc43be5bcb2e04137b1c71ed93664ce03f15781cfdc3b67169746bc
7
+ data.tar.gz: 0f39c119c8e1d9c23d84efdf3ca35ff184721436f448ae3d0e0ecbabe964b36d4054266dc76b802e38597191057c8e8fe4e9a086ff54f15150d62848caa9c22e
@@ -11,6 +11,7 @@ class SimpleSlack::Botter
11
11
  @channel = set_condition_channel(channel) if channel
12
12
  @user = set_condition_user(user) if user
13
13
  @text = set_condition_text(text) if text
14
+ { channel: @channel, user: @user, text: @text }
14
15
  end
15
16
 
16
17
  def set_responce(channel: nil , text: nil, user: nil)
@@ -20,6 +21,7 @@ class SimpleSlack::Botter
20
21
  if block_given?
21
22
  @responce_block = Proc.new(&yield)
22
23
  end
24
+ { channel: @responce_channel, user: @responce_user, text: @responce_text }
23
25
  end
24
26
 
25
27
  def status
@@ -74,7 +76,8 @@ class SimpleSlack::Botter
74
76
  end
75
77
 
76
78
  def set_condition_channel(channel)
77
- return nil if channel.nil?
79
+ return nil if channel.nil?
80
+ return "all" if channel.to_s == "all" rescue false
78
81
  case channel
79
82
  when String
80
83
  @client.get.channel(channel)
@@ -90,7 +93,8 @@ class SimpleSlack::Botter
90
93
  end
91
94
 
92
95
  def set_condition_user(user)
93
- return nil if user.nil?
96
+ return nil if user.nil?
97
+ return "all" if user.to_s == "all" rescue false
94
98
  case user
95
99
  when String
96
100
  @client.get.user(user)
@@ -106,7 +110,8 @@ class SimpleSlack::Botter
106
110
  end
107
111
 
108
112
  def set_condition_text(text)
109
- return nil if text.nil?
113
+ return nil if text.nil?
114
+ return "all" if text.to_s == "all" rescue false
110
115
  case text
111
116
  when String
112
117
  text.gsub(/\p{blank}/,"\s").strip.split("\s")
@@ -3,72 +3,89 @@ class SimpleSlack::Getter
3
3
  @slack = slack
4
4
  end
5
5
 
6
- def channels
6
+ # use options for
7
+ # :is_channel, :creator, :members, :topic, :purpose, :num_members, etc
8
+ def channels(options = [])
7
9
  channels = @slack.channels_list
8
10
  channels["channels"].map do |channel|
9
- { id: channel["id"], name: channel["name"] }
11
+ add_params = options.empty? ? {} : options_to_hash(options, channel)
12
+ { id: channel["id"], name: channel["name"] }.merge(add_params)
10
13
  end.sort_by {|ch| ch[:name] }
11
14
  end
12
15
 
13
- def channel(key)
16
+ def channel(key, options = [])
14
17
  key.to_s =~ /\AC.{8}\Z/ ? key_id = key : key_name = key
15
- @channel_list ||= channels
18
+ channel_list = channels(options)
16
19
  if key_id
17
- @channel_list.find{|ch| ch[:id] == key_id }
20
+ channel_list.find{|ch| ch[:id] == key_id }
18
21
  elsif key_name
19
- @channel_list.find{|ch| ch[:name] == key_name }
20
- else
21
- "not found"
22
+ channel_list.find{|ch| ch[:name] == key_name }
22
23
  end
23
24
  end
24
25
 
25
- def users
26
+ # use options for
27
+ # :real_name, :is_admin, :is_bot, etc...
28
+ def users(options = [])
26
29
  users = @slack.users_list
27
30
  users["members"].map do |user|
28
- { id: user["id"], name: user["name"] }
31
+ add_params = options.empty? ? {} : options_to_hash(options, user)
32
+ { id: user["id"], name: user["name"] }.merge(add_params)
29
33
  end.sort_by {|ch| ch[:name] }
30
34
  end
31
35
 
32
- def user(key)
36
+ def user(key, options = [])
33
37
  key.to_s =~ /\AU.{8}\Z/ ? key_id = key : key_name = key
34
- @user_list ||= users
38
+ user_list = users(options)
35
39
  if key_id
36
- @user_list.find{|user| user[:id] == key_id }
40
+ user_list.find{|user| user[:id] == key_id }
37
41
  elsif key_name
38
- @user_list.find{|user| user[:name] == key_name }
39
- else
40
- "not found"
42
+ user_list.find{|user| user[:name] == key_name }
41
43
  end
42
44
  end
43
45
 
44
- def images
45
- image_list = users["members"].map do |user|
46
- { id: user["id"], image: user["profile"]["image_24"] }
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)
47
53
  end
48
54
  end
49
55
 
50
- def image(id)
51
- @image_list ||= images
52
- @image_list.find{|user| user[:id] == id }
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 }
63
+ end
53
64
  end
54
65
 
55
- def ims
66
+ # use options for
67
+ # :created, :is_im, :is_org_shared, :is_user_deleted
68
+ def ims(options = [])
56
69
  im_list = @slack.im_list
57
- im_list["ims"].map do |info|
58
- im_user = if info["user"] == "USLACKBOT"
59
- { id: info["user"], name: "owner" }
70
+ im_list["ims"].map do |im|
71
+ im_user = if im["user"] == "USLACKBOT"
72
+ { id: im["user"], name: "slackbot" }
60
73
  else
61
- user(info["user"])
74
+ user(im["user"])
62
75
  end
63
- { id: info["id"], user: im_user }
76
+ add_params = options.empty? ? {} : options_to_hash(options, im)
77
+ { id: im["id"], user: im_user }.merge(add_params)
64
78
  end
65
79
  end
66
80
 
67
- def im(key)
68
- im_user = user(key)
69
- @im_list ||= ims
70
-
71
- @im_list.find{|info| info[:user][:id] == im_user[:id] }
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 }
88
+ end
72
89
  end
73
90
 
74
91
  def chats
@@ -78,4 +95,17 @@ class SimpleSlack::Getter
78
95
  def chat
79
96
  "yet"
80
97
  end
98
+
99
+ private
100
+
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] })
105
+ end
106
+
107
+ marged_option
108
+ end
109
+
110
+
81
111
  end
@@ -4,42 +4,50 @@ class SimpleSlack::Poster
4
4
  @simple_slack = simple_slack
5
5
  end
6
6
 
7
- def channel(to: , text: , name: "slacker")
8
- to.to_s =~ /\AC0.{7}\Z/ ? id = to : id = convert_channel(to)
9
- send_chat(username: name, channel: id, text: text)
7
+ def channel(to: , text: , name: "slacker", **options)
8
+ id = convert_channel_to_id(to.to_s)
9
+ send_chat({ username: name, channel: id, text: text }.merge(options))
10
10
  end
11
11
 
12
12
  def user(to: , text: , name: "slacker")
13
13
  "yet"
14
14
  end
15
15
 
16
- def chat(channel: nil, user: nil, text: , name: "slacker")
16
+ def chat(channel: nil, user: nil, text: , name: "slacker", **options)
17
17
  if channel
18
- self.channel(to: channel, text: text, name: name)
18
+ self.channel({ to: channel, text: text, name: name }.merge(options))
19
19
  elsif user
20
20
  "yet"
21
21
  end
22
22
  end
23
23
 
24
24
  def im(to: , text: , name: "slacker")
25
- to.to_s =~ /\AD0.{7}\Z/ ? id = to : id = convert_im(to)
25
+ id = convert_im_to_id(to.to_s)
26
26
  send_chat(username: name, channel: id, text: text)
27
27
  end
28
28
 
29
29
  private
30
30
 
31
- def send_chat(username: , channel: , text: , icon_emoji: ":ghost:")
32
- result = @slack.chat_postMessage(username: username, channel: channel, text: text, icon_emoji: icon_emoji)
31
+ def send_chat(username: , channel: , text: , icon_emoji: ":ghost:", **options)
32
+ result = @slack.chat_postMessage({ username: username, channel: channel, text: text, icon_emoji: icon_emoji }.merge(options))
33
33
  result["ok"]
34
34
  end
35
35
 
36
- def convert_channel(name)
37
- channel = @simple_slack.get.channel(name)
38
- channel[:id]
36
+ def convert_channel_to_id(key)
37
+ channel = @simple_slack.get.channel(key)
38
+ if channel
39
+ channel[:id]
40
+ else
41
+ raise "チャンネルが見つかりませんでした。"
42
+ end
39
43
  end
40
44
 
41
- def convert_im(name)
42
- im = @simple_slack.get.im(name)
43
- im[:id]
45
+ def convert_im_to_id(key)
46
+ im = @simple_slack.get.im(key)
47
+ if im
48
+ im[:id]
49
+ else
50
+ raise "IMが見つかりませんでした。"
51
+ end
44
52
  end
45
53
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleSlack
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_slack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - toririn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-22 00:00:00.000000000 Z
11
+ date: 2016-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler