class Anyplayer::Selector

Attributes

errors[R]
verbose[RW]

Public Class Methods

new() click to toggle source
# File lib/anyplayer/selector.rb, line 18
def initialize
  @verbose = false
  @errors = []
end

Public Instance Methods

player() click to toggle source

Returns an instance of the first music player that's launched

# File lib/anyplayer/selector.rb, line 24
def player
  PLAYERS.each do |player|
    player_load(player) || next

    instance = player_class(player).new
    player_on_platform?(instance) || next

    return instance if player_launched?(instance)
  end
  nil
end

Private Instance Methods

log(text) click to toggle source
# File lib/anyplayer/selector.rb, line 95
def log(text)
  $stderr.puts text if verbose
end
platform() click to toggle source
# File lib/anyplayer/selector.rb, line 38
def platform
  @platform ||= begin
    host_os = RbConfig::CONFIG["host_os"]
    case host_os
    when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
      :windows
    when /darwin|mac os/
      :mac
    when /linux/
      :linux
    when /solaris|bsd/
      :unix
    else
      @errors << "Unknown operating system #{host_os.inspect}"
      nil
    end
  end
end
player_class(player) click to toggle source
# File lib/anyplayer/selector.rb, line 76
def player_class(player)
  camelized = player.to_s.split(/_/).map(&:capitalize).join
  Anyplayer.const_get(camelized)
end
player_launched?(player) click to toggle source
# File lib/anyplayer/selector.rb, line 81
def player_launched?(player)
  seconds = 5
  begin
    Timeout.timeout(seconds) do
      launched = player.launched?
      log launched ? "  launched" : "  not launched"
      launched
    end
  rescue Timeout::Error
    log "  timed out after #{seconds} seconds"
    false
  end
end
player_load(player) click to toggle source
# File lib/anyplayer/selector.rb, line 57
def player_load(player)
  log "#{player}:"
  begin
    require "anyplayer/players/#{player}"
    log "  loaded"
    true
  rescue LoadError => e
    log "  #{e.message}"
    @errors << "Error loading #{player}: #{e.message}"
    false
  end
end
player_on_platform?(player) click to toggle source
# File lib/anyplayer/selector.rb, line 70
def player_on_platform?(player)
  result = player.platforms.include?(platform)
  log "  not on platform" unless result
  result
end