dingtalk-robot 0.2.0 → 0.2.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: 97b1c273fef2a375e19ecd2381ddc54d2fa63e24e75e009950c7f7d90344abb6
4
- data.tar.gz: 37727da06b77aa56244ed350b6b521afee42e7a446c16caa1bff822de72d6dc7
3
+ metadata.gz: 88afedaa19a971d5d0be958775341c24a7e9cd2948b0040e3a3b3e9ad93d4049
4
+ data.tar.gz: 7d664f06b7ffed8e940652affa5cac485923b0648c5667a7e30acd26b7e13a43
5
5
  SHA512:
6
- metadata.gz: d668b0fa121bf6d758657668f4c99d0fa9604b3161a3a25c04667710d0290d906e91916c3d87f260f1ba276fb9b505608ea3f4e41ccabfdbef4f5dc1d8d242a7
7
- data.tar.gz: c132ab63e4a0eddf88f459c24c4cb3e76a25261f6bdd25b2651aad5d84aa22d9ac84851328ce8bd760580acacc4e8b0fb024a3bfbfbad9681f971a56482135e7
6
+ metadata.gz: 593af027aab117413d124be0ae9123dab63146b755e2457e0c32824ef346c13652754a05b8ab850b56d7d4362d120aca2f128fd698a7cde75be3e23184f70042
7
+ data.tar.gz: 0cbbee6efcae6624f152fc9bcaee8ee8adf7bbb34d8db0476fdf0b2c8070904b5c172ac58a823eb8971c88a861b8fb263ce88dff4bb32e19111b872e8e50f181
data/README.md CHANGED
@@ -6,6 +6,14 @@ A simple wrapper for [Dingtalk Group Robot](https://open-doc.dingtalk.com/microa
6
6
  [![Build Status](https://travis-ci.org/pinewong/dingtalk-robot.svg)](https://travis-ci.org/pinewong/dingtalk-robot)
7
7
  [![Test Coverage](https://codecov.io/github/pinewong/dingtalk-robot/coverage.svg?branch=master)](https://codecov.io/github/pinewong/dingtalk-robot?branch=master)
8
8
 
9
+ ## Supported Type
10
+
11
+ - [x] Text
12
+ - [x] Markdown
13
+ - [x] ActionCard
14
+ - [x] FeedCard
15
+ - [ ] Link
16
+
9
17
  ## Installation
10
18
 
11
19
  Add this line to your application's Gemfile:
@@ -6,6 +6,8 @@ require 'dingtalk/robot/configurable'
6
6
  require 'dingtalk/robot/errors'
7
7
  require 'dingtalk/robot/strategies/text_strategy'
8
8
  require 'dingtalk/robot/strategies/markdown_strategy'
9
+ require 'dingtalk/robot/strategies/action_card_strategy'
10
+ require 'dingtalk/robot/strategies/feed_card_strategy'
9
11
 
10
12
  module Dingtalk
11
13
  # DingTalk Group Robot
@@ -44,7 +46,7 @@ module Dingtalk
44
46
  raise ArgumentError, "Undefined channel template, channel: #{channel}, template_dir: #{config.template_dir}"
45
47
  end
46
48
  types = template_paths.map do |template_path|
47
- template_path[%r{#{config.template_dir}/#{channel}.([a-zA-Z]+).erb}, 1].to_sym
49
+ template_path[%r{#{config.template_dir}/#{channel}.([a-z_]+).erb}, 1].to_sym
48
50
  end
49
51
  types.include?(config.message_type) ? config.message_type : types.first
50
52
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dingtalk
4
+ class Robot
5
+ # Markdown type strategy for sending message
6
+ class ActionCardStrategy
7
+ def initialize(webhook_url, message)
8
+ @webhook_url = webhook_url
9
+ @message = message
10
+ end
11
+
12
+ # @option options [String] :title
13
+ # @option options [String] :single_title
14
+ # @option options [String] :single_url
15
+ # @option options [Integer] :hide_avatar (0) 0-show, 1-hide
16
+ def notify(**options)
17
+ title = options[:title].to_s
18
+ raise ArgumentError, 'title must be present, strategy: action_card' if title.empty?
19
+ single_title = options[:single_title].to_s
20
+ raise ArgumentError, 'single_title must be present, strategy: action_card' if single_title.empty?
21
+ single_url = options[:single_url].to_s
22
+ raise ArgumentError, 'single_url must be present, strategy: action_card' if single_url.empty?
23
+ hide_avatar = options[:hide_avatar].to_i
24
+ body = generate_body(title, single_title, single_url, hide_avatar)
25
+ headers = {
26
+ 'Content-Type': 'application/json',
27
+ Accept: 'application/json'
28
+ }
29
+ Net::HTTP.post(URI(webhook_url), body.to_json, headers).body
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :webhook_url, :message
35
+
36
+ def generate_body(title, single_title, single_url, hide_avatar)
37
+ {
38
+ msgtype: 'actionCard',
39
+ actionCard: {
40
+ title: title,
41
+ text: message,
42
+ singleTitle: single_title,
43
+ singleURL: single_url,
44
+ hideAvatar: hide_avatar
45
+ }
46
+ }
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dingtalk
4
+ class Robot
5
+ # Markdown type strategy for sending message
6
+ class FeedCardStrategy
7
+ Link = Struct.new(:title, :messageURL, :picURL)
8
+
9
+ def initialize(webhook_url, message)
10
+ @webhook_url = webhook_url
11
+ @message = message
12
+ end
13
+
14
+ # @option options [Array<Link>, Link] :links
15
+ def notify(**options)
16
+ links = Array.wrap(options[:links])
17
+ all_present = links.all? { |link| link.all?(&:present?) }
18
+ raise ArgumentError, 'All items of links must be present, strategy: feed_card' unless all_present
19
+ body = generate_body(links)
20
+ headers = {
21
+ 'Content-Type': 'application/json',
22
+ Accept: 'application/json'
23
+ }
24
+ Net::HTTP.post(URI(webhook_url), body.to_json, headers).body
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :webhook_url, :message
30
+
31
+ def generate_body(links)
32
+ {
33
+ msgtype: 'feedCard',
34
+ feedCard: {
35
+ links: links
36
+ }
37
+ }
38
+ end
39
+ end
40
+ end
41
+ end
@@ -9,7 +9,7 @@ module Dingtalk
9
9
  @message = message
10
10
  end
11
11
 
12
- # @option options [String] :title (required)
12
+ # @option options [String] :title
13
13
  # @option options [Array<String>] :at_mobiles
14
14
  # @option options [Boolean] :is_at_all
15
15
  def notify(**options)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dingtalk
4
4
  class Robot
5
- VERSION = '0.2.0'
5
+ VERSION = '0.2.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dingtalk-robot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pine Wong
@@ -58,6 +58,8 @@ files:
58
58
  - lib/dingtalk/robot.rb
59
59
  - lib/dingtalk/robot/configurable.rb
60
60
  - lib/dingtalk/robot/errors.rb
61
+ - lib/dingtalk/robot/strategies/action_card_strategy.rb
62
+ - lib/dingtalk/robot/strategies/feed_card_strategy.rb
61
63
  - lib/dingtalk/robot/strategies/markdown_strategy.rb
62
64
  - lib/dingtalk/robot/strategies/text_strategy.rb
63
65
  - lib/dingtalk/robot/version.rb