slkecho 2.1.0 → 2.1.2

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: 1aa38817dd7a85a9bb6a39924da65ad1d45173c29b058347481c1f98e62b37a0
4
- data.tar.gz: 963364abf7315212c650aa05650634dc3662e3bd1728adaaa83e89f9f94d5ad7
3
+ metadata.gz: 295daf68fcaa6ff59b760b56838278783ad0aa2faf9d0762d2f71224171f7651
4
+ data.tar.gz: 175739afc0d696ab3cee16032ab9f9559bb6dcaa2d4223f4be60875943d1870a
5
5
  SHA512:
6
- metadata.gz: 1079f1a9c85669cb0ea19a6879b9d724a389f3bf7c23dfbd716fb9fc73dffda189c1d42a9dd8674a90d7decf42037a8adc4b9f32a6718230673b0c27f5c71acd
7
- data.tar.gz: 33b784774eb1c33908807c0600af4e9113dfa28ffb957da395ed2742741db00360bb7741f80fdb94990daf08ddb768925eaf02efcd2f977b8435fe395b005ca7
6
+ metadata.gz: cc03a7970e96f640c5ab1264100968d240189f018214443f79237ec9b4f67faea585c0416e29f14f5a743a75c54d41549ee209adbf746dba5c5c94f1188a535b
7
+ data.tar.gz: ba52052e096ee65b03d7d869205f51492b825ffaa2bd1aa298d77b79b2067f766923ce02eb4ba29834bd809ba3614e096c330595f0f6bda27ee9738155b0e838
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [v2.1.2] - 2024-03-05
4
+
5
+ - refactor: Use pattern matching
6
+ - chore: Add rubocop-rake
7
+
8
+ ## [v2.1.1] - 2024-02-12
9
+
10
+ - chore: Add ruby-lsp gem
11
+ - refactor: Parse JSON with symbolize names
12
+ - refactor: Make request body with symbolize keys
13
+ - refactor: Make blocks with symbolize keys
14
+ - refactor: Extract http response checking
15
+ - test: Introduce saharspec gem
16
+
3
17
  ## [v2.1.0] - 2024-01-19
4
18
 
5
19
  - feat: Add --message-as-blocks option
@@ -7,10 +7,10 @@ module Slkecho
7
7
  def build_from_message(message, user_id = nil)
8
8
  [
9
9
  {
10
- "type" => "section",
11
- "text" => {
12
- "type" => "mrkdwn",
13
- "text" => user_id.nil? ? message : "<@#{user_id}> #{message}"
10
+ type: "section",
11
+ text: {
12
+ type: "mrkdwn",
13
+ text: user_id.nil? ? message : "<@#{user_id}> #{message}"
14
14
  }
15
15
  }
16
16
  ]
@@ -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)
@@ -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 PostMessage
@@ -22,26 +24,25 @@ module Slkecho
22
24
  end
23
25
 
24
26
  def request(params)
25
- response = @http.post(
26
- @uri.path,
27
- request_body(params).to_json,
28
- @headers
29
- )
30
- raise Slkecho::SlackApiHttpError, "#{response.code} #{response.message}" unless response.is_a?(Net::HTTPSuccess)
31
-
32
- result = JSON.parse(response.body)
33
- raise Slkecho::SlackApiResultError, result["error"] unless result["ok"]
34
-
35
- true
27
+ result = Slkecho::SlackRequest.send_request do
28
+ @http.post(@uri.path, request_body(params).to_json, @headers)
29
+ end
30
+
31
+ case result
32
+ in { ok: true }
33
+ true
34
+ in { ok: false, error: error }
35
+ raise Slkecho::SlackApiResultError, error
36
+ end
36
37
  end
37
38
 
38
39
  def request_body(params)
39
40
  {
40
- "channel" => params.channel,
41
- "blocks" => params.blocks,
42
- "username" => params.username,
43
- "icon_url" => params.icon_url,
44
- "icon_emoji" => params.icon_emoji
41
+ channel: params.channel,
42
+ blocks: params.blocks,
43
+ username: params.username,
44
+ icon_url: params.icon_url,
45
+ icon_emoji: params.icon_emoji
45
46
  }
46
47
  end
47
48
  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.1.0"
4
+ VERSION = "2.1.2"
5
5
  end
data/lib/slkecho.rb CHANGED
@@ -19,6 +19,7 @@ module Slkecho
19
19
  end
20
20
 
21
21
  class InvalidOptionError < StandardError; end
22
+ class SlackApiError < StandardError; end
22
23
  class SlackApiHttpError < StandardError; end
23
24
  class SlackApiResultError < StandardError; end
24
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.1.0
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - okonomi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-19 00:00:00.000000000 Z
11
+ date: 2024-03-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Post message to Slack like echo command.
14
14
  email:
@@ -29,6 +29,7 @@ files:
29
29
  - lib/slkecho/option_parser.rb
30
30
  - lib/slkecho/options.rb
31
31
  - lib/slkecho/slack_client.rb
32
+ - lib/slkecho/slack_request.rb
32
33
  - lib/slkecho/slack_request/lookup_user_by_email.rb
33
34
  - lib/slkecho/slack_request/post_message.rb
34
35
  - lib/slkecho/version.rb