class MovidaEvents::Poller

Polls for events and calls a method for each one

Attributes

stopped[R]

Indicates if the stop method has been called.

@see stop @return [Boolean] True if the poller is stopped

Public Class Methods

new(client, options = {}) click to toggle source

Create a new `MovidaEvents::Poller` object

@param client [Client] The client for making API requests @param options [Hash] Configuration options @option options [Integer] :newer_than Requests events that occur

only after the given ID

@option options [String,Array<String>] :event_type Filter by only the

given event types
# File lib/movida_events/poller.rb, line 20
def initialize(client, options = {})
  @client = client
  @options = default_options.merge(options)
  @stopped = false
end

Public Instance Methods

on_poll(&block) click to toggle source

Set a callback to run whenever an API request is made

Only one callback may be set

@yield Every time a poll request is made @yieldparam stats [Stats] The current stats

# File lib/movida_events/poller.rb, line 32
def on_poll(&block)
  @on_poll = block
end
poll(times = nil) { |event, clone| ... } click to toggle source

Poll for events

This method continues infinitely unless `times` is set.

@param times [Integer,nil] If set, polling stops after `times` requests. @yield Every new event @yieldparam event [Almodovar::Resource] The event object @yieldparam stats [Stats] The current stats

# 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() click to toggle source

Stop the poller

When this is called, the poller will finish processing the current request and any associated events before stopping.

# File lib/movida_events/poller.rb, line 62
def stop
  @stopped = true
end

Private Instance Methods

before_request(stats) click to toggle source

Before each request, updates stats and calls `on_poll`.

@param stats [Stats] The current stats

# File lib/movida_events/poller.rb, line 103
def before_request(stats)
  stats.start_request
  @on_poll&.call(stats.clone)
end
default_options() click to toggle source

Get the default poller options

@return [Hash] The default options

# File lib/movida_events/poller.rb, line 130
def default_options
  {
    newer_than: nil,
    interval: 30
  }
end
event_types() click to toggle source

Builds the event types parameter

Converts the `event_types` option into a string @return [String] The stringified events param

# File lib/movida_events/poller.rb, line 95
def event_types
  types = @options[:event_types]
  types.is_a?(Array) ? types.join(',') : types
end
initial_stats() click to toggle source

Sets up the initial stats state before polling

If `newer_than` is not set, gets the latest event ID

@return [Stats] The initial stats

# File lib/movida_events/poller.rb, line 113
def initial_stats
  Stats.new(@options[:newer_than] || latest_id)
end
latest_id() click to toggle source

Get the most recent event ID from the API

The default behavior of the events API if given no parameters is to return the last 50 events. So we select the last event from those.

@return [Integer] The most recent event ID

# File lib/movida_events/poller.rb, line 123
def latest_id
  @client.events.to_a.last.id
end
repeat(times) { || ... } click to toggle source

Repeat times or forever

@param times [Integer,nil] The number of times to repeat, or nil for

infinite

@yield `times` times or forever

# File lib/movida_events/poller.rb, line 73
def repeat(times)
  times ? times.times { yield } : loop { yield }
end
request_params(stats) click to toggle source

Builds the params for events requests

The last event from stats determines where to start for the next API request

@param stats [Stats] The current stats @return [Hash] The request params

# File lib/movida_events/poller.rb, line 84
def request_params(stats)
  {
    newer_than: stats.last,
    event_type: event_types
  }
end