Sha256: 3275345e8268d8e85ba66a9ae4fe68de904b60e26d2361f04e7145517190988e

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# encoding: utf-8

require 'rss'
require 'nokogiri'

module AirPlayer
  class Playlist < Array
    def initialize(options = {})
      @shuffle = options['shuffle'] || false
      @repeat  = options['repeat']  || false
    end

    def add(item)
      case type(item)
      when :local
        concat(media_in_local(item))
      when :podcast
        concat(media_in_podcast(item))
      when :url
        push(Media.new(item))
      end
      self
    end

    def entries(&blk)
      loop do
        shuffle! if @shuffle
        send(:each, &blk)
        break unless @repeat
      end
    end

    private
      def type(item)
        if Dir.exists?(File.expand_path(item))
          :local
        elsif Media.playable? item
          :url
        elsif RSS::Parser.parse(open(item))
          :podcast
        end
      end

      def media_in_local(path)
        Dir.entries(File.expand_path(path)).sort.map do |node|
          Media.new(File.expand_path(node, path)) if Media.playable? node
        end.compact
      end

      def media_in_podcast(path)
        Nokogiri::XML(open(path)).search('enclosure').map do |node|
          Media.new(node.attributes['url'].text)
        end
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
airplayer-0.0.6 lib/airplayer/playlist.rb