Class: Turntabler::Event Private

Inherits:
Object
  • Object
show all
Defined in:
lib/turntabler/event.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides access to all of the events that get triggered by incoming messages from the Turntable API

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, command, args) ⇒ Event

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new event triggered with the given data



300
301
302
303
304
305
306
307
# File 'lib/turntabler/event.rb', line 300

def initialize(client, command, args)
  @client = client
  @args = args
  @data = args[0]
  @name = self.class.commands[command]
  @results = __send__("typecast_#{command}_event")
  @results = [[@results].compact] unless @results.is_a?(Array)
end

Class Attribute Details

.commandsHash<String, String> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Maps Turntable command => event name



14
15
16
# File 'lib/turntabler/event.rb', line 14

def commands
  @commands
end

Instance Attribute Details

#argsArray<Object> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The raw arguments list from the event



285
286
287
# File 'lib/turntabler/event.rb', line 285

def args
  @args
end

#dataHash<String, Object> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The raw hash of data parsed from the event



289
290
291
# File 'lib/turntabler/event.rb', line 289

def data
  @data
end

#nameString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The name of the event that was triggered



281
282
283
# File 'lib/turntabler/event.rb', line 281

def name
  @name
end

#resultsArray<Array<Object>> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The typecasted results args parsed from the event



293
294
295
# File 'lib/turntabler/event.rb', line 293

def results
  @results
end

Class Method Details

.command?(command) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines whether the given command is handled.



37
38
39
# File 'lib/turntabler/event.rb', line 37

def command?(command)
  commands.include?(command)
end

.handle(name, command = name) {|data| ... } ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines a new event that maps to the given Turntable command. The block defines how to typecast the data that is received from Turntable.

Yields:

  • (data)

    Gives the data to typecast to the block

Yield Parameters:

  • data (Hash)

    The data received from Turntable

Yield Returns:

  • The typecasted data that should be passed into any handlers bound to the event



25
26
27
28
29
30
31
# File 'lib/turntabler/event.rb', line 25

def handle(name, command = name, &block)
  block ||= lambda { [args] }
  commands[command] = name

  define_method("typecast_#{command}_event", &block)
  protected :"typecast_#{command}_event"
end