immosquare-slack 0.1.6 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f225c081bd5414be15eb18de6283012de0a0763266aa7ad69df66bb42a09104
4
- data.tar.gz: 576e4573f9cd77b62ee364231f21e1ba38e00367d7f2bd4da567fa36b1ffb4f2
3
+ metadata.gz: 622166a109e7d56741e24a45e19fb139940e467aaa93713fdbf9ee57800f8d00
4
+ data.tar.gz: a7603ceee1e8467dd39b48441b201a5fea6938a3a5a3308bf7855066cdce8059
5
5
  SHA512:
6
- metadata.gz: 180c9241ecb12c5784a9d287087f37ef22fc58f1126e48107d51dcbeb92dc17efe769fdab3d1fae19f083d1c6e88bc26bab4712d09e3650543933eb14adf0714
7
- data.tar.gz: 6b3cbd7e068f7d0cc3575c3403104c739e18a167d02549be2c11c2f0a9daa770a4df218671b3f0edb527237ee49d2faee46cfd7892536ad42257fd90269743a9
6
+ metadata.gz: cd8138d1bca5e66c75777ccdee7752c8530fa38ae5aa0e8c737679a25994927274fe6eb87c5840ca936c77d5a562c5d74836f86b665e233a6a66b50fd901e495
7
+ data.tar.gz: 3db28b76da6504ef0b8ee7f99309342b860071430ec32843fc886063cf59ffaa117fe4fedfffc252c010a62607a40580dd4f4688784fecb120973155cc790120
@@ -1,10 +1,15 @@
1
1
  module ImmosquareSlack
2
2
  module Channel
3
3
  extend SharedMethods
4
+
5
+ GENERAL_CHANNEL = "general".freeze
6
+
4
7
  class << self
5
8
 
6
9
  ##============================================================##
7
- ## Pour récupérer la liste des channels
10
+ ## Fetches the list of channels (public + private, including
11
+ ## archived). Memoized per process — call sites that need a
12
+ ## fresh list should reset @list_channels explicitly.
8
13
  ##============================================================##
9
14
  def list_channels
10
15
  @list_channels ||= begin
@@ -17,14 +22,29 @@ module ImmosquareSlack
17
22
  end
18
23
 
19
24
  ##============================================================##
20
- ## Pour poster un message dans un channel
25
+ ## Posts a message to a channel.
26
+ ##
27
+ ## `channel_name` and `bot_name` are optional keyword args:
28
+ ## if nil, they fall back to the global config
29
+ ## `ImmosquareSlack.configuration.default_channel` and
30
+ ## `default_bot_name`. Lets apps that always notify the same
31
+ ## channel avoid repeating the value at every call site.
32
+ ##
33
+ ## If no channel can be resolved after fallback, raises an
34
+ ## ArgumentError immediately (clearer than the cryptic error
35
+ ## returned by the Slack API).
21
36
  ##============================================================##
22
- def post_message(channel_name, text, notify: nil, notify_text: nil, bot_name: nil, notify_general_if_invalid_channel: true)
37
+ def post_message(text, channel_name: nil, notify: nil, notify_text: nil, bot_name: nil, notify_general_if_invalid_channel: true)
38
+ channel_name ||= ImmosquareSlack.configuration.default_channel
39
+ bot_name ||= ImmosquareSlack.configuration.default_bot_name
40
+
41
+ raise(ArgumentError, "channel_name is required (or set ImmosquareSlack.configuration.default_channel)") if channel_name.nil?
42
+
23
43
  channel_id = get_channel_id_by_name(channel_name)
24
44
 
25
45
  if channel_id.nil?
26
46
  text = "immosquare-slack missing channel *#{channel_name}*\nmessage:\n#{text}"
27
- return post_message("general", text, :notify => :channel, :notify_text => "", :bot_name => bot_name, :notify_general_if_invalid_channel => false) if channel_name != "general" && notify_general_if_invalid_channel
47
+ return post_message(text, :channel_name => GENERAL_CHANNEL, :notify => :channel, :notify_text => "", :bot_name => bot_name, :notify_general_if_invalid_channel => false) if channel_name != GENERAL_CHANNEL && notify_general_if_invalid_channel
28
48
 
29
49
  raise("channel '#{channel_name}' not found on slack")
30
50
  end
@@ -43,32 +63,41 @@ module ImmosquareSlack
43
63
  make_slack_api_call(url, :method => :post, :body => body)
44
64
  end
45
65
 
46
-
47
-
48
66
  private
49
67
 
50
68
  ##============================================================##
51
- ## Pour récupérer l'id d'un channel en fonction de son nom
69
+ ## Resolves a channel id from its name. "general" is matched
70
+ ## via the `is_general` flag because workspaces can rename
71
+ ## their general channel.
52
72
  ##============================================================##
53
73
  def get_channel_id_by_name(channel_name)
54
74
  channels = list_channels
55
- channel = channels.find {|c| channel_name == "general" ? c["is_general"] == true : (c["name"] == channel_name && c["is_archived"] == false) }
56
- channel.present? ? channel["id"] : nil
75
+ channel = channels.find do |c|
76
+ if channel_name == GENERAL_CHANNEL
77
+ c["is_general"]
78
+ else
79
+ c["name"] == channel_name && c["is_archived"] == false
80
+ end
81
+ end
82
+ channel.nil? ? nil : channel["id"]
57
83
  end
58
84
 
59
85
  ##============================================================##
60
- ## Pour récupérer la liste des membres d'un channel
86
+ ## Fetches the list of members of a channel.
61
87
  ##============================================================##
62
88
  def get_channel_members(channel_id)
63
89
  fetch_paginated_data("https://slack.com/api/conversations.members", "members", {:channel => channel_id})
64
90
  end
65
91
 
66
92
  ##============================================================##
67
- ## Méthode récupérant les membres d'un channel et les notifier
68
- ## sur le message
69
- ## on ne peut pas utilser in? si on veut que la gem soit
70
- ## compatible avec ruby (sans rails)
71
- ## pareil pour present?
93
+ ## Builds the notification prefix prepended to the message
94
+ ## body. `notify` may be:
95
+ ## - an Array of emails: mentions matching workspace members
96
+ ## - :all : mentions every member of the channel
97
+ ## - :channel/:here/:everyone : Slack-wide broadcast tokens
98
+ ##
99
+ ## Note: avoids `in?` and `present?` so the gem stays usable
100
+ ## from plain Ruby (no Rails / ActiveSupport required).
72
101
  ##============================================================##
73
102
  def build_notification_text(channel_id, notify, text = "Hello")
74
103
  final =
@@ -87,8 +116,9 @@ module ImmosquareSlack
87
116
  "<!everyone>"
88
117
  end
89
118
 
119
+ return nil if final.to_s.empty?
90
120
 
91
- "#{text} #{final}\n" if !final.to_s.empty?
121
+ text.empty? ? "#{final}\n" : "#{text} #{final}\n"
92
122
  end
93
123
 
94
124
  end
@@ -1,10 +1,12 @@
1
1
  module ImmosquareSlack
2
2
  class Configuration
3
3
 
4
- attr_accessor :slack_api_token_bot
4
+ attr_accessor :slack_api_token_bot, :default_channel, :default_bot_name
5
5
 
6
6
  def initialize
7
7
  @slack_api_token_bot = nil
8
+ @default_channel = nil
9
+ @default_bot_name = nil
8
10
  end
9
11
 
10
12
  end
@@ -6,7 +6,7 @@ module ImmosquareSlack
6
6
  private
7
7
 
8
8
  ##============================================================##
9
- ## On récupère tous les résultats avec une loop sur le cursor
9
+ ## Fetches all results by looping on the Slack API cursor.
10
10
  ##============================================================##
11
11
  def fetch_paginated_data(url, data_key, extra_query = {})
12
12
  items = []
@@ -24,9 +24,9 @@ module ImmosquareSlack
24
24
  end
25
25
 
26
26
  ##============================================================##
27
- ## On fait le call à l'API Slack. On raise le message entier
28
- ## en string si la réponse n'est pas "ok" ou si la réponse
29
- ## n'est pas un JSON valide.
27
+ ## Calls the Slack API. Raises the full response body as a
28
+ ## string if the response is not "ok" or if the body is not
29
+ ## valid JSON.
30
30
  ##============================================================##
31
31
  def make_slack_api_call(url, method: :get, query: {}, body: nil)
32
32
  options = {
@@ -37,15 +37,15 @@ module ImmosquareSlack
37
37
  }
38
38
 
39
39
  ##============================================================##
40
- ## On crée les options en fonction du cas de figure
40
+ ## Builds the request options based on the call type.
41
41
  ##============================================================##
42
42
  options[:query] = query if query.any?
43
43
  options[:body] = body.to_json if body
44
44
 
45
45
  ##============================================================##
46
- ## On send la requête et on parse la réponse
46
+ ## Sends the request and parses the response.
47
47
  ##============================================================##
48
- response = HTTParty.send(method, url, options)
48
+ response = HTTParty.public_send(method, url, options)
49
49
  parsed_response = JSON.parse(response.body)
50
50
  raise(parsed_response.to_json) if !parsed_response["ok"]
51
51
 
@@ -1,3 +1,3 @@
1
1
  module ImmosquareSlack
2
- VERSION = "0.1.6".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-slack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - immosquare
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-12-11 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: httparty
@@ -45,7 +44,6 @@ homepage: https://github.com/immosquare/immosquare-slack
45
44
  licenses:
46
45
  - MIT
47
46
  metadata: {}
48
- post_install_message:
49
47
  rdoc_options: []
50
48
  require_paths:
51
49
  - lib
@@ -53,15 +51,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
51
  requirements:
54
52
  - - ">="
55
53
  - !ruby/object:Gem::Version
56
- version: 2.7.2
54
+ version: 3.2.6
57
55
  required_rubygems_version: !ruby/object:Gem::Requirement
58
56
  requirements:
59
57
  - - ">="
60
58
  - !ruby/object:Gem::Version
61
59
  version: '0'
62
60
  requirements: []
63
- rubygems_version: 3.5.22
64
- signing_key:
61
+ rubygems_version: 4.0.12
65
62
  specification_version: 4
66
63
  summary: A Ruby gem for integrating with Slack API to perform various actions like
67
64
  posting messages.