Module: Turntabler

Defined in:
lib/turntabler.rb,
lib/turntabler/resource.rb,
lib/turntabler/boot.rb,
lib/turntabler/room.rb,
lib/turntabler/snag.rb,
lib/turntabler/song.rb,
lib/turntabler/user.rb,
lib/turntabler/vote.rb,
lib/turntabler/error.rb,
lib/turntabler/event.rb,
lib/turntabler/avatar.rb,
lib/turntabler/client.rb,
lib/turntabler/handler.rb,
lib/turntabler/message.rb,
lib/turntabler/sticker.rb,
lib/turntabler/version.rb,
lib/turntabler/loggable.rb,
lib/turntabler/playlist.rb,
lib/turntabler/assertions.rb,
lib/turntabler/connection.rb,
lib/turntabler/preferences.rb,
lib/turntabler/digest_helpers.rb,
lib/turntabler/room_directory.rb,
lib/turntabler/authorized_user.rb,
lib/turntabler/sticker_placement.rb,
lib/turntabler/playlist_directory.rb

Overview

Turntable.FM API for Ruby

Defined Under Namespace

Modules: Assertions, DigestHelpers, Loggable, Version Classes: APIError, AuthorizedUser, Avatar, Boot, Client, Connection, ConnectionError, Error, Event, Handler, Message, Playlist, PlaylistDirectory, Preferences, Resource, Room, RoomDirectory, Snag, Song, Sticker, StickerPlacement, User, Vote

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerLogger

The logger to use for all Turntable messages. By default, everything is logged to STDOUT.

Returns:

  • (Logger)


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

def logger
  @logger
end

Class Method Details

.events(*names) ⇒ Object

Note:

Events must be defined before handlers can be registered for them

Defines one or more custom events for which handlers can be registered and triggered via Turntabler::Client#on and Turntabler::Client#trigger, respectively.

Examples:

Turntabler::Client.events :custom_event1, :custom_event2

Parameters:

  • names (Array<String>)

    The names of the events to define

Raises:

  • (ArgumentError)

    if the event has already been defined



111
112
113
114
115
116
117
118
119
# File 'lib/turntabler.rb', line 111

def events(*names)
  names.each do |name|
    if Event.command?(name)
      raise ArgumentError, "Event :#{name} is already defined"
    else
      Event.handle(name)
    end
  end
end

.interactiveObject

Note:

You must continue to run all commands on a client through Turntabler#run.

Whether this is going to be used in an interactive console such as IRB. If this is enabled then EventMachine will run in a separate thread. This will allow IRB to continue to actually be interactive.

Examples:

require 'turntabler'

Turntabler.interactive
Turntabler.run do
  @client = Turntabler::Client.new(...)
  @client.start
end

# ...later on after the connection has started and you want to interact with it
Turntabler.run do
  @client.user.load
  # ...
end


34
35
36
# File 'lib/turntabler.rb', line 34

def interactive
  Thread.new { EM.run }.abort_on_exception = true
end

.run(*args, &block) ⇒ Object

Note:

If you’re already running within an EventMachine reactor and a

Sets up the proper EventMachine reactor / Fiber to run commands against a client. If this is not in interactive mode, then the block won’t return until the EventMachine reactor is stopped.

Fiber, then there’s no need to call this method prior to interacting with a Turntabler::Client instance.

Exception handling

Any exceptions that occur within the block will be automatically caught and logged. This prevents the EventMachine reactor from dying.

Examples:

# Non-interactive, not in reactor / fiber
Turntabler.run do
  client = Turntabler::Client.new(...)
  client.room.become_dj
  # ...
end

# Interactive, not in reactor / fiber
Turntabler.interactive
Turntabler.run do
  @client = ...
end
Turntabler.run do
  @client.room.become_dj
  # ...
end

# Non-interactive, already in reactor / fiber
client = Turntabler::Client(...)
client.room.become_dj

DSL

# Takes the same arguments as Turntabler::Client
Turntabler.run(EMAIL, PASSWORD, :room => ROOM) do
  room.become_dj
  on :user_enter do
    # ...
  end
end


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/turntabler.rb', line 80

def run(*args, &block)
  if EM.reactor_running?
    EM.next_tick do
      EM.synchrony do
        begin
          if args.any?
            # Run the block within a client
            Client.new(*args, &block)
          else
            # Just run the block within a fiber
            block.call
          end
        rescue StandardError => ex
          logger.error(([ex.message] + ex.backtrace) * "\n")
        end
      end
    end
  else
    EM.synchrony { run(*args, &block) }
  end
end