Class: MovidaEvents::Poller

Inherits:
Object
  • Object
show all
Defined in:
lib/movida_events/poller.rb

Overview

Polls for events and calls a method for each one

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}) ⇒ Poller

Create a new MovidaEvents::Poller object

Parameters:

  • client (Client)

    The client for making API requests

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :newer_than (Integer)

    Requests events that occur only after the given ID

  • :event_type (String, Array<String>)

    Filter by only the given event types



20
21
22
23
24
# File 'lib/movida_events/poller.rb', line 20

def initialize(client, options = {})
  @client = client
  @options = default_options.merge(options)
  @stopped = false
end

Instance Attribute Details

#stoppedBoolean (readonly)

Indicates if the stop method has been called.

Returns:

  • (Boolean)

    True if the poller is stopped

See Also:



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

Yields:

  • Every time a poll request is made

Yield Parameters:

  • stats (Stats)

    The current stats



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.

Parameters:

  • times (Integer, nil) (defaults to: nil)

    If set, polling stops after times requests.

Yields:

  • Every new event

Yield Parameters:

  • event (Almodovar::Resource)

    The event object

  • stats (Stats)

    The current stats



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

#stopObject

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