Class: Asana::Resources::Events

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/asana/resource_includes/events.rb

Overview

An infinite collection of events.

Since they are infinite, if you want to filter or do other collection operations without blocking indefinitely you should call #lazy on them to turn them into a lazy collection.

Examples:

# Subscribes to an event stream and blocks indefinitely, printing
# information of every event as it comes in.
events = Events.new(resource: 'someresourceID', client: client)
events.each do |event|
  puts [event.type, event.action]
end

# Lazily filters events as they come in and prints them.
events = Events.new(resource: 'someresourceID', client: client)
events.lazy.select { |e| e.type == 'task' }.each do |event|
  puts [event.type, event.action]
end

Instance Method Summary collapse

Constructor Details

#initialize(resource: required('resource'), client: required('client'), wait: 1, options: {}) ⇒ Events

Initializes a new Events instance, subscribed to a resource ID.

Parameters:

  • resource (String) (defaults to: required('resource'))

    a resource ID. Can be a task id or a workspace id.

  • client (Asana::Client) (defaults to: required('client'))

    a client to perform the requests.

  • wait (Integer) (defaults to: 1)

    the number of seconds to wait between each poll.

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

    the request I/O options



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/asana/resource_includes/events.rb', line 37

def initialize(resource: required('resource'),
               client: required('client'),
               wait: 1, options: {})
  @resource  = resource
  @client    = client
  @events    = []
  @wait      = wait
  @options   = options
  @sync      = nil
  @last_poll = nil
end

Instance Method Details

#each(&block) ⇒ Object

Iterates indefinitely over all events happening to a particular resource from the @sync timestamp or from now if it is nil.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/asana/resource_includes/events.rb', line 51

def each(&block)
  if block
    loop do
      poll if @events.empty?
      event = @events.shift
      yield event if event
    end
  else
    to_enum
  end
end