Class: Pling::Gateway
- Inherits:
-
Object
- Object
- Pling::Gateway
- Includes:
- Configurable
- Defined in:
- lib/pling/gateway.rb
Overview
This is the base class of all gateways. It defines the public interface of all gateways and provides helper methods to configure gateways and define the device types a gateway is able to handle.
Gateway implementations must set the types they’re able to handle by calling the Gateway.handles macro.
Every gateway must implement a #deliver! method which does the actual delivering.
Direct Known Subclasses
Class Method Summary collapse
-
.discover(device) ⇒ Pling::Gateway
Finds a gateway that handles the given device.
-
.handled_types ⇒ Array<#to_sym>
Returns a list of device types that this gateway is able to handle.
-
.handles(*types) ⇒ Array<#to_sym>
Defines the device types a gateway is able to handle.
Instance Method Summary collapse
-
#deliver(message, device, stack = nil) ⇒ Object
Delivers the given message to the given device using the given stack.
-
#deliver!(message, device) ⇒ Object
Delivers the given message to the given device without using the middleware.
-
#handles?(device) ⇒ Boolean
Checks if this gateway is able to handle the given device.
-
#initialize(config = {}) ⇒ Gateway
constructor
Initializes a new Gateway instance.
-
#setup! ⇒ Object
Sets up this gateway.
-
#teardown! ⇒ Object
Tears down this gateway.
Constructor Details
#initialize(config = {}) ⇒ Gateway
Initializes a new Gateway instance.
64 65 66 67 68 69 |
# File 'lib/pling/gateway.rb', line 64 def initialize(config = {}) setup_configuration(config) middlewares = configuration[:middlewares] configuration.merge!(:middlewares => Pling::DelayedInitializer.new) middlewares.each { |middleware| configuration[:middlewares] << middleware } if middlewares end |
Class Method Details
.discover(device) ⇒ Pling::Gateway
Finds a gateway that handles the given device
32 33 34 35 36 37 38 |
# File 'lib/pling/gateway.rb', line 32 def discover(device) device = Pling._convert(device, :device) Pling.gateways.initialize! Pling.gateways.detect do |gateway| gateway.handles?(device) end or raise(Pling::NoGatewayFound, "Could not find a gateway for #{device.class} with type :#{device.type}") end |
.handled_types ⇒ Array<#to_sym>
Returns a list of device types that this gateway is able to handle
53 54 55 |
# File 'lib/pling/gateway.rb', line 53 def handled_types @handled_types ||= [] end |
.handles(*types) ⇒ Array<#to_sym>
Defines the device types a gateway is able to handle
45 46 47 |
# File 'lib/pling/gateway.rb', line 45 def handles(*types) @handled_types = [types].flatten.map { |t| t.to_sym } end |
Instance Method Details
#deliver(message, device, stack = nil) ⇒ Object
Delivers the given message to the given device using the given stack. If the :on_exception callback is configured it’ll rescue all Pling::Errors and pass them to the given callback.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/pling/gateway.rb', line 99 def deliver(, device, stack = nil) = Pling._convert(, :message) device = Pling._convert(device, :device) stack ||= [] + configuration[:middlewares].initialize! return deliver!(, device) if stack.empty? stack.shift.deliver(, device) do |m, d| deliver(m, d, stack) end rescue Pling::Error => error callback = configuration[:on_exception] callback && callback.respond_to?(:call) ? callback.call(error) : raise end |
#deliver!(message, device) ⇒ Object
Delivers the given message to the given device without using the middleware.
121 122 123 |
# File 'lib/pling/gateway.rb', line 121 def deliver!(, device) raise NotImplementedError, "Please implement #{self.class}#deliver!(message, device)" end |
#handles?(device) ⇒ Boolean
Checks if this gateway is able to handle the given device
85 86 87 88 |
# File 'lib/pling/gateway.rb', line 85 def handles?(device) device = Pling._convert(device, :device) self.class.handled_types.include?(device.type) end |
#setup! ⇒ Object
Sets up this gateway
73 74 |
# File 'lib/pling/gateway.rb', line 73 def setup! end |
#teardown! ⇒ Object
Tears down this gateway
78 79 |
# File 'lib/pling/gateway.rb', line 78 def teardown! end |