Class: MovidaEvents::Poller
- Inherits:
-
Object
- Object
- MovidaEvents::Poller
- Defined in:
- lib/movida_events/poller.rb
Overview
Polls for events and calls a method for each one
Instance Attribute Summary collapse
-
#stopped ⇒ Boolean
readonly
Indicates if the stop method has been called.
Instance Method Summary collapse
-
#initialize(client, options = {}) ⇒ Poller
constructor
Create a new
MovidaEvents::Poller
object. -
#on_poll {|stats| ... } ⇒ Object
Set a callback to run whenever an API request is made.
-
#poll(times = nil) {|event, stats| ... } ⇒ Object
Poll for events.
-
#stop ⇒ Object
Stop the poller.
Constructor Details
#initialize(client, options = {}) ⇒ Poller
Create a new MovidaEvents::Poller
object
20 21 22 23 24 |
# File 'lib/movida_events/poller.rb', line 20 def initialize(client, = {}) @client = client @options = .merge() @stopped = false end |
Instance Attribute Details
#stopped ⇒ Boolean (readonly)
Indicates if the stop method has been called.
10 11 12 |
# File 'lib/movida_events/poller.rb', line 10 def stopped @stopped end |
Instance Method Details
#on_poll {|stats| ... } ⇒ Object
Set a callback to run whenever an API request is made
Only one callback may be set
32 33 34 |
# File 'lib/movida_events/poller.rb', line 32 def on_poll(&block) @on_poll = block end |
#poll(times = nil) {|event, stats| ... } ⇒ Object
Poll for events
This method continues infinitely unless times
is set.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/movida_events/poller.rb', line 44 def poll(times = nil) stats = initial_stats repeat(times) do before_request(stats) @client.events(request_params(stats)) do |event| stats.receive_event(event) yield event, stats.clone if block_given? end break if @stopped sleep @options[:interval] if stats.request_events.zero? end end |
#stop ⇒ Object
Stop the poller
When this is called, the poller will finish processing the current request and any associated events before stopping.
62 63 64 |
# File 'lib/movida_events/poller.rb', line 62 def stop @stopped = true end |