Class: Kagu::Tracks

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kagu/tracks.rb

Constant Summary collapse

EXTENSIONS =
%w(.aac .flac .mp3 .wav)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(library) ⇒ Tracks

Returns a new instance of Tracks.

Raises:

  • (ArgumentError)


11
12
13
14
# File 'lib/kagu/tracks.rb', line 11

def initialize(library)
  raise ArgumentError.new("#{self.class}#library must be a library, #{library.inspect} given") unless library.is_a?(Library)
  @library = library
end

Instance Attribute Details

#libraryObject (readonly)

Returns the value of attribute library.



9
10
11
# File 'lib/kagu/tracks.rb', line 9

def library
  @library
end

Instance Method Details

#each(&block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kagu/tracks.rb', line 16

def each(&block)
  return unless block_given?
  File.open(library.path, 'r') do |file|
    while !file.eof? && (line = file.readline.strip)
      next unless line.starts_with?('<key>Track ID</key>')
      attributes = {}
      begin
        match = line.match(/<key>(.+)<\/key><(\w+)>(.*)<\/\2>/)
        next unless match
        name = "itunes_#{match[1].downcase.gsub(' ', '_')}"
        value = match[3]
        attributes[name] = value
      end while (line = file.readline.strip) != '</dict>'
      yield(Track.new(attributes)) if attributes['itunes_track_type'] == 'File' && attributes['itunes_podcast'].blank? && EXTENSIONS.include?(File.extname(attributes['itunes_location'].try(:downcase)))
    end
  end
end