Skip to content

Commit cfd4bac

Browse files
committed
Initial Commit
- Configuration of the gem - Send basic notifications and simple message cards
0 parents  commit cfd4bac

24 files changed

+640
-0
lines changed

.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -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

.rspec

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -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

CHANGES.md

+6
Original file line numberDiff line numberDiff line change
@@ -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

Gemfile

+6
Original file line numberDiff line numberDiff line change
@@ -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

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -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.

README.md

+67
Original file line numberDiff line numberDiff line change
@@ -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).

Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -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

bin/console

+21
Original file line numberDiff line numberDiff line change
@@ -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__)

bin/setup

+8
Original file line numberDiff line numberDiff line change
@@ -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

lib/teams_connector.rb

+39
Original file line numberDiff line numberDiff line change
@@ -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

lib/teams_connector/configuration.rb

+31
Original file line numberDiff line numberDiff line change
@@ -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

lib/teams_connector/notification.rb

+46
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -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

lib/teams_connector/post_worker.rb

+14
Original file line numberDiff line numberDiff line change
@@ -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

lib/teams_connector/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TeamsConnector
2+
VERSION = "0.1.0"
3+
end

spec/spec_helper.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require "bundler/setup"
2+
require 'webmock/rspec'
3+
require 'sidekiq'
4+
require "teams_connector"
5+
6+
RSpec.configure do |config|
7+
# Enable flags like --only-failures and --next-failure
8+
config.example_status_persistence_file_path = ".rspec_status"
9+
10+
# Disable RSpec exposing methods globally on `Module` and `main`
11+
config.disable_monkey_patching!
12+
13+
config.expect_with :rspec do |c|
14+
c.syntax = :expect
15+
end
16+
end
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
RSpec.describe TeamsConnector::Configuration do
2+
before do
3+
TeamsConnector.reset
4+
end
5+
6+
subject { TeamsConnector.configuration }
7+
8+
it "has a default configuration" do
9+
is_expected.to have_attributes(default: nil)
10+
is_expected.to have_attributes(channels: {})
11+
is_expected.to have_attributes(always_use_default: false)
12+
is_expected.to have_attributes(method: :direct)
13+
is_expected.to have_attributes(template_dir: %w[templates teams_connector])
14+
is_expected.to have_attributes(color: "3f95b5")
15+
end
16+
17+
it "has a function to add a channel" do
18+
expect(subject.channels).to be_empty
19+
expect { subject.channel :default, "https://test.url/" }.to change { subject.channels }
20+
expect(subject.channels.count).to eq 1
21+
expect(subject.channels).to include :default
22+
end
23+
24+
context "default=" do
25+
it "allows setting an existing channel as default" do
26+
subject.channel :new_default, "https.//new.default.url/"
27+
expect { subject.default = :new_default }.to change { subject.default }.from(nil).to(:new_default)
28+
end
29+
30+
it "raises an error if a non existing channel is selected as default" do
31+
expect { subject.default = :not_existing_channel }.to raise_error ArgumentError
32+
end
33+
end
34+
35+
context "method=" do
36+
it "supports direct" do
37+
subject.method = :direct
38+
expect(subject.method).to eq :direct
39+
end
40+
41+
it "supports sidekiq" do
42+
subject.method = :sidekiq
43+
expect(subject.method).to eq :sidekiq
44+
end
45+
46+
it "does not allow sidekiq when it is not available" do
47+
hide_const("Sidekiq")
48+
expect { subject.method = :sidekiq }.to raise_error ArgumentError
49+
end
50+
51+
it "does not allow invalid" do
52+
expect { subject.method = :invalid }.to raise_error ArgumentError
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)