slkecho 2.1.5 → 2.1.6
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 +4 -4
- data/CHANGELOG.md +7 -3
- data/lib/slkecho/http.rb +22 -0
- data/lib/slkecho/option_parser.rb +23 -22
- data/lib/slkecho/options.rb +19 -1
- data/lib/slkecho/slack_request/lookup_user_by_email.rb +16 -14
- data/lib/slkecho/slack_request/post_message.rb +26 -21
- data/lib/slkecho/version.rb +1 -1
- data/sig/slkecho/http.rbs +5 -0
- data/sig/slkecho/slack_request/lookup_user_by_email.rbs +2 -4
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1758bd89b754485e44d4268df132bb18866fc210d4ecb6e460f3e76bf0906f76
|
4
|
+
data.tar.gz: 3ab51b0007244fa47314cfb837a254e668a9ea2f00a5e96b433cdd42b672d3b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49428da2ed9644b5d70c154dd7c0218aaac8fe009f1fdd22ca4abd20601e71db9d7fd254ef9dbd78839402281fe76f789e1bdf07d4489d52d6bed197f4a5f549
|
7
|
+
data.tar.gz: b9f38dc1c3be84941b08b9cc69e0fa94bf84e7439d1c4e03b03ad99fc3d695a39efee653702d85f44a3dd459ff599511fd58368a0b42ca6a69466a6440734969
|
data/CHANGELOG.md
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
-
|
3
|
+
## [v2.1.6] - 2025-01-05
|
4
|
+
|
5
|
+
- refactor: options building
|
6
|
+
- refactor: slack request
|
4
7
|
|
5
8
|
## [v2.1.5] - 2025-01-04
|
6
9
|
|
7
|
-
-
|
10
|
+
- feat: Start type signature implementation
|
11
|
+
- chore: update dependencies
|
8
12
|
|
9
13
|
## [v2.1.4] - 2024-08-31
|
10
14
|
|
11
|
-
-
|
15
|
+
- chore: update dependencies
|
12
16
|
|
13
17
|
## [v2.1.3] - 2024-03-15
|
14
18
|
|
data/lib/slkecho/http.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "net/http"
|
4
|
+
require "uri"
|
5
|
+
|
6
|
+
module Slkecho
|
7
|
+
class HTTP
|
8
|
+
class << self
|
9
|
+
def get(uri, headers: nil)
|
10
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
11
|
+
http.use_ssl = uri.scheme == "https"
|
12
|
+
http.get(uri, headers)
|
13
|
+
end
|
14
|
+
|
15
|
+
def post(uri, headers: nil, body: nil)
|
16
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
17
|
+
http.use_ssl = uri.scheme == "https"
|
18
|
+
http.post(uri, body, headers)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -4,46 +4,47 @@ require "optparse"
|
|
4
4
|
|
5
5
|
module Slkecho
|
6
6
|
class OptionParser
|
7
|
-
def option_parser # rubocop:disable Metrics/
|
7
|
+
def option_parser # rubocop:disable Metrics/MethodLength
|
8
8
|
@option_parser ||= ::OptionParser.new do |o|
|
9
9
|
o.banner = "Usage: slkecho [options] message"
|
10
10
|
o.program_name = "slkecho"
|
11
11
|
o.version = Slkecho::VERSION
|
12
|
-
o.on("-c", "--channel CHANNEL", "Slack channel to post message.")
|
13
|
-
o.on("-m", "--mention-by-email EMAIL", "Mention to user by email.")
|
14
|
-
o.on("--username USERNAME", "Set user name for message.")
|
15
|
-
o.on("--icon-url ICON_URL", "Set user icon image for message by URL.")
|
16
|
-
o.on("--icon-emoji ICON_EMOJI", "Set user image for message by emoji.")
|
17
|
-
o.on("--message-as-blocks", "Post message as blocks.")
|
12
|
+
o.on("-c", "--channel CHANNEL", "Slack channel to post message.")
|
13
|
+
o.on("-m", "--mention-by-email EMAIL", "Mention to user by email.")
|
14
|
+
o.on("--username USERNAME", "Set user name for message.")
|
15
|
+
o.on("--icon-url ICON_URL", "Set user icon image for message by URL.")
|
16
|
+
o.on("--icon-emoji ICON_EMOJI", "Set user image for message by emoji.")
|
17
|
+
o.on("--message-as-blocks", "Post message as blocks.")
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
def parse(argv)
|
22
|
-
options =
|
22
|
+
options = build_options(argv)
|
23
23
|
validate_options(options)
|
24
24
|
|
25
25
|
options
|
26
26
|
end
|
27
27
|
|
28
|
-
def
|
29
|
-
|
30
|
-
argv = option_parser.parse(argv)
|
28
|
+
def build_options(argv)
|
29
|
+
option_values = {}
|
30
|
+
argv = option_parser.parse(argv, into: option_values)
|
31
|
+
option_values = option_values.transform_keys { _1.to_s.tr("-", "_") }
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
33
|
+
Slkecho::Options.new(option_values).tap do |opt|
|
34
|
+
opt.message = fetch_message(argv)
|
35
|
+
end
|
36
|
+
end
|
37
37
|
|
38
|
-
|
38
|
+
def fetch_message(argv)
|
39
|
+
if !argv.empty?
|
40
|
+
argv.first
|
41
|
+
elsif !$stdin.tty?
|
42
|
+
$stdin.read.then { _1.empty? ? nil : _1 }
|
43
|
+
end
|
39
44
|
end
|
40
45
|
|
41
46
|
def validate_options(options)
|
42
|
-
|
43
|
-
raise Slkecho::InvalidOptionError, "channel is required." if options.channel.nil?
|
44
|
-
|
45
|
-
# message
|
46
|
-
raise Slkecho::InvalidOptionError, "message is missing." if options.message.nil?
|
47
|
+
raise Slkecho::InvalidOptionError, options.error_message unless options.valid?
|
47
48
|
|
48
49
|
true
|
49
50
|
end
|
data/lib/slkecho/options.rb
CHANGED
@@ -1,7 +1,25 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_model"
|
4
|
+
|
3
5
|
module Slkecho
|
4
6
|
class Options
|
5
|
-
|
7
|
+
include ActiveModel::Model
|
8
|
+
include ActiveModel::Attributes
|
9
|
+
|
10
|
+
attribute :channel, :string
|
11
|
+
attribute :mention_by_email, :string
|
12
|
+
attribute :message, :string
|
13
|
+
attribute :username, :string
|
14
|
+
attribute :icon_url, :string
|
15
|
+
attribute :icon_emoji, :string
|
16
|
+
attribute :message_as_blocks, :boolean
|
17
|
+
|
18
|
+
validates :channel, presence: { message: "is required." }
|
19
|
+
validates :message, presence: { message: "is missing." }, if: -> { message.nil? }
|
20
|
+
|
21
|
+
def error_message
|
22
|
+
errors.full_messages.join(" ").downcase
|
23
|
+
end
|
6
24
|
end
|
7
25
|
end
|
@@ -5,28 +5,17 @@ require "uri"
|
|
5
5
|
require "json"
|
6
6
|
|
7
7
|
require_relative "../slack_request"
|
8
|
+
require_relative "../http"
|
8
9
|
|
9
10
|
module Slkecho
|
10
11
|
module SlackRequest
|
11
12
|
class LookupUserByEmail
|
12
13
|
def initialize(slack_api_token:)
|
13
14
|
@slack_api_token = slack_api_token
|
14
|
-
|
15
|
-
@uri = URI.parse("https://slack.com/api/users.lookupByEmail")
|
16
|
-
host = @uri.host
|
17
|
-
@http = Net::HTTP.new(host, @uri.port) unless host.nil?
|
18
|
-
@http.use_ssl = true
|
19
|
-
@headers = {
|
20
|
-
"Authorization" => "Bearer #{slack_api_token}",
|
21
|
-
"Content-Type" => "application/x-www-form-urlencoded"
|
22
|
-
}
|
23
15
|
end
|
24
16
|
|
25
17
|
def request(email:)
|
26
|
-
user_info =
|
27
|
-
@http.get(uri_with_query(@uri, { email: email }), @headers)
|
28
|
-
end
|
29
|
-
|
18
|
+
user_info = send_request(email, @slack_api_token)
|
30
19
|
case user_info
|
31
20
|
in { ok: true, user: user }
|
32
21
|
user
|
@@ -37,8 +26,21 @@ module Slkecho
|
|
37
26
|
end
|
38
27
|
end
|
39
28
|
|
29
|
+
private
|
30
|
+
|
31
|
+
def send_request(email, token)
|
32
|
+
Slkecho::SlackRequest.send_request do
|
33
|
+
uri = uri_with_query("https://slack.com/api/users.lookupByEmail", { email: email })
|
34
|
+
headers = {
|
35
|
+
"Authorization" => "Bearer #{token}",
|
36
|
+
"Content-Type" => "application/x-www-form-urlencoded"
|
37
|
+
}
|
38
|
+
Slkecho::HTTP.get(uri, headers: headers)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
40
42
|
def uri_with_query(uri, params)
|
41
|
-
uri.
|
43
|
+
URI(uri).tap { _1.query = URI.encode_www_form(params) }
|
42
44
|
end
|
43
45
|
end
|
44
46
|
end
|
@@ -4,30 +4,30 @@ require "net/http"
|
|
4
4
|
require "uri"
|
5
5
|
require "json"
|
6
6
|
|
7
|
+
require_relative "../http"
|
7
8
|
require_relative "../slack_request"
|
8
9
|
|
9
10
|
module Slkecho
|
10
11
|
module SlackRequest
|
11
12
|
class PostMessage
|
12
|
-
Params = Struct.new(:channel, :blocks, :username, :icon_url, :icon_emoji, keyword_init: true)
|
13
|
+
Params = Struct.new(:channel, :blocks, :username, :icon_url, :icon_emoji, keyword_init: true) do
|
14
|
+
def to_request_body
|
15
|
+
JSON.dump({
|
16
|
+
channel: channel,
|
17
|
+
blocks: blocks,
|
18
|
+
username: username,
|
19
|
+
icon_url: icon_url,
|
20
|
+
icon_emoji: icon_emoji
|
21
|
+
})
|
22
|
+
end
|
23
|
+
end
|
13
24
|
|
14
25
|
def initialize(slack_api_token:)
|
15
26
|
@slack_api_token = slack_api_token
|
16
|
-
|
17
|
-
@uri = URI.parse("https://slack.com/api/chat.postMessage")
|
18
|
-
@http = Net::HTTP.new(@uri.host, @uri.port)
|
19
|
-
@http.use_ssl = true
|
20
|
-
@headers = {
|
21
|
-
"Content-Type" => "application/json; charset=utf-8",
|
22
|
-
"Authorization" => "Bearer #{slack_api_token}"
|
23
|
-
}
|
24
27
|
end
|
25
28
|
|
26
29
|
def request(params)
|
27
|
-
result =
|
28
|
-
@http.post(@uri.path, request_body(params).to_json, @headers)
|
29
|
-
end
|
30
|
-
|
30
|
+
result = send_request(@slack_api_token, params)
|
31
31
|
case result
|
32
32
|
in { ok: true }
|
33
33
|
true
|
@@ -36,14 +36,19 @@ module Slkecho
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
39
|
+
private
|
40
|
+
|
41
|
+
def send_request(token, params)
|
42
|
+
Slkecho::SlackRequest.send_request do
|
43
|
+
uri = URI("https://slack.com/api/chat.postMessage")
|
44
|
+
headers = {
|
45
|
+
"Content-Type" => "application/json; charset=utf-8",
|
46
|
+
"Authorization" => "Bearer #{token}"
|
47
|
+
}
|
48
|
+
body = params.to_request_body
|
49
|
+
|
50
|
+
Slkecho::HTTP.post(uri, headers: headers, body: body)
|
51
|
+
end
|
47
52
|
end
|
48
53
|
end
|
49
54
|
end
|
data/lib/slkecho/version.rb
CHANGED
@@ -2,12 +2,10 @@ module Slkecho
|
|
2
2
|
module SlackRequest
|
3
3
|
class LookupUserByEmail
|
4
4
|
@slack_api_token: String
|
5
|
-
@uri: URI::Generic
|
6
|
-
@http: Net::HTTP
|
7
|
-
@headers: Hash[String, String]
|
8
5
|
|
9
6
|
def initialize: (slack_api_token: String) -> void
|
10
|
-
def
|
7
|
+
def send_request: (String, String) -> untyped
|
8
|
+
def uri_with_query: (String, untyped) -> untyped
|
11
9
|
end
|
12
10
|
end
|
13
11
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slkecho
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- okonomi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-01-
|
12
|
-
dependencies:
|
11
|
+
date: 2025-01-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
description: Post message to Slack like echo command.
|
14
28
|
email:
|
15
29
|
- okonomi@oknm.jp
|
@@ -27,6 +41,7 @@ files:
|
|
27
41
|
- lib/slkecho/blocks_builder.rb
|
28
42
|
- lib/slkecho/cli.rb
|
29
43
|
- lib/slkecho/configuration.rb
|
44
|
+
- lib/slkecho/http.rb
|
30
45
|
- lib/slkecho/option_parser.rb
|
31
46
|
- lib/slkecho/options.rb
|
32
47
|
- lib/slkecho/slack_client.rb
|
@@ -35,6 +50,7 @@ files:
|
|
35
50
|
- lib/slkecho/slack_request/post_message.rb
|
36
51
|
- lib/slkecho/version.rb
|
37
52
|
- sig/slkecho.rbs
|
53
|
+
- sig/slkecho/http.rbs
|
38
54
|
- sig/slkecho/slack_request.rbs
|
39
55
|
- sig/slkecho/slack_request/lookup_user_by_email.rbs
|
40
56
|
homepage: https://github.com/okonomi/slkecho
|