postmark-inbound 0.1.0 → 0.2.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 +4 -4
- data/README.md +21 -13
- data/bin/pin-server +1 -0
- data/lib/postmark-inbound/config.rb +6 -2
- data/lib/postmark-inbound/handler.rb +32 -31
- data/lib/postmark-inbound/server.rb +4 -4
- data/lib/postmark-inbound/version.rb +1 -1
- data/lib/postmark-inbound.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68e263e910d1c898e9959f28756fb370f65f7dfb
|
4
|
+
data.tar.gz: a15cd5ea90284cb8c1c1689a5b1a4f4b5f413df0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 455d1a0606cf8504d01e2a91d4233630a8e0ce1a1f655b38a9747d3f4523a024ade1b841062be8e39b2dabd7861d94fcf0c27c628a182d5f59aff0db2128cbec
|
7
|
+
data.tar.gz: 3e4bf98990750698e31ed501cb6372485983fa144d4d5bac37cc582d1f6d947149476f75a49d3729f3820af1512e180613a0483b19ee5a9becf9acd364a5194b
|
data/README.md
CHANGED
@@ -36,27 +36,35 @@ PINS::Config.setup do |c|
|
|
36
36
|
'someSTRING1234',
|
37
37
|
'OTHERstring987'
|
38
38
|
]
|
39
|
+
# Paths of where you've stored the handlers
|
40
|
+
c.handler_paths = [
|
41
|
+
'handlers'
|
42
|
+
]
|
39
43
|
# HTTP server (Sinatra) settings
|
40
44
|
c.dump_errors = true
|
41
45
|
c.logging = true
|
42
46
|
end
|
47
|
+
```
|
48
|
+
|
49
|
+
#### Handlers
|
43
50
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
+
Handler blocks are called for every incoming requests. Create as many files you'd like containing handlers, for example: `handlers/examples.rb`
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
PINS::Handler.add do |pin|
|
55
|
+
# The 'pin' variable contains data from Postmark
|
56
|
+
break unless pin[:originalrecipient] == 'myinbox@pm.example.com'
|
57
|
+
puts "It's a match!"
|
58
|
+
# Do something more
|
51
59
|
end
|
52
60
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
end
|
61
|
+
# You can have multiple handlers in one file
|
62
|
+
PINS::Handler.add do |pin|
|
63
|
+
break unless pin[:spam_status]
|
64
|
+
puts "We've got a spam."
|
65
|
+
# Do something more
|
59
66
|
end
|
67
|
+
|
60
68
|
```
|
61
69
|
|
62
70
|
### Use
|
data/bin/pin-server
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
require 'postmark-inbound/handler'
|
2
|
-
|
3
1
|
module PINS
|
4
2
|
|
3
|
+
def self.config
|
4
|
+
Config.shared
|
5
|
+
end
|
6
|
+
|
5
7
|
class Config
|
6
8
|
|
7
9
|
# Load Ruby config file
|
@@ -27,11 +29,13 @@ module PINS
|
|
27
29
|
|
28
30
|
attr_accessor :user
|
29
31
|
attr_accessor :passwords
|
32
|
+
attr_accessor :handler_paths
|
30
33
|
attr_accessor :dump_errors
|
31
34
|
attr_accessor :logging
|
32
35
|
|
33
36
|
def initialize
|
34
37
|
@passwords = []
|
38
|
+
@handler_paths = []
|
35
39
|
@dump_errors = false
|
36
40
|
@logging = false
|
37
41
|
end
|
@@ -1,52 +1,53 @@
|
|
1
1
|
module PINS
|
2
2
|
|
3
|
+
def self.handlers
|
4
|
+
Handler.handlers
|
5
|
+
end
|
6
|
+
|
3
7
|
class Handler
|
4
8
|
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
PINS.logger.warn("Handler not added, duplicate name: #{name}")
|
11
|
-
return nil
|
12
|
-
end
|
13
|
-
handler = Handler.new(name)
|
14
|
-
yield handler if block_given?
|
15
|
-
@catalog[name] = handler
|
16
|
-
PINS.logger.info("Added handler: #{name}")
|
9
|
+
# Load handlers from directories designated in config
|
10
|
+
def self.autoload
|
11
|
+
PINS.config.handler_paths.each { |path|
|
12
|
+
load_from_path(path)
|
13
|
+
}
|
17
14
|
end
|
18
15
|
|
19
|
-
#
|
20
|
-
|
21
|
-
|
16
|
+
# Load handlers from a directory
|
17
|
+
# @param path [String] directory name
|
18
|
+
def self.load_from_path(path)
|
19
|
+
Dir.chdir(path) {
|
20
|
+
Dir.foreach('.') { |f| load f unless File.directory?(f) }
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
# Call as often as necessary to add handlers; each call creates a PINS::Handler object
|
25
|
+
def self.add(&block)
|
26
|
+
@handlers ||= []
|
27
|
+
@handlers << Handler.new(&block)
|
28
|
+
PINS.logger.debug("Added handler: #{@handlers.last}")
|
29
|
+
end
|
30
|
+
|
31
|
+
# @return [Array] containing all the handlers
|
32
|
+
def self.handlers
|
33
|
+
@handlers
|
22
34
|
end
|
23
35
|
|
24
36
|
# Run the handlers, typically called by the server
|
25
37
|
# @param pin [Hash] from Postmark inbound webhook JSON
|
26
|
-
def self.
|
38
|
+
def self.run_handlers(pin)
|
27
39
|
PINS.logger.info('Running handlers...')
|
28
|
-
|
29
|
-
(Handler.catalog)[name].run(pin)
|
30
|
-
end
|
40
|
+
@handlers.each { |h| h.run(pin) }
|
31
41
|
PINS.logger.info('Done running handlers.')
|
32
42
|
end
|
33
43
|
|
34
|
-
|
35
|
-
|
36
|
-
def initialize(name)
|
37
|
-
PINS.logger.debug("Initializing handler: #{name}")
|
38
|
-
@myname = name
|
39
|
-
end
|
40
|
-
|
41
|
-
# When adding a handler, call this to register a block
|
42
|
-
def set_block(&block)
|
43
|
-
PINS.logger.debug("Registering block for handler: #{myname}")
|
44
|
+
def initialize(&block)
|
44
45
|
@block = block
|
45
46
|
end
|
46
47
|
|
47
48
|
def run(obj)
|
48
|
-
PINS.logger.warn("No block to execute for handler: #{
|
49
|
-
PINS.logger.debug("Running handler: #{
|
49
|
+
PINS.logger.warn("No block to execute for handler: #{self}") unless @block
|
50
|
+
PINS.logger.debug("Running handler: #{self}")
|
50
51
|
@block.call(obj)
|
51
52
|
end
|
52
53
|
|
@@ -6,8 +6,8 @@ module PINS
|
|
6
6
|
# The Sinatra server
|
7
7
|
class Server < Sinatra::Base
|
8
8
|
|
9
|
-
use Rack::Auth::Basic, "Restricted Area" do |
|
10
|
-
|
9
|
+
use Rack::Auth::Basic, "Restricted Area" do |_username, password|
|
10
|
+
PINS.config.passwords.include?(password)
|
11
11
|
end
|
12
12
|
|
13
13
|
helpers Helper
|
@@ -15,7 +15,7 @@ module PINS
|
|
15
15
|
configure do
|
16
16
|
set :environment, :production
|
17
17
|
disable :static
|
18
|
-
c =
|
18
|
+
c = PINS.config
|
19
19
|
set :dump_errors, c.dump_errors
|
20
20
|
set :logging, c.logging
|
21
21
|
PINS.logger.info('Sinatra server configured.')
|
@@ -25,7 +25,7 @@ module PINS
|
|
25
25
|
PINS.logger.info('Incoming request received.')
|
26
26
|
PINS.logger.debug("Body size: #{request.content_length} bytes")
|
27
27
|
request.body.rewind
|
28
|
-
Handler.
|
28
|
+
Handler.run_handlers(parse_json(request.body.read))
|
29
29
|
body ''
|
30
30
|
end
|
31
31
|
|
data/lib/postmark-inbound.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: postmark-inbound
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ken J.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kajiki
|