Class: PINS::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/postmark-inbound/handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Handler

Returns a new instance of Handler.



44
45
46
# File 'lib/postmark-inbound/handler.rb', line 44

def initialize(&block)
  @block = block
end

Class Method Details

.add(&block) ⇒ Object

Call as often as necessary to add handlers; each call creates a PINS::Handler object



25
26
27
28
29
# File 'lib/postmark-inbound/handler.rb', line 25

def self.add(&block)
  @handlers ||= []
  @handlers << Handler.new(&block)
  PINS.logger.debug("Added handler: #{@handlers.last}")
end

.autoloadObject

Load handlers from directories designated in config



10
11
12
13
14
# File 'lib/postmark-inbound/handler.rb', line 10

def self.autoload
  PINS.config.handler_paths.each { |path|
    load_from_path(path)
  }
end

.handlersArray

Returns containing all the handlers.

Returns:

  • (Array)

    containing all the handlers



32
33
34
# File 'lib/postmark-inbound/handler.rb', line 32

def self.handlers
  @handlers
end

.load_from_path(path) ⇒ Object

Load handlers from a directory

Parameters:

  • path (String)

    directory name



18
19
20
21
22
# File 'lib/postmark-inbound/handler.rb', line 18

def self.load_from_path(path)
  Dir.chdir(path) {
    Dir.foreach('.') { |f| load f unless File.directory?(f) }
  }
end

.run_handlers(pin) ⇒ Object

Run the handlers, typically called by the server

Parameters:

  • pin (Hash)

    from Postmark inbound webhook JSON



38
39
40
41
42
# File 'lib/postmark-inbound/handler.rb', line 38

def self.run_handlers(pin)
  PINS.logger.info('Running handlers...')
  @handlers.each { |h| h.run(pin) }
  PINS.logger.info('Done running handlers.')
end

Instance Method Details

#run(obj) ⇒ Object



48
49
50
51
52
# File 'lib/postmark-inbound/handler.rb', line 48

def run(obj)
  PINS.logger.warn("No block to execute for handler: #{self}") unless @block
  PINS.logger.debug("Running handler: #{self}")
  @block.call(obj)
end