slack_mailer 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +37 -0
- data/README.md +90 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/slack_mailer/configuration.rb +27 -0
- data/lib/slack_mailer/delivery_worker.rb +29 -0
- data/lib/slack_mailer/version.rb +5 -0
- data/lib/slack_mailer.rb +43 -0
- data/slack_mailer.gemspec +25 -0
- metadata +113 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f7b02c1b68e24f2001a70a4e34dee2a5230139a6a10d14b0d368630a78b1886e
|
|
4
|
+
data.tar.gz: 9c6895c0a4286c029ce834fab53ed033c2faab0fb2ad7a45f69cb6c768965cd4
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: fb39ef37aa06e7898737ff6473765627bdab19079a91e2abb5aa6ebeebb445f41f9c70cd8c63b0afe46e9b699eaca1462eae7a5b9a2981f1827e934b31ab835d
|
|
7
|
+
data.tar.gz: e32b7dddd40d4a592fb0c2016ac95e0bcd0fea757537ec50ecce35a229888d251283116f9937bd732ef68e4a571f8774983a5ebcbacdaf0d0ac518126a14b5bf
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
slack_mailer (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
diff-lcs (1.3)
|
|
10
|
+
rake (10.5.0)
|
|
11
|
+
rspec (3.7.0)
|
|
12
|
+
rspec-core (~> 3.7.0)
|
|
13
|
+
rspec-expectations (~> 3.7.0)
|
|
14
|
+
rspec-mocks (~> 3.7.0)
|
|
15
|
+
rspec-core (3.7.1)
|
|
16
|
+
rspec-support (~> 3.7.0)
|
|
17
|
+
rspec-expectations (3.7.0)
|
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
19
|
+
rspec-support (~> 3.7.0)
|
|
20
|
+
rspec-mocks (3.7.0)
|
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
22
|
+
rspec-support (~> 3.7.0)
|
|
23
|
+
rspec-support (3.7.1)
|
|
24
|
+
slack-notifier (2.3.2)
|
|
25
|
+
|
|
26
|
+
PLATFORMS
|
|
27
|
+
ruby
|
|
28
|
+
|
|
29
|
+
DEPENDENCIES
|
|
30
|
+
bundler (~> 1.16.a)
|
|
31
|
+
rake (~> 10.0)
|
|
32
|
+
rspec (~> 3.0)
|
|
33
|
+
slack-notifier (~> 2.3.2)
|
|
34
|
+
slack_mailer!
|
|
35
|
+
|
|
36
|
+
BUNDLED WITH
|
|
37
|
+
1.16.0.pre.3
|
data/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# SlackMailer
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
Add this line to your application's Gemfile:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem 'slack_mailer'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle install
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install slack_mailer
|
|
18
|
+
## Dependencies
|
|
19
|
+
|
|
20
|
+
Before using game you need install [sidekiq](https://github.com/mperham/sidekiq) in to your project.
|
|
21
|
+
|
|
22
|
+
## Configuring
|
|
23
|
+
|
|
24
|
+
Add queue for sidekiq in to config/sidekiq.yml
|
|
25
|
+
|
|
26
|
+
```yml
|
|
27
|
+
- [slack_messages, 1]
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Create configuration file config/initializers/slack_mailer.rb
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
Slack::Mailer.configure do |config|
|
|
34
|
+
config.templates_path = "#{Rails.root}/app/views/slack_templates/"
|
|
35
|
+
config.templates_type = 'text'
|
|
36
|
+
config.erb_in_templates = true
|
|
37
|
+
config.slack_hook_urls = ['https://hooks.slack.com/services/...........',
|
|
38
|
+
'https://hooks.slack.com/services/...........']
|
|
39
|
+
end
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
app/slack_mailers/user_mailer.rb
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
class UserMailer < Slack::Mailer
|
|
48
|
+
|
|
49
|
+
def created(user)
|
|
50
|
+
@user = user
|
|
51
|
+
mail(to: 'channel_name', template: 'created')
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
end
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
If you want to send message via Mailer without sidekiq
|
|
58
|
+
```ruby
|
|
59
|
+
class UserMailer < Slack::Mailer
|
|
60
|
+
|
|
61
|
+
def created(user)
|
|
62
|
+
@user = user
|
|
63
|
+
mail(to: 'channel_name', template: 'created', use_sidekiq: false)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
end
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Mailer will be using template *app/views/slack_templates/user_mailer/created.text.erb*
|
|
70
|
+
|
|
71
|
+
```text
|
|
72
|
+
Name: <%= @user.name %>
|
|
73
|
+
Full name: <%= @user.full_name %>
|
|
74
|
+
Phone: <%= @user.phone %>
|
|
75
|
+
```
|
|
76
|
+
#### Sending messages
|
|
77
|
+
```ruby
|
|
78
|
+
UserMailer.created(user)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Sending small messages(one line message)
|
|
82
|
+
```ruby
|
|
83
|
+
Slack::Mailer.send_message('#channel', 'name', 'message')
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Sending message bypassing sidekiq
|
|
87
|
+
```ruby
|
|
88
|
+
Slack::Mailer.send_direct_message('#channel', 'name', 'message')
|
|
89
|
+
```
|
|
90
|
+
Also you can do this by using [slack-notifier](https://github.com/stevenosloan/slack-notifier)
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "slack_mailer"
|
|
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
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module Slack
|
|
2
|
+
class Mailer
|
|
3
|
+
class Configuration
|
|
4
|
+
|
|
5
|
+
attr_accessor :templates_path, :templates_type, :erb_in_templates, :slack_hook_urls
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@templates_path = nil
|
|
9
|
+
@templates_type = nil
|
|
10
|
+
@erb_in_templates = false
|
|
11
|
+
@slack_hook_urls = nil
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.config
|
|
15
|
+
@configuration ||= Slack::Mailer::Configuration.new
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.reset
|
|
19
|
+
@configuration = Slack::Mailer::Configuration.new
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.configure
|
|
23
|
+
yield(config)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require_relative 'configuration'
|
|
2
|
+
require 'slack-notifier'
|
|
3
|
+
require 'sidekiq'
|
|
4
|
+
|
|
5
|
+
module Slack
|
|
6
|
+
class Mailer
|
|
7
|
+
class DeliveryWorker
|
|
8
|
+
include Sidekiq::Worker
|
|
9
|
+
sidekiq_options queue: :slack_messages
|
|
10
|
+
sidekiq_options retry: Proc.new{ Slack::Mailer::Configuration.config.slack_hook_urls.length }, dead: false
|
|
11
|
+
sidekiq_retry_in { 1 }
|
|
12
|
+
sidekiq_retries_exhausted do |msg|
|
|
13
|
+
Slack::Mailer::DeliveryWorker.perform_async(msg['args'][0])
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
attr_accessor :name, :channel, :message, :url, :retry_count
|
|
17
|
+
|
|
18
|
+
def retry_count
|
|
19
|
+
@retry_count || 0
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def perform(params)
|
|
23
|
+
params['url'] = Slack::Mailer::Configuration.config.slack_hook_urls[retry_count]
|
|
24
|
+
params.each{ |attribute, value| send("#{attribute}=", value) if respond_to?(attribute) }
|
|
25
|
+
Slack::Notifier.new(url, username: name, channel: channel, link_names: 1).ping(message)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/slack_mailer.rb
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require_relative 'slack_mailer/configuration'
|
|
2
|
+
require_relative 'slack_mailer/delivery_worker'
|
|
3
|
+
require 'slack-notifier'
|
|
4
|
+
|
|
5
|
+
module Slack
|
|
6
|
+
class Mailer
|
|
7
|
+
class << self
|
|
8
|
+
|
|
9
|
+
def method_missing(method, *args)
|
|
10
|
+
self.new.send(method, *args)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def send_message(channel = '', name = '', message = '')
|
|
14
|
+
return if channel.empty? || name.empty? || message.empty?
|
|
15
|
+
Slack::Mailer::DeliveryWorker.perform_async(name: name, message: message, channel: channel)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def send_direct_message(channel = '', name = '', message = '')
|
|
19
|
+
return if channel.empty? || name.empty? || message.empty?
|
|
20
|
+
url = Slack::Mailer::Configuration.config.slack_hook_urls.mrandom
|
|
21
|
+
Slack::Notifier.new(url, username: name, channel: channel, link_names: 1).ping(message)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def mail(to: nil, template: nil, use_sidekiq: true)
|
|
27
|
+
channel = '#' << to
|
|
28
|
+
instance_variables = self.instance_variables.map { |instance_variable|
|
|
29
|
+
{ instance_variable => self.instance_variable_get(instance_variable) }
|
|
30
|
+
}.reduce(:merge)
|
|
31
|
+
message = collect_message(template, instance_variables)
|
|
32
|
+
method = use_sidekiq ? :send_message : :send_direct_message
|
|
33
|
+
self.class.send(method, channel, "#{self.class.name}##{template}", message)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def collect_message(template, instance_variables)
|
|
37
|
+
template = "#{template}.#{Slack::Mailer::Configuration.config.templates_type}"
|
|
38
|
+
template + '.erb' if Slack::Mailer::Configuration.config.erb_in_templates
|
|
39
|
+
ActionView::Base.new("#{Slack::Mailer::Configuration.config.templates_path}/#{self.class.name.underscore}",
|
|
40
|
+
{}, ActionController::Base.new).render(file: template, locals: instance_variables || {})
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "slack_mailer/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "slack_mailer"
|
|
8
|
+
spec.version = Slack::Mailer::VERSION
|
|
9
|
+
spec.authors = ["Appodeal"]
|
|
10
|
+
spec.email = ["p@appodeal.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{This gem make more comfortable working with slack}
|
|
13
|
+
spec.description = %q{}
|
|
14
|
+
spec.homepage = "https://github.com/appodeal/slack_mailer"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
|
18
|
+
end
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.16.a"
|
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
24
|
+
spec.add_dependency "slack-notifier", "~> 2.3.2"
|
|
25
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: slack_mailer
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Appodeal
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-03-16 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.16.a
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 1.16.a
|
|
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: slack-notifier
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 2.3.2
|
|
62
|
+
type: :runtime
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 2.3.2
|
|
69
|
+
description: ''
|
|
70
|
+
email:
|
|
71
|
+
- p@appodeal.com
|
|
72
|
+
executables: []
|
|
73
|
+
extensions: []
|
|
74
|
+
extra_rdoc_files: []
|
|
75
|
+
files:
|
|
76
|
+
- ".gitignore"
|
|
77
|
+
- ".rspec"
|
|
78
|
+
- ".travis.yml"
|
|
79
|
+
- Gemfile
|
|
80
|
+
- Gemfile.lock
|
|
81
|
+
- README.md
|
|
82
|
+
- Rakefile
|
|
83
|
+
- bin/console
|
|
84
|
+
- bin/setup
|
|
85
|
+
- lib/slack_mailer.rb
|
|
86
|
+
- lib/slack_mailer/configuration.rb
|
|
87
|
+
- lib/slack_mailer/delivery_worker.rb
|
|
88
|
+
- lib/slack_mailer/version.rb
|
|
89
|
+
- slack_mailer.gemspec
|
|
90
|
+
homepage: https://github.com/appodeal/slack_mailer
|
|
91
|
+
licenses: []
|
|
92
|
+
metadata: {}
|
|
93
|
+
post_install_message:
|
|
94
|
+
rdoc_options: []
|
|
95
|
+
require_paths:
|
|
96
|
+
- lib
|
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
|
+
requirements:
|
|
99
|
+
- - ">="
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: '0'
|
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
|
+
requirements:
|
|
104
|
+
- - ">="
|
|
105
|
+
- !ruby/object:Gem::Version
|
|
106
|
+
version: '0'
|
|
107
|
+
requirements: []
|
|
108
|
+
rubyforge_project:
|
|
109
|
+
rubygems_version: 2.7.6
|
|
110
|
+
signing_key:
|
|
111
|
+
specification_version: 4
|
|
112
|
+
summary: This gem make more comfortable working with slack
|
|
113
|
+
test_files: []
|