Module: SpotifyWeb

Defined in:
lib/spotify_web.rb,
lib/spotify_web/resource.rb,
lib/spotify_web/user.rb,
lib/spotify_web/song.rb,
lib/spotify_web/event.rb,
lib/spotify_web/album.rb,
lib/spotify_web/error.rb,
lib/spotify_web/schema.rb,
lib/spotify_web/artist.rb,
lib/spotify_web/client.rb,
lib/spotify_web/handler.rb,
lib/spotify_web/version.rb,
lib/spotify_web/loggable.rb,
lib/spotify_web/playlist.rb,
lib/spotify_web/assertions.rb,
lib/spotify_web/connection.rb,
lib/spotify_web/restriction.rb,
lib/spotify_web/schema/core.pb.rb,
lib/spotify_web/schema/radio.pb.rb,
lib/spotify_web/authorized_user.rb,
lib/spotify_web/schema/mercury.pb.rb,
lib/spotify_web/schema/metadata.pb.rb,
lib/spotify_web/schema/playlist4.pb.rb,
lib/spotify_web/schema/bartender.pb.rb,
lib/spotify_web/resource_collection.rb,
lib/spotify_web/schema/socialgraph.pb.rb

Overview

Spotify Web API for Ruby

Defined Under Namespace

Modules: Assertions, Loggable, Schema, Version Classes: APIError, Album, Artist, AuthorizedUser, Client, Connection, ConnectionError, Error, Event, Handler, Playlist, Resource, ResourceCollection, Restriction, Song, User

Constant Summary collapse

USER_AGENT =

The user agent for Spotify access

'node-spotify-web (Chrome/13.37 compatible-ish)'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerLogger

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

Returns:

  • (Logger)


17
18
19
# File 'lib/spotify_web.rb', line 17

def logger
  @logger
end

Class Method Details

.interactiveObject

Note:

You must continue to run all commands on a client through SpotifyWeb#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 'spotify_web'

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

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


38
39
40
# File 'lib/spotify_web.rb', line 38

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 SpotifyWeb::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
SpotifyWeb.run do
  client = SpotifyWeb::Client.new(...)
  client.playlists
  # ...
end

# Interactive, not in reactor / fiber
SpotifyWeb.interactive
SpotifyWeb.run do
  @client = ...
end
SpotifyWeb.run do
  @client.playlists
  # ...
end

# Non-interactive, already in reactor / fiber
client = SpotifyWeb::Client(...)
client.playlists

DSL

# Takes the same arguments as SpotifyWeb::Client
SpotifyWeb.run(USERNAME, PASSWORD) do
  user.playlists
end


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

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