Class: Scrobbler::Track

Inherits:
Base
  • Object
show all
Extended by:
ImageClassFuncs
Includes:
ImageObjectFuncs
Defined in:
lib/scrobbler/track.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(input, data = {}) ⇒ Track

Returns a new instance of Track.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/scrobbler/track.rb', line 94

def initialize(input, data={})
  super()
  #check for old style parameter arguments
  if data.class == String 
    data = {:name => data}
  end
  
  if input.class == String && data[:mbid] && data[:mbid] == true
    raise ArgumentError, "MBID is required for an MBID query" if input.blank?
    @mbid = input
    load_album_info() unless !data[:include_info].nil? && data[:include_info] == false 
  else
    raise ArgumentError, "Artist is required" if input.blank?
    raise ArgumentError, "Name is required" if data[:name].blank?
    @artist = Artist.new(input)
    @name = data[:name]
    load_info() if data[:include_info]
  end
end

Instance Attribute Details

#albumObject

Returns the value of attribute album.



43
44
45
# File 'lib/scrobbler/track.rb', line 43

def album
  @album
end

#artistObject

Returns the value of attribute artist.



42
43
44
# File 'lib/scrobbler/track.rb', line 42

def artist
  @artist
end

#countObject

Returns the value of attribute count.



42
43
44
# File 'lib/scrobbler/track.rb', line 42

def count
  @count
end

#dateObject

Returns the value of attribute date.



43
44
45
# File 'lib/scrobbler/track.rb', line 43

def date
  @date
end

#durationObject

Returns the value of attribute duration.



44
45
46
# File 'lib/scrobbler/track.rb', line 44

def duration
  @duration
end

#idObject

Returns the value of attribute id.



42
43
44
# File 'lib/scrobbler/track.rb', line 42

def id
  @id
end

#listenersObject

Returns the value of attribute listeners.



44
45
46
# File 'lib/scrobbler/track.rb', line 44

def listeners
  @listeners
end

#mbidObject

Returns the value of attribute mbid.



42
43
44
# File 'lib/scrobbler/track.rb', line 42

def mbid
  @mbid
end

#nameObject

Returns the value of attribute name.



42
43
44
# File 'lib/scrobbler/track.rb', line 42

def name
  @name
end

#now_playingObject

Returns the value of attribute now_playing.



43
44
45
# File 'lib/scrobbler/track.rb', line 43

def now_playing
  @now_playing
end

#playcountObject

Returns the value of attribute playcount.



42
43
44
# File 'lib/scrobbler/track.rb', line 42

def playcount
  @playcount
end

#rankObject

Returns the value of attribute rank.



42
43
44
# File 'lib/scrobbler/track.rb', line 42

def rank
  @rank
end

#streamableObject

Returns the value of attribute streamable.



43
44
45
# File 'lib/scrobbler/track.rb', line 43

def streamable
  @streamable
end

#tagcountObject

Returns the value of attribute tagcount.



43
44
45
# File 'lib/scrobbler/track.rb', line 43

def tagcount
  @tagcount
end

#urlObject

Returns the value of attribute url.



42
43
44
# File 'lib/scrobbler/track.rb', line 42

def url
  @url
end

Class Method Details

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/scrobbler/track.rb', line 60

def data_from_xml(xml, o = {})
  o = {:include_album_info => true, :include_artist_info => true}.merge(o) 
  data = {}
  xml.children.each do |child|
    data[:name] = child.content if child.name == 'name' || child.name == 'title'
    data[:mbid] = child.content.to_i if child.name == 'mbid'
    data[:id] = child.content.to_i if child.name == 'id'
    data[:duration] = child.content.to_i if child.name == 'duration'
    data[:url] = child.content if child.name == 'url' || child.name == 'identifier'
    data[:date] = Time.parse(child.content) if child.name == 'date'
    data[:listeners] = child.content.to_i if child.name == 'listeners'
    data[:artist] = Artist.new_from_xml(child, o) if (child.name == 'artist' || child.name == 'creator') && o[:include_artist_info]
    data[:album] = Album.new_from_xml(child, o) if child.name == 'album' && o[:include_album_info]
    data[:playcount] = child.content.to_i if child.name == 'playcount'
    data[:tagcount] = child.content.to_i if child.name == 'tagcount'
    maybe_image_node(data, child)
    if child.name == 'streamable'
      if ['1', 'true'].include?(child.content)
        data[:streamable] = true
      else
        data[:streamable] = false
      end
    end
  end
  
  
  data[:rank] = xml['rank'].to_i if xml['rank']
  data[:now_playing] = true if xml['nowplaying'] && xml['nowplaying'] == 'true'
  
  data[:now_playing] = false if data[:now_playing].nil? 
  o.merge(data)
end

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



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/scrobbler/track.rb', line 47

def new_from_xml(xml, o = {})
  data = self.data_from_xml(xml, o)
  return nil if data[:name].nil?
  if data[:artist].blank? 
    if data[:creator].blank? 
      data[:artist] = data[:creator];
    else 
      raise Error, "Supplied XML to track has no artist or creator"
    end
  end
  Track.new(data[:artist], data)
end

Instance Method Details

#==(otherTrack) ⇒ Object



145
146
147
148
149
150
# File 'lib/scrobbler/track.rb', line 145

def ==(otherTrack)
  if otherTrack.is_a?(Scrobbler::Track)
    return ((@name == otherTrack.name) && (@artist == otherTrack.artist))
  end
  false
end

#add_tags(tags) ⇒ Object

Raises:

  • (NotImplementedError)


114
115
116
117
# File 'lib/scrobbler/track.rb', line 114

def add_tags(tags)
  # This function requires authentication, but SimpleAuth is not yet 2.0
  raise NotImplementedError
end

#banObject

Raises:

  • (NotImplementedError)


119
120
121
122
# File 'lib/scrobbler/track.rb', line 119

def ban
  # This function requires authentication, but SimpleAuth is not yet 2.0
  raise NotImplementedError
end

#load_infoObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/scrobbler/track.rb', line 125

def load_info
  return nil if @info_loaded
  doc = Base.request('track.getinfo', :artist => @artist.name, :track => @name)
  doc.root.children.each do |child|
    next unless child.name == 'track'
    data = self.class.data_from_xml(child)
    populate_data(data)
    @info_loaded = true
    break
  end
end

#top_fans(force = false) ⇒ Object



137
138
139
# File 'lib/scrobbler/track.rb', line 137

def top_fans(force=false)
  get_response('track.gettopfans', :fans, 'topfans', 'user', {:artist=>@artist.name, :track=>@name}, force)
end

#top_tags(force = false) ⇒ Object



141
142
143
# File 'lib/scrobbler/track.rb', line 141

def top_tags(force=false)
  get_response('track.gettoptags', :top_tags, 'toptags', 'tag', {:artist=>@artist.name.to_s, :track=>@name}, force)
end