slkecho 1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6d32f248314a8ecc59325cf6cb2092a53ef4bfed9e9866ad46c0ad09076e79d2
4
+ data.tar.gz: 377be48a2826d3a0df573127cb06add79bb25a0a31fb4c152a7eb709faee127c
5
+ SHA512:
6
+ metadata.gz: 3aa071ad29e150bcb983f3c674ba6aaccda853fc8ff65fe93395a92e86e7e4190e39f5dcfcad9f88019b74986ccd2bba41556ee5a5b5f8a3d720686e072f12bb
7
+ data.tar.gz: 6293abc9ec4d005d50fa5fdc1672e6e981713957ee3a9548ef86d9c2c0a4d2a9c4c8a1f2da06d19738ddad31613f9345159321df5dd59088cd65c3d17eb056cf
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 3.0
6
+
7
+ Style/Documentation:
8
+ Enabled: false
9
+
10
+ Style/StringLiterals:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ Enabled: true
16
+ EnforcedStyle: double_quotes
17
+
18
+ Layout/LineLength:
19
+ Max: 120
20
+
21
+ RSpec/NamedSubject:
22
+ Enabled: false
@@ -0,0 +1,6 @@
1
+ {
2
+ "recommendations": [
3
+ "dotenv.dotenv-vscode",
4
+ "shopify.ruby-extensions-pack"
5
+ ]
6
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ // IntelliSense を使用して利用可能な属性を学べます。
3
+ // 既存の属性の説明をホバーして表示します。
4
+ // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type": "rdbg",
9
+ "name": "Debug current file with rdbg",
10
+ "request": "launch",
11
+ "script": "${file}",
12
+ "args": [],
13
+ "askParameters": true
14
+ },
15
+ {
16
+ "type": "rdbg",
17
+ "name": "Attach with rdbg",
18
+ "request": "attach"
19
+ }
20
+ ]
21
+ }
data/CHANGELOG.md ADDED
@@ -0,0 +1,42 @@
1
+ ## [Unreleased]
2
+
3
+ ## [v1.2.0] - 2024-01-08
4
+
5
+ - feat: Add --username option
6
+
7
+ ## [v1.1.0] - 2024-01-08
8
+
9
+ - test: reset SLACK_API_TOKEN env var
10
+ - feat: Message can be set from stdin
11
+
12
+ ## [v1.0.1] - 2024-01-08
13
+
14
+ - fix: Delay validation of configuration
15
+ - fix: Error messages are now output to stderr
16
+
17
+ ## [v1.0.0] - 2024-01-06
18
+
19
+ - docs: Update installation section
20
+ - feat: Improve error message
21
+ - style: Align line wrapping
22
+
23
+ ## [v0.3.0] - 2024-01-06
24
+
25
+ - Allow channel id
26
+ - Allow user id
27
+ - Update README
28
+
29
+ ## [v0.2.1] - 2024-01-06
30
+
31
+ - Fix error handling
32
+
33
+ ## [v0.2.0] - 2024-01-06
34
+
35
+ - chore: add dotenv extension
36
+ - Add --mention option
37
+ - Improve error handling
38
+ - Command line options
39
+
40
+ ## [v0.1.0] - 2024-01-03
41
+
42
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 okonomi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Slkecho
2
+
3
+ Slkecho is a CLI tool to post message to Slack like echo command.
4
+
5
+ ## Installation
6
+
7
+ Install it yourself as:
8
+
9
+ ```
10
+ gem install slkecho
11
+ ```
12
+
13
+ or add this line to your application's Gemfile:
14
+
15
+ ```
16
+ source "https://rubygems.pkg.github.com/okonomi" do
17
+ gem "slkecho"
18
+ end
19
+ ```
20
+
21
+ More details: [Working with the RubyGems registry - GitHub Docs](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-rubygems-registry#installing-a-package)
22
+
23
+ ## Usage
24
+
25
+ ```
26
+ slkecho -c <channel> -s <subject> -m <mention> message
27
+ ```
28
+
29
+ or message from stdin:
30
+
31
+ ```
32
+ cat message.txt | slkecho -c <channel> -s <subject> -m <mention>
33
+ ```
34
+
35
+ ### Options
36
+
37
+ #### -c, --channel <channel> (required)
38
+
39
+ Specify channel to post message. Channel name (starts `#`) or channel id (starts `C`).
40
+
41
+ #### -s, --subject <subject> (optional)
42
+
43
+ Specify subject to post message.
44
+
45
+ #### -m, --mention <mention> (optional)
46
+
47
+ Specify mention to post message. user email or user id (starts `U`).
48
+
49
+ #### --username <username> (optional)
50
+
51
+ Specify the username for the published message.
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/okonomi/slkecho.
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/exe/slkecho ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../lib/slkecho"
5
+
6
+ Slkecho.configure do |config|
7
+ config.slack_api_token = ENV["SLACK_API_TOKEN"]
8
+ end
9
+
10
+ begin
11
+ Slkecho::CLI.run(ARGV)
12
+ puts "Message sent successfully."
13
+ rescue Slkecho::InvalidConfigurationError => e
14
+ abort "#{e.message} Please set SLACK_API_TOKEN environment variable." if e.item == :slack_api_token
15
+ abort e.message
16
+ rescue Slkecho::InvalidOptionError => e
17
+ abort e.message
18
+ rescue Slkecho::SlackRequestError => e
19
+ abort "Request failed: #{e.message}"
20
+ rescue Slkecho::SlackResponseError => e
21
+ abort "Error responsed: #{e.message}"
22
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+
5
+ module Slkecho
6
+ class CLI
7
+ def initialize(option_parser:, slack_client:)
8
+ @option_parser = option_parser
9
+ @slack_client = slack_client
10
+ end
11
+
12
+ def run(argv)
13
+ options = @option_parser.parse(argv)
14
+
15
+ Slkecho.configuration.validate
16
+
17
+ user_id = mention_to_user_id(options.mention)
18
+
19
+ @slack_client.post_message(
20
+ channel: options.channel,
21
+ message: options.message,
22
+ subject: options.subject,
23
+ user_id: user_id
24
+ )
25
+ end
26
+
27
+ def mention_to_user_id(mention)
28
+ return nil if mention.nil?
29
+
30
+ return mention unless mention.include?("@")
31
+ return mention if mention.start_with?("U")
32
+
33
+ user = @slack_client.lookup_user_by_email(email: mention)
34
+ user["id"]
35
+ end
36
+
37
+ def self.run(argv)
38
+ cli = new(
39
+ option_parser: Slkecho::OptionParser.new,
40
+ slack_client: Slkecho::SlackClient.new(slack_api_token: Slkecho.configuration.slack_api_token)
41
+ )
42
+ cli.run(argv)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slkecho
4
+ class Configuration
5
+ attr_accessor :slack_api_token
6
+
7
+ def validate
8
+ if slack_api_token.nil?
9
+ raise Slkecho::InvalidConfigurationError.new("slack_api_token is required.", :slack_api_token)
10
+ end
11
+
12
+ true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slkecho
4
+ class OptionParser
5
+ def option_parser
6
+ @option_parser ||= ::OptionParser.new do |o|
7
+ o.banner = "Usage: slkecho [options] message"
8
+ o.program_name = "slkecho"
9
+ o.version = Slkecho::VERSION
10
+ o.on("-c", "--channel CHANNEL", "Slack channel to post the message") { @options.channel = _1 }
11
+ o.on("-s", "--subject SUBJECT", "Subject of message") { @options.subject = _1 }
12
+ o.on("-m", "--mention EMAIL", "Mention to user by email") { @options.mention = _1 }
13
+ o.on("--username USERNAME", "Username to post the message") { @options.username = _1 }
14
+ end
15
+ end
16
+
17
+ def parse(argv)
18
+ options = parse_options(argv)
19
+ validate_options(options)
20
+
21
+ options
22
+ end
23
+
24
+ def parse_options(argv)
25
+ @options = Slkecho::Options.new
26
+ argv = option_parser.parse(argv)
27
+
28
+ @options.message = if !argv.empty?
29
+ argv.first
30
+ elsif !$stdin.tty?
31
+ $stdin.read.then { _1.empty? ? nil : _1 }
32
+ end
33
+
34
+ @options.dup
35
+ end
36
+
37
+ def validate_options(options)
38
+ # channel
39
+ channel = options.channel
40
+ raise Slkecho::InvalidOptionError, "channel is required." if channel.nil?
41
+ unless channel.start_with?("#") || channel.start_with?("C")
42
+ raise Slkecho::InvalidOptionError, "channel must start with # or C."
43
+ end
44
+
45
+ # message
46
+ raise Slkecho::InvalidOptionError, "message is missing." if options.message.nil?
47
+
48
+ true
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slkecho
4
+ class Options
5
+ attr_accessor :channel, :subject, :mention, :message, :username
6
+
7
+ def initialize(channel: nil, subject: nil, mention: nil, message: nil, username: nil)
8
+ @channel = channel
9
+ @subject = subject
10
+ @mention = mention
11
+ @message = message
12
+ @username = username
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+ require "json"
6
+
7
+ require_relative "slack_request/lookup_user_by_email"
8
+ require_relative "slack_request/post_message"
9
+
10
+ module Slkecho
11
+ class SlackClient
12
+ def initialize(slack_api_token:)
13
+ @slack_api_token = slack_api_token
14
+
15
+ @uri = URI.parse("https://slack.com/api/chat.postMessage")
16
+ @http = Net::HTTP.new(@uri.host, @uri.port)
17
+ @http.use_ssl = true
18
+ @headers = {
19
+ "Content-Type" => "application/json; charset=utf-8",
20
+ "Authorization" => "Bearer #{slack_api_token}"
21
+ }
22
+ end
23
+
24
+ def lookup_user_by_email(email:)
25
+ Slkecho::SlackRequest::LookupUserByEmail.new(slack_api_token: @slack_api_token)
26
+ .request(email: email)
27
+ end
28
+
29
+ def post_message(channel:, message:, subject: nil, user_id: nil)
30
+ Slkecho::SlackRequest::PostMessage.new(slack_api_token: @slack_api_token)
31
+ .request(channel: channel, message: message, subject: subject, user_id: user_id)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+ require "json"
6
+
7
+ module Slkecho
8
+ module SlackRequest
9
+ class LookupUserByEmail
10
+ def initialize(slack_api_token:)
11
+ @slack_api_token = slack_api_token
12
+
13
+ @uri = URI.parse("https://slack.com/api/users.lookupByEmail")
14
+ @http = Net::HTTP.new(@uri.host, @uri.port)
15
+ @http.use_ssl = true
16
+ @headers = {
17
+ "Authorization" => "Bearer #{slack_api_token}",
18
+ "Content-Type" => "application/x-www-form-urlencoded"
19
+ }
20
+ end
21
+
22
+ def request(email:)
23
+ begin
24
+ response = @http.get(uri_with_query(@uri, { email: email }), @headers)
25
+ rescue StandardError => e
26
+ raise Slkecho::SlackRequestError, e.message
27
+ end
28
+ raise Slkecho::SlackRequestError, response.body unless response.is_a?(Net::HTTPSuccess)
29
+
30
+ user_info = JSON.parse(response.body)
31
+ raise Slkecho::SlackResponseError, "user not found. (#{email})" if user_info["error"] == "users_not_found"
32
+ raise Slkecho::SlackResponseError, user_info["error"] unless user_info["ok"]
33
+
34
+ user_info["user"]
35
+ end
36
+
37
+ def uri_with_query(uri, params)
38
+ uri.dup.tap { _1.query = URI.encode_www_form(params) }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slkecho
4
+ module SlackRequest
5
+ class PostMessage
6
+ def initialize(slack_api_token:)
7
+ @slack_api_token = slack_api_token
8
+
9
+ @uri = URI.parse("https://slack.com/api/chat.postMessage")
10
+ @http = Net::HTTP.new(@uri.host, @uri.port)
11
+ @http.use_ssl = true
12
+ @headers = {
13
+ "Content-Type" => "application/json; charset=utf-8",
14
+ "Authorization" => "Bearer #{slack_api_token}"
15
+ }
16
+ end
17
+
18
+ def request(channel:, message:, subject: nil, user_id: nil)
19
+ response = @http.post(
20
+ @uri.path,
21
+ request_body(channel: channel, message: message, subject: subject, user_id: user_id).to_json,
22
+ @headers
23
+ )
24
+ raise Slkecho::SlackRequestError, response.body unless response.is_a?(Net::HTTPSuccess)
25
+
26
+ result = JSON.parse(response.body)
27
+ raise Slkecho::SlackResponseError, result["error"] unless result["ok"]
28
+
29
+ true
30
+ end
31
+
32
+ def request_body(channel:, message:, subject: nil, user_id: nil, username: nil)
33
+ body = {
34
+ "channel" => channel,
35
+ "blocks" => [],
36
+ "username" => username
37
+ }
38
+ body["blocks"] << header_block(subject) unless subject.nil?
39
+ body["blocks"] << section_block(message, user_id: user_id)
40
+
41
+ body
42
+ end
43
+
44
+ def header_block(text)
45
+ {
46
+ "type" => "header",
47
+ "text" => {
48
+ "type" => "plain_text",
49
+ "text" => text,
50
+ "emoji" => true
51
+ }
52
+ }
53
+ end
54
+
55
+ def section_block(text, user_id: nil)
56
+ {
57
+ "type" => "section",
58
+ "text" => {
59
+ "type" => "mrkdwn",
60
+ "text" => user_id.nil? ? text : "<@#{user_id}> #{text}"
61
+ }
62
+ }
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slkecho
4
+ VERSION = "1.2.0"
5
+ end
data/lib/slkecho.rb ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "slkecho/version"
4
+ require_relative "slkecho/configuration"
5
+ require_relative "slkecho/cli"
6
+ require_relative "slkecho/options"
7
+ require_relative "slkecho/option_parser"
8
+ require_relative "slkecho/slack_client"
9
+
10
+ module Slkecho
11
+ class InvalidConfigurationError < StandardError
12
+ attr_reader :item
13
+
14
+ def initialize(message, item = nil)
15
+ @item = item
16
+ super(message)
17
+ end
18
+ end
19
+
20
+ class InvalidOptionError < StandardError; end
21
+ class SlackRequestError < StandardError; end
22
+ class SlackResponseError < StandardError; end
23
+
24
+ class << self
25
+ def configure
26
+ yield(configuration)
27
+ end
28
+
29
+ def configuration
30
+ @configuration ||= Configuration.new
31
+ end
32
+ end
33
+ end
data/sig/slkecho.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Slkecho
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slkecho
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - okonomi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Post message to Slack like echo command.
14
+ email:
15
+ - okonomi@oknm.jp
16
+ executables:
17
+ - slkecho
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - ".vscode/extensions.json"
24
+ - ".vscode/launch.json"
25
+ - CHANGELOG.md
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - exe/slkecho
30
+ - lib/slkecho.rb
31
+ - lib/slkecho/cli.rb
32
+ - lib/slkecho/configuration.rb
33
+ - lib/slkecho/option_parser.rb
34
+ - lib/slkecho/options.rb
35
+ - lib/slkecho/slack_client.rb
36
+ - lib/slkecho/slack_request/lookup_user_by_email.rb
37
+ - lib/slkecho/slack_request/post_message.rb
38
+ - lib/slkecho/version.rb
39
+ - sig/slkecho.rbs
40
+ homepage: https://github.com/okonomi/slkecho
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://github.com/okonomi/slkecho
45
+ source_code_uri: https://github.com/okonomi/slkecho
46
+ changelog_uri: https://github.com/okonomi/slkecho/blob/main/CHANGELOG.md
47
+ github_repo: https://github.com/okonomi/slkecho
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.0.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.5.3
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Post message to Slack like echo command.
67
+ test_files: []