lita-mailgun 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 837eed2e55191540ef1dae2727f12a5cbb9c12c4
4
+ data.tar.gz: 80708d792fcd7dd1a2b471f0a78d606e82c123ec
5
+ SHA512:
6
+ metadata.gz: 6fecb1ebd3d401561b1f568df8bfbafb36917d617fc5f28de3af25620fe1d8b3a6da7e6e65b4e9e75f416694c691de599de7e94cec1eab97f41d94eb17d2b1f8
7
+ data.tar.gz: e7e54c9cfa8cef4690f87dac22617f484dc214612ede1741aa47c94023cff38965132ea87c4a512800b2a19edeb50488288eaf63ac698d2c21a49d0714e58663
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 James Healy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # lita-mailgun
2
+
3
+ A lita plugin for interacting with mailgun.com.
4
+
5
+ ## Installation
6
+
7
+ Add this gem to your lita installation by including the following line in your Gemfile:
8
+
9
+ gem "lita-mailgun"
10
+
11
+ ## Externally triggered events
12
+
13
+ This handler can accept mailgun events and trigger a variety of activities as appropriate. To
14
+ get started, use the mailgun web interface to configure a webhook that POSTs events to:
15
+
16
+ http://your-lita-bot.com/mailgun
17
+
18
+ ### Dropped Email Reports
19
+
20
+ To print a warning when a high number of recent emails to a domain were
21
+ dropped, edit your lita\_config.rb to include the following line.
22
+
23
+ config.handlers.mailgun_droppped_rate.channel_name = "channel-name"
24
+
25
+ The warnings will look something like this:
26
+
27
+ [mailgun] [bigpond.net.au] 8/10 (80.0%) recent emails dropped
28
+
29
+ ## Chat commands
30
+
31
+ This handler provides no additional chat commands. Yet.
32
+
33
+ ## TODO
34
+
35
+ Possible ideas for new features, either via chat commands or externally triggered events:
36
+
37
+ * maybe set a maximum period of time to store the event data?
@@ -0,0 +1,4 @@
1
+ require "lita/mailgun"
2
+ require "lita/mailgun_event"
3
+ require "lita/mailgun_dropped_rate"
4
+ require "lita/mailgun_dropped_rate_repository"
@@ -0,0 +1,21 @@
1
+ require "lita"
2
+ require "lita/mailgun_event"
3
+
4
+ module Lita
5
+ module Handlers
6
+ # Receives buildkite webhooks and emits them onto the lita event bus
7
+ # so other handlers can do their thing
8
+ class Mailgun < Handler
9
+
10
+ http.post "/mailgun", :mailgun_event
11
+
12
+ def mailgun_event(request, response)
13
+ event = MailgunEvent.new(request.params)
14
+ robot.trigger(:mailgun_event, event: event)
15
+ end
16
+
17
+ end
18
+
19
+ Lita.register_handler(Mailgun)
20
+ end
21
+ end
@@ -0,0 +1,51 @@
1
+ require "lita"
2
+ require 'lita/mailgun_event'
3
+ require 'lita-timing'
4
+
5
+ module Lita
6
+ module Handlers
7
+ # Watch mailgun webhooks for successful and dropped deliveries, and report any domains
8
+ # with a high dropped rate
9
+ class MailgunDroppedRate < Handler
10
+ ONE_HOUR = 60 * 60
11
+ REPORTING_THRESHOLD = 50 # percent
12
+
13
+ config :channel_name
14
+
15
+ on :mailgun_event, :monitor_event
16
+
17
+ def monitor_event(payload)
18
+ event = payload[:event]
19
+
20
+ repository.record(event.recipient_domain, event.name.to_sym)
21
+
22
+ result = repository.dropped_rate(event.recipient_domain)
23
+ if result.dropped_rate > REPORTING_THRESHOLD
24
+ persistent_every(event.recipient_domain, ONE_HOUR) do
25
+ robot.send_message(target, result_to_message(result))
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def result_to_message(result)
33
+ "[mailgun] [#{result.domain}] #{result.dropped}/#{result.total} (#{result.dropped_rate.to_s("F")}%) recent emails dropped"
34
+ end
35
+
36
+ def repository
37
+ @repository ||= MailgunDroppedRateRepository.new(redis)
38
+ end
39
+
40
+ def target
41
+ Source.new(room: Lita::Room.find_by_name(config.channel_name) || "general")
42
+ end
43
+
44
+ def persistent_every(name, seconds, &block)
45
+ Lita::Timing::RateLimit.new(name, redis).once_every(seconds, &block)
46
+ end
47
+ end
48
+
49
+ Lita.register_handler(MailgunDroppedRate)
50
+ end
51
+ end
@@ -0,0 +1,59 @@
1
+ require "ostruct"
2
+ require "bigdecimal"
3
+
4
+ module Lita
5
+ class MailgunDroppedRateRepository
6
+ VALID_EVENTS = [:delivered, :dropped]
7
+
8
+ class DroppedResult
9
+ attr_reader :domain, :dropped, :total
10
+
11
+ def initialize(domain, dropped, total)
12
+ @domain, @dropped, @total = domain, dropped.to_i, total.to_i
13
+ end
14
+
15
+ def dropped_rate
16
+ (BigDecimal.new(dropped) / BigDecimal.new(total) * 100).round(3)
17
+ end
18
+ end
19
+
20
+ def initialize(redis)
21
+ @redis = redis
22
+ end
23
+
24
+ def record(domain, event_name)
25
+ return false unless valid_event?(event_name)
26
+
27
+ key = "events-#{domain}"
28
+ @redis.rpush(key, event_name.to_s)
29
+ @redis.ltrim(key, -20, -1)
30
+ true
31
+ end
32
+
33
+ def dropped_rate(domain)
34
+ DroppedResult.new( domain, dropped_count(domain), total_count(domain) )
35
+ end
36
+
37
+ private
38
+
39
+ def dropped_count(domain)
40
+ fetch_events(domain).select { |item|
41
+ item == "dropped".freeze
42
+ }.size
43
+ end
44
+
45
+ def total_count(domain)
46
+ fetch_events(domain).size
47
+ end
48
+
49
+ def fetch_events(domain)
50
+ key = "events-#{domain}"
51
+
52
+ list = @redis.lrange(key, 0, 19)
53
+ end
54
+
55
+ def valid_event?(event_name)
56
+ VALID_EVENTS.include?(event_name)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,22 @@
1
+ module Lita
2
+ class MailgunEvent
3
+ attr_reader :data
4
+
5
+ def initialize(data)
6
+ @data = data
7
+ end
8
+
9
+ def name
10
+ @data.fetch("event")
11
+ end
12
+
13
+ def recipient
14
+ @data.fetch("recipient")
15
+ end
16
+
17
+ def recipient_domain
18
+ recipient.split("@").last
19
+ end
20
+
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-mailgun
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Healy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
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: lita
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: lita-timing
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Lita handler for interacting with mailgun.com
98
+ email:
99
+ - james.healy@theconversation.edu.au
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files:
103
+ - README.md
104
+ - MIT-LICENSE
105
+ files:
106
+ - MIT-LICENSE
107
+ - README.md
108
+ - lib/lita-mailgun.rb
109
+ - lib/lita/mailgun.rb
110
+ - lib/lita/mailgun_dropped_rate.rb
111
+ - lib/lita/mailgun_dropped_rate_repository.rb
112
+ - lib/lita/mailgun_event.rb
113
+ homepage: http://github.com/conversation/lita-mailgun
114
+ licenses:
115
+ - MIT
116
+ metadata:
117
+ lita_plugin_type: handler
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 1.9.3
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.5.1
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Lita handler for interacting with mailgun.com
138
+ test_files: []