Class: Scrobbler::Playlist

Inherits:
Base
  • Object
show all
Extended by:
ImageClassFuncs
Includes:
ImageObjectFuncs
Defined in:
lib/scrobbler/playlist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ImageClassFuncs

maybe_image_node

Methods included from ImageObjectFuncs

#check_image_node, #image

Methods inherited from Base

api_key=, connection, get, maybe_streamable_attribute, maybe_streamable_node, post_request, request, sanitize, secret=

Constructor Details

#initialize(url, data = {}) ⇒ Playlist



68
69
70
71
# File 'lib/scrobbler/playlist.rb', line 68

def initialize(url,data={})
  @url = url
  fetch() unless data[:include_info] && data[:include_info] == false
end

Instance Attribute Details

#creatorObject (readonly)

Returns the value of attribute creator.



8
9
10
# File 'lib/scrobbler/playlist.rb', line 8

def creator
  @creator
end

#dateObject (readonly)

Returns the value of attribute date.



8
9
10
# File 'lib/scrobbler/playlist.rb', line 8

def date
  @date
end

#descriptionObject (readonly)

Returns the value of attribute description.



9
10
11
# File 'lib/scrobbler/playlist.rb', line 9

def description
  @description
end

#durationObject (readonly)

Returns the value of attribute duration.



9
10
11
# File 'lib/scrobbler/playlist.rb', line 9

def duration
  @duration
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/scrobbler/playlist.rb', line 8

def id
  @id
end

#sizeObject (readonly)

Returns the value of attribute size.



9
10
11
# File 'lib/scrobbler/playlist.rb', line 9

def size
  @size
end

#streamableObject (readonly)

Returns the value of attribute streamable.



9
10
11
# File 'lib/scrobbler/playlist.rb', line 9

def streamable
  @streamable
end

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/scrobbler/playlist.rb', line 8

def title
  @title
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/scrobbler/playlist.rb', line 8

def url
  @url
end

Class Method Details

.data_from_xml(xml, o = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/scrobbler/playlist.rb', line 33

def data_from_xml(xml, o = {})      
  o = {:include_track_info => true}.merge(o)
  data = {}
  xml.children.each do |child|
    data[:id] = child.content.to_i if child.name == 'id'
    data[:title] = child.content if child.name == 'title'

    maybe_image_node(data, child)
    data[:date] = Time.parse(child.content) if child.name == 'date'

    data[:size] = child.content.to_i if child.name == 'size'
    data[:description] = child.content if child.name == 'description'
    data[:duration] = child.content.to_i if child.name == 'duration'

    if child.name == 'streamable'
      if ['1', 'true'].include?(child.content)
        data[:streamable] = true
      else
        data[:streamable] = false
      end
    end
    
    if child.name == 'trackList'
      data[:tracks] = []
      child.children.each do |grandchild|
        data[:tracks] << Track.new_from_xml(grandchild, o) if grandchild.name == 'track'
      end
    end
    data[:creator] = child.content if child.name == 'creator'
    data[:url]     = child.content if child.name == 'url'
  end
  data
end

.new_from_album(album) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
# File 'lib/scrobbler/playlist.rb', line 20

def new_from_album(album)
  pp album.class
  raise ArgumentError, "Must create a playlist from an album object" if album.class != Scrobbler::Album
  Playlist.new(url_for_album(album.album_id))
end

.new_from_xml(xml) ⇒ Object



26
27
28
29
30
31
# File 'lib/scrobbler/playlist.rb', line 26

def new_from_xml(xml)
  data = data_from_xml(xml)
  puts "Creating Playlist: #{data[:title]}"
  return nil if data[:title].nil?
  Playlist.new(data[:url],data)
end

.url_for_album(album_id) ⇒ Object



12
13
14
# File 'lib/scrobbler/playlist.rb', line 12

def url_for_album(album_id)
  "lastfm://playlist/album/"+album_id.to_s
end

.url_for_playlist(playlist_id) ⇒ Object



16
17
18
# File 'lib/scrobbler/playlist.rb', line 16

def url_for_playlist(playlist_id)
  "lastfm://playlist/album/"+playlist_id.to_s
end

Instance Method Details

#fetchObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/scrobbler/playlist.rb', line 73

def fetch() 
  #Since playlists aren't static in the same sense the other objects are, dont cache results, always fetch the lastest version.
  xml = Base.request('playlist.fetch', {:playlistURL => @url});
  unless xml.root['status'] == 'failed'
    xml.root.children.each do |child|
      next unless child.name == 'playlist'
      data = self.class.data_from_xml(child, {:include_track_info => false, :include_album_info => false})
      populate_data(data)
      break
    end # xml.children.each do |child|
  end
end