immosquare-slack 0.1.6 → 0.2.1

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: 382c2959d5d7d3869803f78d02a7247288a194045513777288caff5c2fa7abae
4
+ data.tar.gz: 403787e3740082733bb3966bd58bde6ccb45d4e3768e8f06027c1470e66872d3
5
5
  SHA512:
6
- metadata.gz: 180c9241ecb12c5784a9d287087f37ef22fc58f1126e48107d51dcbeb92dc17efe769fdab3d1fae19f083d1c6e88bc26bab4712d09e3650543933eb14adf0714
7
- data.tar.gz: 6b3cbd7e068f7d0cc3575c3403104c739e18a167d02549be2c11c2f0a9daa770a4df218671b3f0edb527237ee49d2faee46cfd7892536ad42257fd90269743a9
6
+ metadata.gz: c93db1d79831bfbaab04b9e00b8fba3236c5e0e5bd040207498d573c05897575de36ef6ec38ecfadfc35481c4469e0a1fd8dce97c674d846ec57ee9606f3c78d
7
+ data.tar.gz: 233f3fe54d124c757f3c07369ca7a9d1e615d42b6c09d98d86e931d5dd98e22d7cd9abd9d042375d1a70b2c2aa9878ed79719ce11864a91ecf32e333b92d1c22
@@ -1,12 +1,24 @@
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 for the lifetime of that
12
+ ## process (a Puma worker or Sidekiq runs for hours/days).
13
+ ##
14
+ ## Piège : le cache ne reflète que l'état du workspace au
15
+ ## premier appel. Un channel créé ou renommé ensuite n'y est
16
+ ## pas. `force: true` vide le cache et refetch — à utiliser sur
17
+ ## un miss de lookup pour distinguer un channel réellement
18
+ ## absent d'un cache périmé avant tout fallback.
8
19
  ##============================================================##
9
- def list_channels
20
+ def list_channels(force: false)
21
+ @list_channels = nil if force
10
22
  @list_channels ||= begin
11
23
  extra_params = {
12
24
  :types => ["public_channel", "private_channel"].join(","),
@@ -17,14 +29,35 @@ module ImmosquareSlack
17
29
  end
18
30
 
19
31
  ##============================================================##
20
- ## Pour poster un message dans un channel
32
+ ## Posts a message to a channel.
33
+ ##
34
+ ## `channel_name` and `bot_name` are optional keyword args:
35
+ ## if nil, they fall back to the global config
36
+ ## `ImmosquareSlack.configuration.default_channel` and
37
+ ## `default_bot_name`. Lets apps that always notify the same
38
+ ## channel avoid repeating the value at every call site.
39
+ ##
40
+ ## If no channel can be resolved after fallback, raises an
41
+ ## ArgumentError immediately (clearer than the cryptic error
42
+ ## returned by the Slack API).
21
43
  ##============================================================##
22
- def post_message(channel_name, text, notify: nil, notify_text: nil, bot_name: nil, notify_general_if_invalid_channel: true)
44
+ def post_message(text, channel_name: nil, notify: nil, notify_text: nil, bot_name: nil, notify_general_if_invalid_channel: true)
45
+ channel_name ||= ImmosquareSlack.configuration.default_channel
46
+ bot_name ||= ImmosquareSlack.configuration.default_bot_name
47
+
48
+ raise(ArgumentError, "channel_name is required (or set ImmosquareSlack.configuration.default_channel)") if channel_name.nil?
49
+
50
+ ##============================================================##
51
+ ## A miss can mean the channel really doesn't exist, or that
52
+ ## the memoized list predates its creation. Retry once on a
53
+ ## freshly fetched list before declaring it missing.
54
+ ##============================================================##
23
55
  channel_id = get_channel_id_by_name(channel_name)
56
+ channel_id = get_channel_id_by_name(channel_name, :force => true) if channel_id.nil?
24
57
 
25
58
  if channel_id.nil?
26
59
  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
60
+ 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
61
 
29
62
  raise("channel '#{channel_name}' not found on slack")
30
63
  end
@@ -43,32 +76,41 @@ module ImmosquareSlack
43
76
  make_slack_api_call(url, :method => :post, :body => body)
44
77
  end
45
78
 
46
-
47
-
48
79
  private
49
80
 
50
81
  ##============================================================##
51
- ## Pour récupérer l'id d'un channel en fonction de son nom
82
+ ## Resolves a channel id from its name. "general" is matched
83
+ ## via the `is_general` flag because workspaces can rename
84
+ ## their general channel.
52
85
  ##============================================================##
53
- def get_channel_id_by_name(channel_name)
54
- 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
86
+ def get_channel_id_by_name(channel_name, force: false)
87
+ channels = list_channels(:force => force)
88
+ channel = channels.find do |c|
89
+ if channel_name == GENERAL_CHANNEL
90
+ c["is_general"]
91
+ else
92
+ c["name"] == channel_name && c["is_archived"] == false
93
+ end
94
+ end
95
+ channel&.[]("id")
57
96
  end
58
97
 
59
98
  ##============================================================##
60
- ## Pour récupérer la liste des membres d'un channel
99
+ ## Fetches the list of members of a channel.
61
100
  ##============================================================##
62
101
  def get_channel_members(channel_id)
63
102
  fetch_paginated_data("https://slack.com/api/conversations.members", "members", {:channel => channel_id})
64
103
  end
65
104
 
66
105
  ##============================================================##
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?
106
+ ## Builds the notification prefix prepended to the message
107
+ ## body. `notify` may be:
108
+ ## - an Array of emails: mentions matching workspace members
109
+ ## - :all : mentions every member of the channel
110
+ ## - :channel/:here/:everyone : Slack-wide broadcast tokens
111
+ ##
112
+ ## Note: avoids `in?` and `present?` so the gem stays usable
113
+ ## from plain Ruby (no Rails / ActiveSupport required).
72
114
  ##============================================================##
73
115
  def build_notification_text(channel_id, notify, text = "Hello")
74
116
  final =
@@ -87,8 +129,9 @@ module ImmosquareSlack
87
129
  "<!everyone>"
88
130
  end
89
131
 
132
+ return nil if final.to_s.empty?
90
133
 
91
- "#{text} #{final}\n" if !final.to_s.empty?
134
+ text.empty? ? "#{final}\n" : "#{text} #{final}\n"
92
135
  end
93
136
 
94
137
  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.1".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.1
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.