postmark-inbound 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 034aa428798a3824eddbccfcc91f128d32d09983
4
- data.tar.gz: 0340b574dc1ef7d314b86417e9c9a6c9f5b9d3eb
3
+ metadata.gz: 68e263e910d1c898e9959f28756fb370f65f7dfb
4
+ data.tar.gz: a15cd5ea90284cb8c1c1689a5b1a4f4b5f413df0
5
5
  SHA512:
6
- metadata.gz: eb30b5de2aa8042ee269c0dd88caf501132710e63a7cbfb7e0b25adad0fdd4c33016ca67704713b85656a0980575127041899872a2497f5650added9845c1451
7
- data.tar.gz: e7be9a60f997880660ad087ca3bb360f7c76f2b0cede8fc4acd956ac39ea78002dd24f9385e48bc38cbc6eed7e55f8eacd1c0be60b629e6c56142b4340764789
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
- # Add and register handler blocks; all handlers will be called for every incoming request
45
- PINS::Handler.add(:my_handler1) do |h|
46
- h.set_block do |pin|
47
- break unless pin[:originalrecipient] == 'myinbox@pm.example.com'
48
- puts "It's a match!"
49
- # Do something more
50
- end
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
- PINS::Handler.add(:my_handler2) do |h|
54
- h.set_block do |pin|
55
- break unless pin[:spam_status]
56
- puts "We've got a spam."
57
- # Do something more
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
@@ -9,6 +9,7 @@ Kajiki.run(opts) do |cmd|
9
9
  case cmd
10
10
  when 'start'
11
11
  PINS::Config.load_config(opts[:config]) if opts[:config]
12
+ PINS::Handler.autoload
12
13
  require 'postmark-inbound/server'
13
14
  PINS.logger.warn('Postmark Inbound Server starting...')
14
15
  Rack::Server.start({
@@ -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
- # Call as often as necessary to add handlers, usually from the config file; each call creates a PINS::Handler object
6
- # @param name [Symbol] or a String
7
- def self.add(name)
8
- @catalog ||= {}
9
- if @catalog[name]
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
- # @return [Hash] containing all the handlers
20
- def self.catalog
21
- return @catalog
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.run(pin, names = Handler.catalog.keys)
38
+ def self.run_handlers(pin)
27
39
  PINS.logger.info('Running handlers...')
28
- names.each do |name|
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
- attr_reader :myname
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: #{myname}") unless @block
49
- PINS.logger.debug("Running handler: #{myname}")
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 |username, password|
10
- Config.shared.passwords.include?(password)
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 = Config.shared
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.run(parse_json(request.body.read))
28
+ Handler.run_handlers(parse_json(request.body.read))
29
29
  body ''
30
30
  end
31
31
 
@@ -1,5 +1,5 @@
1
1
  module PINS
2
2
 
3
- Version = '0.1.0'
3
+ Version = '0.2.0'
4
4
 
5
5
  end
@@ -1,2 +1,3 @@
1
1
  require 'postmark-inbound/logger'
2
2
  require 'postmark-inbound/config'
3
+ require 'postmark-inbound/handler'
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.1.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-22 00:00:00.000000000 Z
11
+ date: 2017-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kajiki