teams_connector 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1637a8989e80ccddbd794e906543c29845f701ae2aa2241728cfc27c42eb4005
4
+ data.tar.gz: 4396ee9a4a861078e29a84b7e4f372505ad397d71ab4b93082476c40d9e984d5
5
+ SHA512:
6
+ metadata.gz: 074b2bb7966fd01e514c8c089335e67ab8cdb643a64cef413d744307f441b6acd4523beba610caf5345383a6043b8ab80166d20f66e7bdec6762d21b8c1a8c3c
7
+ data.tar.gz: 010c32f3c62b14102603b3196124a671630d38c9d18d9a661dfb10324f9b1ebf47190d9e8098fa925ba013d29705fe9f80b2d25178a0a13149308c1d2b46d26b
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ /.idea/
11
+
12
+ Gemfile.lock
13
+
14
+ # rspec failure tracking
15
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.6
7
+ before_install: gem install bundler -v 1.17.3
data/CHANGES.md ADDED
@@ -0,0 +1,6 @@
1
+ # Teams Connector Changelog
2
+
3
+ 0.1.0
4
+ ---
5
+ - Initial commit of the Teams Connector gem
6
+ - Send messages as cards defined by JSON templates to configured Microsoft Teams channels
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in teams_connector.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Qurasoft GmbH
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,67 @@
1
+ # Teams Connector
2
+
3
+ Welcome to Teams Connector. This gem allows you to easily send messages from your ruby project to Microsoft Teams channels.
4
+ It integrates in your rails project, when you are using bundler or even in plain ruby projects.
5
+
6
+ The messages can be send synchronous or asynchronous with [Sidekiq](https://github.com/mperham/sidekiq).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'teams_connector'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install teams_connector
23
+
24
+ ## Usage
25
+ After setting up the Incoming Webhook Connector four your Microsoft Teams channel, it is as simple as configuring the channel and creating a new `TeamsConnector::Notification`.
26
+
27
+ ```ruby
28
+ # Configuration
29
+ TeamsConnector.configure do |config|
30
+ config.channel :channel_id, "https://<YOUR COMPLETE WEBHOOK URL GOES HERE>"
31
+ end
32
+
33
+ # Send a test card to your channel
34
+ TeamsConnector::Notification.new(:test_card, :channel_id).deliver_later
35
+
36
+ # Send a card with a list of facts
37
+ content = {
38
+ title: "Teams Connector Readme",
39
+ subtitle: "A list of facts",
40
+ facts: {
41
+ "Usage": "Testing the facts Card"
42
+ }
43
+ }
44
+ TeamsConnector::Notification::Message.new(:facts_card, "This is a summary", content).deliver_later
45
+ ```
46
+ This gem provides some basic templates in its default template path. You can also define your own templates in your own path. The default templates will be still available so you can mix and match.
47
+
48
+ ### Default templates
49
+
50
+ Template name | Description
51
+ -----|-------
52
+ :test_card | A simple text message without any configurable content for testing
53
+ :facts_card | A card with title, subtitle and a list of facts
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/qurasoft/teams_connector. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
64
+
65
+ ## License
66
+
67
+ 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,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "teams_connector"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ TeamsConnector.configure do |config|
14
+ config.channel :default, "<INSERT YOUR WEBHOOK URL HERE>"
15
+ config.default = :default
16
+ config.always_use_default = true
17
+ config.method = :direct
18
+ end
19
+
20
+ require "irb"
21
+ 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,31 @@
1
+ module TeamsConnector
2
+ class Configuration
3
+ DEFAULT_TEMPLATE_DIR = %w[templates teams_connector]
4
+
5
+ attr_accessor :default, :channels, :always_use_default, :method, :template_dir, :color
6
+
7
+ def initialize
8
+ @default = nil
9
+ @channels = {}
10
+ @always_use_default = false
11
+ @method = :direct
12
+ @template_dir = DEFAULT_TEMPLATE_DIR
13
+ @color = "3f95b5"
14
+ end
15
+
16
+ def default=(channel)
17
+ raise ArgumentError, "Desired default channel '#{channel}' is not configured" unless @channels.key?(channel)
18
+ @default = channel
19
+ end
20
+
21
+ def method=(method)
22
+ raise ArgumentError, "Method '#{method.to_s}' is not supported" unless [:direct, :sidekiq].include? method
23
+ raise ArgumentError, "Sidekiq is not available" if method == :sidekiq && !defined? Sidekiq
24
+ @method = method
25
+ end
26
+
27
+ def channel(name, url)
28
+ @channels[name] = url;
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ module TeamsConnector
2
+ class Notification::Message < Notification
3
+ attr_accessor :summary, :content
4
+
5
+ def initialize(template, summary, content = {}, channel = TeamsConnector.configuration.default)
6
+ super(template, channel)
7
+ @summary = summary
8
+ @content = content
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,46 @@
1
+ require 'erb'
2
+ require 'net/http'
3
+ require 'teams_connector/post_worker' if defined? Sidekiq
4
+
5
+ module TeamsConnector
6
+ class Notification
7
+ attr_accessor :template, :channel
8
+
9
+ def initialize(template, channel)
10
+ @template = template
11
+ @channel = channel
12
+ end
13
+
14
+ def deliver_later
15
+ template_path = find_template
16
+
17
+ renderer = ERB.new(File.read(template_path))
18
+ renderer.location = [template_path.to_s, 0]
19
+
20
+ url = TeamsConnector.configuration.channels[@channel]
21
+ url = TeamsConnector.configuration.channels[TeamsConnector.configuration.default] if TeamsConnector.configuration.always_use_default
22
+ raise ArgumentError, "The Teams channel '#{@channel}' is not available in the configuration." if url.nil?
23
+
24
+ content = renderer.result(binding)
25
+
26
+ if TeamsConnector.configuration.method == :sidekiq
27
+ TeamsConnector::PostWorker.perform_async(url, content)
28
+ else
29
+ response = Net::HTTP.post(URI(url), content, { "Content-Type": "application/json" })
30
+ response.value
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def find_template
37
+ path = File.join(TeamsConnector::project_root, *TeamsConnector.configuration.template_dir, "#{@template.to_s}.json.erb")
38
+ unless File.exist? path
39
+ path = File.join(TeamsConnector::gem_root, *TeamsConnector::Configuration::DEFAULT_TEMPLATE_DIR, "#{@template.to_s}.json.erb")
40
+ end
41
+ raise ArgumentError, "The template '#{@template}' is not available." unless File.exist? path
42
+
43
+ path
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ require 'net/http'
2
+ require 'sidekiq/worker'
3
+
4
+ module TeamsConnector
5
+ class PostWorker
6
+ # === Includes ===
7
+ include Sidekiq::Worker
8
+
9
+ def perform(url, content)
10
+ response = Net::HTTP.post(URI(url), content, { "Content-Type": "application/json" })
11
+ response.value
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module TeamsConnector
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,39 @@
1
+ require 'teams_connector/configuration'
2
+ require 'teams_connector/version'
3
+ require 'teams_connector/notification'
4
+ require 'teams_connector/notification/message'
5
+
6
+ module TeamsConnector
7
+ class << self
8
+ attr_accessor :configuration
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.reset
16
+ @configuration = Configuration.new
17
+ end
18
+
19
+ def self.configure
20
+ yield configuration
21
+ end
22
+
23
+ def self.project_root
24
+ if defined?(Rails)
25
+ return Rails.root
26
+ end
27
+
28
+ if defined?(Bundler)
29
+ return Bundler.root
30
+ end
31
+
32
+ Dir.pwd
33
+ end
34
+
35
+ def self.gem_root
36
+ spec = Gem::Specification.find_by_name("teams_connector")
37
+ spec.gem_dir rescue project_root
38
+ end
39
+ end
@@ -0,0 +1,42 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "teams_connector/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "teams_connector"
8
+ spec.version = TeamsConnector::VERSION
9
+ spec.authors = ["Lucas Keune"]
10
+ spec.email = ["lucas.keune@qurasoft.de"]
11
+
12
+ spec.summary = %q{Simple connector to send messages and cards to Microsoft Teams channels}
13
+ spec.description = %q{Send templated messages or adaptive cards to Microsoft Teams channels}
14
+ spec.homepage = "https://github.com/Qurasoft/teams_connector"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/Qurasoft/teams_connector"
22
+ spec.metadata["changelog_uri"] = "https://github.com/Qurasoft/teams_connector/blob/main/CHANGES.md"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.17"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "rspec", "~> 3.0"
39
+ spec.add_development_dependency "webmock"
40
+ spec.add_development_dependency "sidekiq"
41
+ spec.add_development_dependency "simplecov"
42
+ end
@@ -0,0 +1,18 @@
1
+ {
2
+ "@type": "MessageCard",
3
+ "@context": "http://schema.org/extensions",
4
+ "themeColor": "<%= TeamsConnector.configuration.color %>>",
5
+ "summary": "<%= @summary %>",
6
+ "sections": [
7
+ {
8
+ "markdown": true,
9
+ "activityTitle": "<%= @content[:title] %>",
10
+ "activitySubtitle": "<%= @content[:subtitle] %>",
11
+ <% if @content[:facts].present? %>
12
+ "facts": [
13
+ <%= @content[:facts].map {|k| {name: k[0], value: k[1]}.to_json}.join(", ") %>
14
+ ]
15
+ <% end %>
16
+ }
17
+ ]
18
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "@type": "MessageCard",
3
+ "@context": "http://schema.org/extensions",
4
+ "themeColor": "3f95b5",
5
+ "summary": "This is a test summary",
6
+ "sections": [
7
+ {
8
+ "markdown": true,
9
+ "activityTitle": "Quokka",
10
+ "activitySubtitle": "About the short-tailed scrub wallaby",
11
+ "facts": [
12
+ {
13
+ "name": "Fun fact",
14
+ "value": "Are always smiling and the 'happiest' animals on earth."
15
+ }
16
+ ]
17
+ }
18
+ ]
19
+ }
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: teams_connector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Keune
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sidekiq
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Send templated messages or adaptive cards to Microsoft Teams channels
98
+ email:
99
+ - lucas.keune@qurasoft.de
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CHANGES.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/teams_connector.rb
115
+ - lib/teams_connector/configuration.rb
116
+ - lib/teams_connector/notification.rb
117
+ - lib/teams_connector/notification/message.rb
118
+ - lib/teams_connector/post_worker.rb
119
+ - lib/teams_connector/version.rb
120
+ - teams_connector.gemspec
121
+ - templates/teams_connector/facts_card.json.erb
122
+ - templates/teams_connector/test_card.json.erb
123
+ homepage: https://github.com/Qurasoft/teams_connector
124
+ licenses:
125
+ - MIT
126
+ metadata:
127
+ homepage_uri: https://github.com/Qurasoft/teams_connector
128
+ source_code_uri: https://github.com/Qurasoft/teams_connector
129
+ changelog_uri: https://github.com/Qurasoft/teams_connector/blob/main/CHANGES.md
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubygems_version: 3.0.9
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Simple connector to send messages and cards to Microsoft Teams channels
149
+ test_files: []