slkecho 2.0.1 → 2.1.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: 7e644caea3b2e9401cfc58f37814cbf04d94d74292f4b150eb12f108d63022e7
4
- data.tar.gz: '081186fc14c84b7311f710a7d14f7f8c962ca69e9165857cd03aa38ebb9f65fc'
3
+ metadata.gz: 4316f376c8717965ab2c3518c054be6d983bc005d343803305bdb90f2bc0b963
4
+ data.tar.gz: 11cf2ec4be6b49b6a4d4d566ac9c767036ae91871c3ebe39e4a760198f13e367
5
5
  SHA512:
6
- metadata.gz: 79303c561c911bf9c9e46f2f7ccc7f23baa76738fde53219e071eccb17ca9764ef5a9fb1c5091136e75291ee2336a9a2ee340c93945dcba2c5e66ac25b016629
7
- data.tar.gz: e372eeb64e03fe3a1275365ee64f815dd55454a10414a0c896644d33e7a5731bbcdfe5aae21f833480cfaf582f9f4ee6f5196231a9cd9987cb79aa52535ae7cc
6
+ metadata.gz: 25a8acf5fd4f84c7d76b91ac9a44e99e780e398c0b878dac33352e621e5139dc09d9d13b041eaec04227b20c7996596799656b5cf367195d65aeb10b959507e2
7
+ data.tar.gz: da7a9a3a06f06910e7660f5408c0a9c9a3b1d0f057c02ad88b5c9bc10ba96f559141b956659a6993b6b04c611a6fde5f0b26a06998c39ad9651270ad21d877f0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [v2.1.1] - 2024-02-12
4
+
5
+ - chore: Add ruby-lsp gem
6
+ - refactor: Parse JSON with symbolize names
7
+ - refactor: Make request body with symbolize keys
8
+ - refactor: Make blocks with symbolize keys
9
+ - refactor: Extract http response checking
10
+ - test: Introduce saharspec gem
11
+
12
+ ## [v2.1.0] - 2024-01-19
13
+
14
+ - feat: Add --message-as-blocks option
15
+ - fix: Require libraries
16
+ - style: Enable RuboCop new cops and fix offenses
17
+ - style: Add rubocop-performance and fix offenses
18
+
3
19
  ## [v2.0.1] - 2024-01-17
4
20
 
5
21
  - feat: Add Dockerfile
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Slkecho
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/slkecho.svg)](https://badge.fury.io/rb/slkecho)
4
+
3
5
  Slkecho is a CLI tool to post message to Slack like echo command.
4
6
 
5
7
  ## Installation
@@ -63,6 +65,12 @@ Set user image for message by emoji.
63
65
 
64
66
  See below: https://api.slack.com/methods/chat.postMessage#arg_icon_emoji
65
67
 
68
+ ### --message-as-blocks (optional)
69
+
70
+ Post message as blocks.
71
+
72
+ See below: https://api.slack.com/methods/chat.postMessage#arg_blocks
73
+
66
74
  ## Contributing
67
75
 
68
76
  Bug reports and pull requests are welcome on GitHub at https://github.com/okonomi/slkecho.
data/exe/slkecho CHANGED
@@ -4,7 +4,7 @@
4
4
  require_relative "../lib/slkecho"
5
5
 
6
6
  Slkecho.configure do |config|
7
- config.slack_api_token = ENV["SLACK_API_TOKEN"]
7
+ config.slack_api_token = ENV.fetch("SLACK_API_TOKEN", nil)
8
8
  end
9
9
 
10
10
  begin
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Slkecho
6
+ class BlocksBuilder
7
+ def build_from_message(message, user_id = nil)
8
+ [
9
+ {
10
+ type: "section",
11
+ text: {
12
+ type: "mrkdwn",
13
+ text: user_id.nil? ? message : "<@#{user_id}> #{message}"
14
+ }
15
+ }
16
+ ]
17
+ end
18
+
19
+ def build_from_json(json, user_id = nil)
20
+ JSON.parse(json.gsub("<mention>", "<@#{user_id}>"))
21
+ end
22
+ end
23
+ end
data/lib/slkecho/cli.rb CHANGED
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "optparse"
4
-
5
3
  module Slkecho
6
4
  class CLI
7
- def initialize(option_parser:, slack_client:)
5
+ def initialize(option_parser:, slack_client:, blocks_builder:)
8
6
  @option_parser = option_parser
9
7
  @slack_client = slack_client
8
+ @blocks_builder = blocks_builder
10
9
  end
11
10
 
12
11
  def run(argv)
@@ -27,17 +26,26 @@ module Slkecho
27
26
  def post_message_params_from(options, user_id)
28
27
  Slkecho::SlackClient::PostMessageParams.new(
29
28
  channel: options.channel,
30
- message: user_id.nil? ? options.message : "<@#{user_id}> #{options.message}",
29
+ blocks: blocks_from(options.message, user_id, options.message_as_blocks),
31
30
  username: options.username,
32
31
  icon_url: options.icon_url,
33
32
  icon_emoji: options.icon_emoji
34
33
  )
35
34
  end
36
35
 
36
+ def blocks_from(message, user_id, message_as_blocks)
37
+ if message_as_blocks
38
+ @blocks_builder.build_from_json(message, user_id)
39
+ else
40
+ @blocks_builder.build_from_message(message, user_id)
41
+ end
42
+ end
43
+
37
44
  def self.run(argv)
38
45
  cli = new(
39
46
  option_parser: Slkecho::OptionParser.new,
40
- slack_client: Slkecho::SlackClient.new(slack_api_token: Slkecho.configuration.slack_api_token)
47
+ slack_client: Slkecho::SlackClient.new(slack_api_token: Slkecho.configuration.slack_api_token),
48
+ blocks_builder: Slkecho::BlocksBuilder.new
41
49
  )
42
50
  cli.run(argv)
43
51
  end
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "optparse"
4
+
3
5
  module Slkecho
4
6
  class OptionParser
5
- def option_parser # rubocop:disable Metrics/AbcSize
7
+ def option_parser # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
6
8
  @option_parser ||= ::OptionParser.new do |o|
7
9
  o.banner = "Usage: slkecho [options] message"
8
10
  o.program_name = "slkecho"
@@ -12,6 +14,7 @@ module Slkecho
12
14
  o.on("--username USERNAME", "Set user name for message.") { @options.username = _1 }
13
15
  o.on("--icon-url ICON_URL", "Set user icon image for message by URL.") { @options.icon_url = _1 }
14
16
  o.on("--icon-emoji ICON_EMOJI", "Set user image for message by emoji.") { @options.icon_emoji = _1 }
17
+ o.on("--message-as-blocks", "Post message as blocks.") { @options.message_as_blocks = true }
15
18
  end
16
19
  end
17
20
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Slkecho
4
4
  class Options
5
- attr_accessor :channel, :mention_by_email, :message, :username, :icon_url, :icon_emoji
5
+ attr_accessor :channel, :mention_by_email, :message, :username, :icon_url, :icon_emoji, :message_as_blocks
6
6
  end
7
7
  end
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "net/http"
4
- require "uri"
5
- require "json"
6
-
7
3
  require_relative "slack_request/lookup_user_by_email"
8
4
  require_relative "slack_request/post_message"
9
5
 
@@ -13,14 +9,6 @@ module Slkecho
13
9
 
14
10
  def initialize(slack_api_token:)
15
11
  @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
12
  end
25
13
 
26
14
  def lookup_user_by_email(email:)
@@ -4,6 +4,8 @@ require "net/http"
4
4
  require "uri"
5
5
  require "json"
6
6
 
7
+ require_relative "../slack_request"
8
+
7
9
  module Slkecho
8
10
  module SlackRequest
9
11
  class LookupUserByEmail
@@ -20,18 +22,18 @@ module Slkecho
20
22
  end
21
23
 
22
24
  def request(email:)
23
- begin
24
- response = @http.get(uri_with_query(@uri, { email: email }), @headers)
25
- rescue StandardError => e
26
- raise Slkecho::SlackApiHttpError, e.message
25
+ user_info = Slkecho::SlackRequest.send_request do
26
+ @http.get(uri_with_query(@uri, { email: email }), @headers)
27
27
  end
28
- raise Slkecho::SlackApiHttpError, response.body unless response.is_a?(Net::HTTPSuccess)
29
28
 
30
- user_info = JSON.parse(response.body)
31
- raise Slkecho::SlackApiResultError, "user not found. (#{email})" if user_info["error"] == "users_not_found"
32
- raise Slkecho::SlackApiResultError, user_info["error"] unless user_info["ok"]
29
+ case user_info
30
+ in { ok: true, user: user }
31
+ user
32
+ in { ok: false, error: error }
33
+ raise Slkecho::SlackApiResultError, "user not found. (#{email})" if error == "users_not_found"
33
34
 
34
- user_info["user"]
35
+ raise Slkecho::SlackApiResultError, error
36
+ end
35
37
  end
36
38
 
37
39
  def uri_with_query(uri, params)
@@ -1,9 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "net/http"
4
+ require "uri"
5
+ require "json"
6
+
7
+ require_relative "../slack_request"
8
+
3
9
  module Slkecho
4
10
  module SlackRequest
5
11
  class PostMessage
6
- Params = Struct.new(:channel, :message, :username, :icon_url, :icon_emoji, keyword_init: true)
12
+ Params = Struct.new(:channel, :blocks, :username, :icon_url, :icon_emoji, keyword_init: true)
7
13
 
8
14
  def initialize(slack_api_token:)
9
15
  @slack_api_token = slack_api_token
@@ -18,40 +24,28 @@ module Slkecho
18
24
  end
19
25
 
20
26
  def request(params)
21
- response = @http.post(
22
- @uri.path,
23
- request_body(params).to_json,
24
- @headers
25
- )
26
- raise Slkecho::SlackApiHttpError, "#{response.code} #{response.message}" unless response.is_a?(Net::HTTPSuccess)
27
+ result = Slkecho::SlackRequest.send_request do
28
+ @http.post(
29
+ @uri.path,
30
+ request_body(params).to_json,
31
+ @headers
32
+ )
33
+ end
27
34
 
28
- result = JSON.parse(response.body)
29
- raise Slkecho::SlackApiResultError, result["error"] unless result["ok"]
35
+ raise Slkecho::SlackApiResultError, result[:error] unless result[:ok]
30
36
 
31
37
  true
32
38
  end
33
39
 
34
40
  def request_body(params)
35
41
  {
36
- "channel" => params.channel,
37
- "blocks" => blocks_from(params.message),
38
- "username" => params.username,
39
- "icon_url" => params.icon_url,
40
- "icon_emoji" => params.icon_emoji
42
+ channel: params.channel,
43
+ blocks: params.blocks,
44
+ username: params.username,
45
+ icon_url: params.icon_url,
46
+ icon_emoji: params.icon_emoji
41
47
  }
42
48
  end
43
-
44
- def blocks_from(message)
45
- [
46
- {
47
- "type" => "section",
48
- "text" => {
49
- "type" => "mrkdwn",
50
- "text" => message
51
- }
52
- }
53
- ]
54
- end
55
49
  end
56
50
  end
57
51
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slkecho
4
+ module SlackRequest
5
+ def self.send_request
6
+ return unless block_given?
7
+
8
+ response = yield
9
+ raise Slkecho::SlackApiError, "API Response could not be retrieved." unless response.is_a?(Net::HTTPResponse)
10
+ raise Slkecho::SlackApiHttpError, "#{response.code} #{response.message}" unless response.is_a?(Net::HTTPSuccess)
11
+
12
+ begin
13
+ JSON.parse(response.body, symbolize_names: true)
14
+ rescue JSON::ParserError
15
+ raise Slkecho::SlackApiError, "API Response is not JSON."
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slkecho
4
- VERSION = "2.0.1"
4
+ VERSION = "2.1.1"
5
5
  end
data/lib/slkecho.rb CHANGED
@@ -6,6 +6,7 @@ require_relative "slkecho/cli"
6
6
  require_relative "slkecho/options"
7
7
  require_relative "slkecho/option_parser"
8
8
  require_relative "slkecho/slack_client"
9
+ require_relative "slkecho/blocks_builder"
9
10
 
10
11
  module Slkecho
11
12
  class InvalidConfigurationError < StandardError
@@ -18,6 +19,7 @@ module Slkecho
18
19
  end
19
20
 
20
21
  class InvalidOptionError < StandardError; end
22
+ class SlackApiError < StandardError; end
21
23
  class SlackApiHttpError < StandardError; end
22
24
  class SlackApiResultError < StandardError; end
23
25
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slkecho
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - okonomi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-17 00:00:00.000000000 Z
11
+ date: 2024-02-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Post message to Slack like echo command.
14
14
  email:
@@ -23,11 +23,13 @@ files:
23
23
  - README.md
24
24
  - exe/slkecho
25
25
  - lib/slkecho.rb
26
+ - lib/slkecho/blocks_builder.rb
26
27
  - lib/slkecho/cli.rb
27
28
  - lib/slkecho/configuration.rb
28
29
  - lib/slkecho/option_parser.rb
29
30
  - lib/slkecho/options.rb
30
31
  - lib/slkecho/slack_client.rb
32
+ - lib/slkecho/slack_request.rb
31
33
  - lib/slkecho/slack_request/lookup_user_by_email.rb
32
34
  - lib/slkecho/slack_request/post_message.rb
33
35
  - lib/slkecho/version.rb
@@ -40,7 +42,8 @@ metadata:
40
42
  source_code_uri: https://github.com/okonomi/slkecho
41
43
  changelog_uri: https://github.com/okonomi/slkecho/blob/main/CHANGELOG.md
42
44
  github_repo: https://github.com/okonomi/slkecho
43
- post_install_message:
45
+ rubygems_mfa_required: 'true'
46
+ post_install_message:
44
47
  rdoc_options: []
45
48
  require_paths:
46
49
  - lib
@@ -56,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
59
  version: '0'
57
60
  requirements: []
58
61
  rubygems_version: 3.5.3
59
- signing_key:
62
+ signing_key:
60
63
  specification_version: 4
61
64
  summary: Post message to Slack like echo command.
62
65
  test_files: []