Sha256: 3b469c19cedf33a70a2c8edbc0efd7a5b518f9e25b5d33e0fbc54439394c5b39

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# encoding: utf-8

require 'rss'

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_dir
        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.exist?(File.expand_path(item))
        :local_dir
      elsif Media.playable?(item)
        :url
      elsif item.match(/.+(xml|rss)$/)
        :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)
      strict_parse = false
      RSS::Parser.parse(path, strict_parse).items.map do |node|
        Media.new(node.link)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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