Method: EasyUpnp::DeviceControlPoint#on_event

Defined in:
lib/easy_upnp/control_point/device_control_point.rb

#on_event(callback, &block) ⇒ Object

Raises:

  • (ArgumentError)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/easy_upnp/control_point/device_control_point.rb', line 161

def on_event(callback, &block)
  raise ArgumentError, 'Must provide block' if callback.nil?

  options = EventConfigOptions.new(&block)

  listener = EasyUpnp::HttpListener.new do |c|
    options.configure_http_listener.call(c)

    # It'd be kinda weird if a user set this (since a callback is taken as
    # an argument to the `on_event` method), but it'd be even weirder if
    # we ignored it.
    user_callback = c.callback
    c.callback do |r|
      user_callback.call(r) if user_callback
      callback.call(r)
    end
  end

  # exposing the URL as a lambda allows the subscription manager to get a
  # new URL should the server stop and start again on a different port.
  url = ->() { listener.listen }

  manager = EasyUpnp::SubscriptionManager.new(@events_client, url) do |c|
    options.configure_subscription_manager.call(c)

    user_shutdown = c.on_shutdown
    c.on_shutdown = ->() do
      user_shutdown.call if user_shutdown
      listener.shutdown
    end
  end

  manager.subscribe
  manager
end