dingding-notifier 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1ceb6e487d1209fc40bc09dcbd636a17e72ee747c26723ecee803b8a6d65e57b
4
+ data.tar.gz: 79608035fc7d82173176a9ada5420493974003a676fc73cf1805f50966ec9384
5
+ SHA512:
6
+ metadata.gz: fc956b87f5e006fc3be07c893dccf4aff33f4d573b2e2b13bb924d782e95befd2a498435434bfec61b50f80bb2f03b733c73d4752a66af53c4e541a86194ddc3
7
+ data.tar.gz: f80d13798823c654bead04a8d9e0a52ca584d9991af210c1d19765a0933a79414d5966d20195c3772b9a14846dfcb487c4c895116a38fb0fd7a18dfdbd0fcb7c
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ *.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-08-30
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at bineanshi@hotmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ ruby '2.3.8'
6
+ # Specify your gem's dependencies in dingding-notifier.gemspec
7
+ gemspec
8
+
9
+
10
+ group :development do
11
+ gem 'byebug', '~> 11.0'
12
+ gem 'pry-byebug', '~> 3.7'
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 bineanshi
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,43 @@
1
+ # Dingding
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dingding-notifier`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'dingding-notifier'
13
+ ```
14
+ a = Dingding::Notifier.new("https://oapi.dingtalk.com/robot/send?access_token=8b8d91229b22facb61f0166be307fdd9ed67c1725c2ab20a2e1bbaffdd3ed740")
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dingding-notifier
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dingding. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/dingding/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Dingding project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dingding/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "dingding-notifier"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+
11
+ require "irb"
12
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/dingding-notifier/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "dingding-notifier"
7
+ spec.version = Dingding::Notifier::VERSION
8
+ spec.authors = ["bineanshi"]
9
+ spec.email = ["bineanshi@sina.com"]
10
+
11
+ spec.summary = "Write a short summary, because RubyGems requires one."
12
+ spec.description = "Write a longer description or delete this line."
13
+ spec.homepage = "https://gitee.com/gems/ntshibin/dingding-notifier"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.3.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://gitee.com/ntshibin/dingding-notifier"
19
+ spec.metadata["changelog_uri"] = "https://gitee.com/ntshibin/dingding-notifier/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ spec.add_dependency 'eventmachine', '~> 1.2', '>= 1.2.7'
30
+ spec.add_dependency 'faraday', '~> 0.17.4', '>= 0.17.4'
31
+ spec.add_dependency 'faraday_middleware','~> 0.14.0', '>= 0.14.0'
32
+ spec.add_dependency 'faraday-rack', '~> 1.0' if RUBY_VERSION >= "2.4"
33
+ spec.add_dependency 'hashie', '~> 4.1'
34
+ spec.add_dependency 'json', '~> 2.5', '>= 2.5.1'
35
+ spec.add_dependency 'mime-types', '~> 3.3', '>= 3.3.1'
36
+ spec.add_dependency 'multi_json', '~> 1.15'
37
+
38
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dingding
4
+ class Notifier
5
+ class Config
6
+ def initialize
7
+ @http_client = Util::HTTPClient
8
+ @middleware = %i[
9
+ format_text
10
+ format_link
11
+ format_markdown
12
+ format_action_card
13
+ format_feed_card
14
+ ]
15
+ end
16
+
17
+ def http_client client=nil
18
+ return @http_client if client.nil?
19
+ raise ArgumentError, "the http client must respond to ::post" unless client.respond_to?(:post)
20
+
21
+ @http_client = client
22
+ end
23
+
24
+ def middleware *args
25
+ args.delete_if{|a| a.nil?}
26
+ return [:format_text] if args.empty? || args.nil?
27
+
28
+ @middleware =
29
+ if args.length == 1 && args.first.is_a?(Array) || args.first.is_a?(Hash)
30
+ args.first
31
+ else
32
+ args
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+ module Dingding
3
+ class Notifier
4
+ class PayloadMiddleware
5
+ class Base
6
+ class << self
7
+ def middleware_name name
8
+ PayloadMiddleware.register self, name.to_sym
9
+ end
10
+
11
+ def options default_opts
12
+ @default_opts = default_opts
13
+ end
14
+
15
+ def default_opts
16
+ @default_opts ||= {}
17
+ end
18
+ end
19
+
20
+ attr_reader :notifier, :options
21
+
22
+ def initialize notifier, opts={}
23
+ @notifier = notifier
24
+ @options = self.class.default_opts.merge opts
25
+ end
26
+
27
+ def call _payload={}
28
+ raise NoMethodError, "method `call` not defined for class #{self.class}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+ module Dingding
3
+ class Notifier
4
+ class PayloadMiddleware
5
+ class FormatActionCard < Base
6
+ middleware_name :format_action_card
7
+
8
+ options formats: {
9
+ at: {
10
+ atMobiles:[
11
+ ],
12
+ atUserIds:[
13
+ ],
14
+ isAtAll: false
15
+ },
16
+ actionCard: {
17
+ # title: "",
18
+ # text: "",
19
+ # btnOrientation: "0",
20
+ # 整体
21
+ # singleTitle: "",
22
+ # singleURL: "",
23
+ # 独立
24
+ # btns: [],
25
+ # actionURL: "",
26
+ },
27
+ msgtype: "actionCard"
28
+ }.freeze
29
+
30
+
31
+
32
+ def call payload={}
33
+
34
+ return payload unless payload[:format_action_card]
35
+ tmp = options[:formats].dup
36
+ tmp[:at][:atMobiles] = payload[:atMobiles] if payload.has_key?(:atMobiles)
37
+ tmp[:at][:atUserIds] = payload[:atUserIds] if payload.has_key?(:atUserIds)
38
+ tmp[:at][:isAtAll] = payload[:isAtAll] if payload.has_key?(:isAtAll)
39
+ tmp[:actionCard] = payload[:format_action_card] if payload.has_key?(:format_action_card)
40
+ tmp
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+ module Dingding
3
+ class Notifier
4
+ class PayloadMiddleware
5
+ class FormatFeedCard < Base
6
+ middleware_name :format_feed_card
7
+
8
+ options formats: {
9
+ at: {
10
+ atMobiles:[
11
+ ],
12
+ atUserIds:[
13
+ ],
14
+ isAtAll: false
15
+ },
16
+ feedCard: {
17
+ links: [
18
+ {
19
+ title: "",
20
+ messageURL: "",
21
+ picURL: ""
22
+ }
23
+ ]
24
+ },
25
+ msgtype: "feedCard"
26
+ }.freeze
27
+
28
+
29
+
30
+ def call payload={}
31
+
32
+ return payload unless payload[:format_feed_card]
33
+ tmp = options[:formats].dup
34
+ tmp[:at][:atMobiles] = payload[:atMobiles] if payload.has_key?(:atMobiles)
35
+ tmp[:at][:atUserIds] = payload[:atUserIds] if payload.has_key?(:atUserIds)
36
+ tmp[:at][:isAtAll] = payload[:isAtAll] if payload.has_key?(:isAtAll)
37
+ tmp[:feedCard] = payload[:format_feed_card] if payload.has_key?(:format_feed_card)
38
+ tmp
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+ module Dingding
3
+ class Notifier
4
+ class PayloadMiddleware
5
+ class FormatLink < Base
6
+ middleware_name :format_link
7
+
8
+ options formats: {
9
+ at: {
10
+ atMobiles:[
11
+ ],
12
+ atUserIds:[
13
+ ],
14
+ isAtAll: false
15
+ },
16
+ link: {
17
+ text: "",
18
+ title: "",
19
+ picUrl: "",
20
+ messageUrl: ""
21
+ },
22
+ msgtype: "link"
23
+ }.freeze
24
+
25
+ def call payload={}
26
+
27
+ return payload unless payload[:format_link]
28
+ tmp = options[:formats].dup
29
+ tmp[:at][:atMobiles] = payload[:atMobiles] if payload.has_key?(:atMobiles)
30
+ tmp[:at][:atUserIds] = payload[:atUserIds] if payload.has_key?(:atUserIds)
31
+ tmp[:at][:isAtAll] = payload[:isAtAll] if payload.has_key?(:isAtAll)
32
+ tmp[:link] = payload[:format_link] if payload.has_key?(:format_link)
33
+ tmp
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+ module Dingding
3
+ class Notifier
4
+ class PayloadMiddleware
5
+ class FormatMarkdown < Base
6
+ middleware_name :format_markdown
7
+
8
+ options formats: {
9
+ at: {
10
+ atMobiles:[
11
+ ],
12
+ atUserIds:[
13
+ ],
14
+ isAtAll: false
15
+ },
16
+ markdown: {
17
+ title:"",
18
+ text: ""
19
+ },
20
+ msgtype: "markdown"
21
+ }.freeze
22
+
23
+ def call payload={}
24
+
25
+ return payload unless payload[:format_markdown]
26
+ tmp = options[:formats].dup
27
+
28
+ tmp[:at][:atUserIds] = payload[:atUserIds] if payload.has_key?(:atUserIds)
29
+ tmp[:at][:isAtAll] = payload[:isAtAll] if payload.has_key?(:isAtAll)
30
+ tmp[:markdown] = payload[:format_markdown] if payload.has_key?(:format_markdown)
31
+ if payload.has_key?(:atMobiles)
32
+ tmp[:at][:atMobiles] = payload[:atMobiles]
33
+ tmp[:markdown][:text] = "#### #{tmp[:markdown][:title]} #{payload[:atMobiles].map{|a| "@#{a} "}.join(' ')} \n #{tmp[:markdown][:text]}"
34
+ end
35
+ tmp
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+ module Dingding
3
+ class Notifier
4
+ class PayloadMiddleware
5
+ class FormatText < Base
6
+ middleware_name :format_text
7
+
8
+ options formats: {
9
+ at: {
10
+ atMobiles:[
11
+ ],
12
+ atUserIds:[
13
+ ],
14
+ isAtAll: false
15
+ },
16
+ text: {
17
+ content: nil
18
+ },
19
+ msgtype: "text"
20
+ }.freeze
21
+
22
+ def call payload={}
23
+
24
+ return payload unless payload[:format_text]
25
+ tmp = options[:formats].dup
26
+ tmp[:at][:atMobiles] = payload[:atMobiles] if payload.has_key?(:atMobiles)
27
+ tmp[:at][:atUserIds] = payload[:atUserIds] if payload.has_key?(:atUserIds)
28
+ tmp[:at][:isAtAll] = payload[:isAtAll] if payload.has_key?(:isAtAll)
29
+ tmp[:text][:content] = payload[:format_text][:text].to_s if payload.has_key?(:format_text)
30
+ tmp
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dingding
4
+ class Notifier
5
+ class PayloadMiddleware
6
+ class Stack
7
+ attr_reader :notifier,
8
+ :stack
9
+
10
+ def initialize notifier
11
+ @notifier = notifier
12
+ @stack = []
13
+ end
14
+
15
+ def set *middlewares
16
+
17
+ middlewares =
18
+ if middlewares.length == 1 && middlewares.first.is_a?(Hash)
19
+ middlewares.first
20
+ else
21
+ middlewares.flatten
22
+ end
23
+ @stack = middlewares.map do |key, opts|
24
+ PayloadMiddleware.registry.fetch(key).new(*[notifier, opts].compact)
25
+ end
26
+ end
27
+
28
+ def call payload={}
29
+ result = stack.inject payload do |pld, middleware|
30
+ middleware.call(pld)
31
+ end
32
+ result
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dingding
4
+ class Notifier
5
+ class PayloadMiddleware
6
+ class << self
7
+ def registry
8
+ @registry ||= {}
9
+ end
10
+
11
+ def register middleware, name
12
+ registry[name] = middleware
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ require_relative "payload_middleware/stack"
20
+ require_relative "payload_middleware/base"
21
+ require_relative "payload_middleware/format_text"
22
+ require_relative "payload_middleware/format_link"
23
+ require_relative "payload_middleware/format_markdown"
24
+ require_relative "payload_middleware/format_action_card"
25
+ require_relative "payload_middleware/format_feed_card"
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+ require 'json'
6
+ require 'uri'
7
+
8
+ module Dingding
9
+ class Notifier
10
+ class APIError < StandardError
11
+ attr_reader :response
12
+
13
+ def initialize(response)
14
+ @response = response
15
+ end
16
+
17
+ def message
18
+ <<-MSG
19
+ The slack API returned an error: #{@response.body} (HTTP Code #{@response.code})
20
+ Check the "Handling Errors" section on https://api.slack.com/incoming-webhooks for more information
21
+ MSG
22
+ end
23
+ end
24
+
25
+ module Util
26
+ class HTTPClient
27
+ class << self
28
+ def post uri, params
29
+ HTTPClient.new(uri, params).call
30
+ end
31
+ end
32
+
33
+ attr_reader :uri, :params
34
+
35
+ def initialize uri, params
36
+ @uri = uri
37
+ @params = params
38
+ end
39
+
40
+ # rubocop:disable Layout/IndentHeredoc
41
+ def call
42
+ response = Faraday.post(uri, params,{"Content-Type" => "application/json"})
43
+ # http_obj.request(request_obj).tap do |response|
44
+ # unless response.is_a?(Net::HTTPSuccess)
45
+ # raise Dingding::Notifier::APIError.new(response)
46
+ # end
47
+ # end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dingding
4
+ class Notifier
5
+ VERSION = "0.0.1".freeze # rubocop:disable Style/RedundantFreeze
6
+ end
7
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "json"
5
+
6
+ require_relative "dingding-notifier/util/http_client"
7
+
8
+ require_relative "dingding-notifier/payload_middleware"
9
+ require_relative "dingding-notifier/config"
10
+
11
+ require 'pry-byebug'
12
+
13
+ module Dingding
14
+ class Notifier
15
+ attr_reader :webhook_url, :client, :atMobiles, :atUserIds, :isAtAll, :middleware_name
16
+
17
+ def initialize(options={})
18
+ @webhook_url = options.delete(:webhook_url)
19
+ @client = config.http_client
20
+ @atMobiles = options.delete(:atMobiles) || []
21
+ @atUserIds = options.delete(:atUserIds) || []
22
+ @isAtAll = options.delete(:isAtAll) || false
23
+ @middleware_name = config.middleware(options.delete(:middleware))
24
+ if @middleware_name.is_a?(Array)
25
+ @middleware_name = @middleware_name.first
26
+ end
27
+ middleware.set (@middleware_name)
28
+ end
29
+
30
+ def config
31
+ @_config ||= Config.new
32
+ end
33
+
34
+ def call message, options={}
35
+ options[:format_text] ||= {}
36
+ options[:format_text][:text] = message.to_json
37
+ post options
38
+ end
39
+
40
+ def post payload={}
41
+ payload = {middleware_name.to_sym => payload} unless payload.key?(middleware_name)
42
+ payload.merge!({atMobiles: atMobiles, atUserIds: atUserIds,isAtAll: isAtAll})
43
+ client.post webhook_url, middleware.call(payload).to_json
44
+ end
45
+
46
+ private
47
+
48
+ def middleware
49
+ @middleware ||= PayloadMiddleware::Stack.new(self)
50
+ end
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dingding-notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - bineanshi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eventmachine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.7
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.2.7
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 0.17.4
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: 0.17.4
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 0.17.4
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: 0.17.4
53
+ - !ruby/object:Gem::Dependency
54
+ name: faraday_middleware
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 0.14.0
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.14.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 0.14.0
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: 0.14.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: hashie
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '4.1'
80
+ type: :runtime
81
+ prerelease: false
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - "~>"
85
+ - !ruby/object:Gem::Version
86
+ version: '4.1'
87
+ - !ruby/object:Gem::Dependency
88
+ name: json
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '2.5'
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 2.5.1
97
+ type: :runtime
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.5'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 2.5.1
107
+ - !ruby/object:Gem::Dependency
108
+ name: mime-types
109
+ requirement: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: '3.3'
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 3.3.1
117
+ type: :runtime
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '3.3'
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 3.3.1
127
+ - !ruby/object:Gem::Dependency
128
+ name: multi_json
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - "~>"
132
+ - !ruby/object:Gem::Version
133
+ version: '1.15'
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - "~>"
139
+ - !ruby/object:Gem::Version
140
+ version: '1.15'
141
+ description: Write a longer description or delete this line.
142
+ email:
143
+ - bineanshi@sina.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - ".gitignore"
149
+ - CHANGELOG.md
150
+ - CODE_OF_CONDUCT.md
151
+ - Gemfile
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - bin/console
156
+ - bin/setup
157
+ - dingding-notifier.gemspec
158
+ - lib/dingding-notifier.rb
159
+ - lib/dingding-notifier/config.rb
160
+ - lib/dingding-notifier/payload_middleware.rb
161
+ - lib/dingding-notifier/payload_middleware/base.rb
162
+ - lib/dingding-notifier/payload_middleware/format_action_card.rb
163
+ - lib/dingding-notifier/payload_middleware/format_feed_card.rb
164
+ - lib/dingding-notifier/payload_middleware/format_link.rb
165
+ - lib/dingding-notifier/payload_middleware/format_markdown.rb
166
+ - lib/dingding-notifier/payload_middleware/format_text.rb
167
+ - lib/dingding-notifier/payload_middleware/stack.rb
168
+ - lib/dingding-notifier/util/http_client.rb
169
+ - lib/dingding-notifier/version.rb
170
+ homepage: https://gitee.com/gems/ntshibin/dingding-notifier
171
+ licenses:
172
+ - MIT
173
+ metadata:
174
+ homepage_uri: https://gitee.com/gems/ntshibin/dingding-notifier
175
+ source_code_uri: https://gitee.com/ntshibin/dingding-notifier
176
+ changelog_uri: https://gitee.com/ntshibin/dingding-notifier/CHANGELOG.md
177
+ post_install_message:
178
+ rdoc_options: []
179
+ require_paths:
180
+ - lib
181
+ required_ruby_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: 2.3.0
186
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ requirements: []
192
+ rubygems_version: 3.0.9
193
+ signing_key:
194
+ specification_version: 4
195
+ summary: Write a short summary, because RubyGems requires one.
196
+ test_files: []