slkecho 2.1.0 → 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: 1aa38817dd7a85a9bb6a39924da65ad1d45173c29b058347481c1f98e62b37a0
4
- data.tar.gz: 963364abf7315212c650aa05650634dc3662e3bd1728adaaa83e89f9f94d5ad7
3
+ metadata.gz: 4316f376c8717965ab2c3518c054be6d983bc005d343803305bdb90f2bc0b963
4
+ data.tar.gz: 11cf2ec4be6b49b6a4d4d566ac9c767036ae91871c3ebe39e4a760198f13e367
5
5
  SHA512:
6
- metadata.gz: 1079f1a9c85669cb0ea19a6879b9d724a389f3bf7c23dfbd716fb9fc73dffda189c1d42a9dd8674a90d7decf42037a8adc4b9f32a6718230673b0c27f5c71acd
7
- data.tar.gz: 33b784774eb1c33908807c0600af4e9113dfa28ffb957da395ed2742741db00360bb7741f80fdb94990daf08ddb768925eaf02efcd2f977b8435fe395b005ca7
6
+ metadata.gz: 25a8acf5fd4f84c7d76b91ac9a44e99e780e398c0b878dac33352e621e5139dc09d9d13b041eaec04227b20c7996596799656b5cf367195d65aeb10b959507e2
7
+ data.tar.gz: da7a9a3a06f06910e7660f5408c0a9c9a3b1d0f057c02ad88b5c9bc10ba96f559141b956659a6993b6b04c611a6fde5f0b26a06998c39ad9651270ad21d877f0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
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
+
3
12
  ## [v2.1.0] - 2024-01-19
4
13
 
5
14
  - 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,26 @@ 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)
27
+ result = Slkecho::SlackRequest.send_request do
28
+ @http.post(
29
+ @uri.path,
30
+ request_body(params).to_json,
31
+ @headers
32
+ )
33
+ end
31
34
 
32
- result = JSON.parse(response.body)
33
- raise Slkecho::SlackApiResultError, result["error"] unless result["ok"]
35
+ raise Slkecho::SlackApiResultError, result[:error] unless result[:ok]
34
36
 
35
37
  true
36
38
  end
37
39
 
38
40
  def request_body(params)
39
41
  {
40
- "channel" => params.channel,
41
- "blocks" => params.blocks,
42
- "username" => params.username,
43
- "icon_url" => params.icon_url,
44
- "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
45
47
  }
46
48
  end
47
49
  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.1"
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.1
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-02-12 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