Class: Net::DAAP::Song

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/net/daap/song.rb

Overview

This class contains song information returned from the DAAP server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Song

Returns a new instance of Song.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/net/daap/song.rb', line 13

def initialize(args)
  @size           = args['daap.songsize']
  @album          = args[:album]
  @name           = args['dmap.itemname']
  #@artist         = args['daap.songartist']
  @artist         = args[:artist]
  @format         = args['daap.songformat']
  @persistentid   = args['dmap.persistentid']
  @id             = args['dmap.itemid']
  @daap           = args[:daap]
  @db             = args[:db]
  @path = [@artist.name, @album.name].collect { |name|
    name.gsub(File::SEPARATOR, '_') unless name.nil?
  }.join(File::SEPARATOR)
  @file = "#{@name.gsub(File::SEPARATOR, '_')}.#{@format}"
end

Instance Attribute Details

#albumObject (readonly)

Returns the value of attribute album.



8
9
10
# File 'lib/net/daap/song.rb', line 8

def album
  @album
end

#artistObject (readonly)

Returns the value of attribute artist.



8
9
10
# File 'lib/net/daap/song.rb', line 8

def artist
  @artist
end

#fileObject

Returns the value of attribute file.



9
10
11
# File 'lib/net/daap/song.rb', line 9

def file
  @file
end

#formatObject (readonly)

Returns the value of attribute format.



8
9
10
# File 'lib/net/daap/song.rb', line 8

def format
  @format
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/net/daap/song.rb', line 8

def id
  @id
end

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



8
9
10
# File 'lib/net/daap/song.rb', line 8

def name
  @name
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/net/daap/song.rb', line 9

def path
  @path
end

#persistentidObject (readonly)

Returns the value of attribute persistentid.



8
9
10
# File 'lib/net/daap/song.rb', line 8

def persistentid
  @persistentid
end

#sizeObject (readonly)

Returns the value of attribute size.



8
9
10
# File 'lib/net/daap/song.rb', line 8

def size
  @size
end

Instance Method Details

#<=>(other) ⇒ Object



48
49
50
# File 'lib/net/daap/song.rb', line 48

def <=>(other)
  name <=> other.name
end

#get(&block) ⇒ Object

Fetches the song data from the DAAP server and returns it.



31
32
33
34
# File 'lib/net/daap/song.rb', line 31

def get(&block)
  filename = "#{@id}.#{@format}"
  @daap.get_song("databases/#{@db.id}/items/#{filename}", &block)
end

#save(basedir = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/net/daap/song.rb', line 36

def save(basedir = nil)
  path = "#{basedir}#{File::SEPARATOR}#{@path}"
  FileUtils::mkdir_p(path)
  filename = "#{path}#{File::SEPARATOR}#{@file}"
  File.open(filename, "wb") { |file|
    get do |str|
      file.write str
    end
  }
  @daap.log.debug("Saved #{filename}") if @daap.log
end