Class: Scrobbler::User

Inherits:
Base
  • Object
show all
Defined in:
lib/scrobbler/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_key=, connection, fetch_and_parse, sanitize

Constructor Details

#initialize(username, o = {}) ⇒ User

Returns a new instance of User.

Raises:

  • (ArgumentError)


80
81
82
83
84
85
# File 'lib/scrobbler/user.rb', line 80

def initialize(username, o={})
  options = {:include_profile => false}.merge(o)
  raise ArgumentError if username.blank?
  @username = username
  load_profile() if options[:include_profile]
end

Instance Attribute Details

#ageObject

Returns the value of attribute age.



54
55
56
# File 'lib/scrobbler/user.rb', line 54

def age
  @age
end

#avatarObject

Returns the value of attribute avatar.



54
55
56
# File 'lib/scrobbler/user.rb', line 54

def avatar
  @avatar
end

#clusterObject

profile attributes



53
54
55
# File 'lib/scrobbler/user.rb', line 53

def cluster
  @cluster
end

#countryObject

Returns the value of attribute country.



54
55
56
# File 'lib/scrobbler/user.rb', line 54

def country
  @country
end

#genderObject

Returns the value of attribute gender.



54
55
56
# File 'lib/scrobbler/user.rb', line 54

def gender
  @gender
end

#idObject

profile attributes



53
54
55
# File 'lib/scrobbler/user.rb', line 53

def id
  @id
end

#matchObject

neighbor attributes



57
58
59
# File 'lib/scrobbler/user.rb', line 57

def match
  @match
end

#mbox_sha1sumObject

profile attributes



53
54
55
# File 'lib/scrobbler/user.rb', line 53

def mbox_sha1sum
  @mbox_sha1sum
end

#playcountObject

Returns the value of attribute playcount.



54
55
56
# File 'lib/scrobbler/user.rb', line 54

def playcount
  @playcount
end

#realnameObject

profile attributes



53
54
55
# File 'lib/scrobbler/user.rb', line 53

def realname
  @realname
end

#registeredObject

profile attributes



53
54
55
# File 'lib/scrobbler/user.rb', line 53

def registered
  @registered
end

#registered_unixtimeObject

Returns the value of attribute registered_unixtime.



54
55
56
# File 'lib/scrobbler/user.rb', line 54

def registered_unixtime
  @registered_unixtime
end

#urlObject

profile attributes



53
54
55
# File 'lib/scrobbler/user.rb', line 53

def url
  @url
end

#usernameObject (readonly)

attributes needed to initialize



50
51
52
# File 'lib/scrobbler/user.rb', line 50

def username
  @username
end

#weightObject

track fans attributes



60
61
62
# File 'lib/scrobbler/user.rb', line 60

def weight
  @weight
end

Class Method Details

.find(*args) ⇒ Object



72
73
74
75
76
77
# File 'lib/scrobbler/user.rb', line 72

def find(*args)
  options = {:include_profile => false}
  options.merge!(args.pop) if args.last.is_a?(Hash)
  users = args.flatten.inject([]) { |users, u| users << User.new(u, options); users }
  users.length == 1 ? users.pop : users
end

.new_from_xml(xml, doc = nil) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/scrobbler/user.rb', line 63

def new_from_xml(xml, doc=nil)
  u        = User.new(xml.at(:name).inner_html)
  u.url    = (xml).at(:url).inner_html    if (xml).at(:url)
  u.avatar = (xml).at(:image).inner_html  if (xml).at(:image)
  u.weight = (xml).at(:weight).inner_html if (xml).at(:weight)
  u.match  = (xml).at(:match).inner_html  if (xml).at(:match)
  u
end

Instance Method Details

#api_pathObject



87
88
89
# File 'lib/scrobbler/user.rb', line 87

def api_path
  "/#{API_VERSION}/user/#{CGI::escape(username)}"
end

#charts(force = false) ⇒ Object



168
169
170
# File 'lib/scrobbler/user.rb', line 168

def charts(force=false)
  get_instance2('user.getweeklychartlist', :charts, :chart, {'user'=>@username}, force)
end

#current_events(format = :ics) ⇒ Object

Raises:

  • (ArgumentError)


91
92
93
94
95
# File 'lib/scrobbler/user.rb', line 91

def current_events(format=:ics)
  format = :ics if format.to_s == 'ical'
  raise ArgumentError unless ['ics', 'rss'].include?(format.to_s)
  "#{API_URL.chop}#{api_path}/events.#{format}"
end

#friends(force = false, page = 1, limit = 50) ⇒ Object



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

def friends(force=false, page=1, limit=50)
  get_instance2('user.getfriends', :friends, :user, {'user'=>@username, 'page'=>page.to_s, 'limit'=>limit.to_s}, force)
end

#friends_events(format = :ics) ⇒ Object

Raises:

  • (ArgumentError)


97
98
99
100
101
# File 'lib/scrobbler/user.rb', line 97

def friends_events(format=:ics)
  format = :ics if format.to_s == 'ical'
  raise ArgumentError unless ['ics', 'rss'].include?(format.to_s)
  "#{API_URL.chop}#{api_path}/friendevents.#{format}"
end

#load_profileObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/scrobbler/user.rb', line 109

def load_profile
  doc                  = self.class.fetch_and_parse("#{api_path}/profile.xml")
  @id                  = (doc).at(:profile)['id']
  @cluster             = (doc).at(:profile)['cluster']
  @url                 = (doc).at(:url).inner_html
  @realname            = (doc).at(:realname).inner_html
  @mbox_sha1sum        = (doc).at(:mbox_sha1sum).inner_html
  @registered          = (doc).at(:registered).inner_html
  @registered_unixtime = (doc).at(:registered)['unixtime']
  @age                 = (doc).at(:age).inner_html
  @gender              = (doc).at(:gender).inner_html
  @country             = (doc).at(:country).inner_html
  @playcount           = (doc).at(:playcount).inner_html
  @avatar              = (doc).at(:avatar).inner_html
end

#neighbours(force = false) ⇒ Object



145
146
147
# File 'lib/scrobbler/user.rb', line 145

def neighbours(force=false)
  get_instance2('user.getneighbours', :neighbours, :user, {'user'=>@username}, force)
end

#recent_banned_tracks(force = false) ⇒ Object



153
154
155
156
# File 'lib/scrobbler/user.rb', line 153

def recent_banned_tracks(force=false)
  #warn "#{file}:#{lineno}:Warning: Scrobbler::User#recent_banned_tracks is deprecated (not supported by the Last.fm 2.0 API)"
  get_instance(:recentbannedtracks, :recent_banned_tracks, :track, force)
end

#recent_loved_tracks(force = false) ⇒ Object



158
159
160
161
# File 'lib/scrobbler/user.rb', line 158

def recent_loved_tracks(force=false)
  #warn "#{file}:#{lineno}:Warning: Scrobbler::User#recent_loved_tracks is deprecated (not supported by the Last.fm 2.0 API)"
  get_instance(:recentlovedtracks, :recent_loved_tracks, :track, force)
end

#recent_tracks(force = false) ⇒ Object



149
150
151
# File 'lib/scrobbler/user.rb', line 149

def recent_tracks(force=false)
  get_instance2('user.getrecenttracks', :recent_tracks, :track, {'user'=>@username}, force)
end

#recommendations(force = false) ⇒ Object



163
164
165
166
# File 'lib/scrobbler/user.rb', line 163

def recommendations(force=false)
  #warn "#{file}:#{lineno}:Warning: Scrobbler::User#recommendations is deprecated (not supported by the Last.fm 2.0 API)"
  get_instance(:systemrecs, :recommendations, :artist, force)
end

Raises:

  • (ArgumentError)


103
104
105
106
107
# File 'lib/scrobbler/user.rb', line 103

def recommended_events(format=:ics)
  format = :ics if format.to_s == 'ical'
  raise ArgumentError unless ['ics', 'rss'].include?(format.to_s)
  "#{API_URL.chop}#{api_path}/eventsysrecs.#{format}"
end

#top_albums(force = false, period = 'overall') ⇒ Object



129
130
131
# File 'lib/scrobbler/user.rb', line 129

def top_albums(force=false, period='overall')
  get_instance2('user.gettopalbums', :top_albums, :album, {'user'=>@username, 'period'=>period}, force)
end

#top_artists(force = false, period = 'overall') ⇒ Object



125
126
127
# File 'lib/scrobbler/user.rb', line 125

def top_artists(force=false, period='overall')
  get_instance2('user.gettopartists', :top_artists, :artist, {'user'=>@username, 'period'=>period}, force)
end

#top_tags(force = false) ⇒ Object



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

def top_tags(force=false)
  get_instance2('user.gettoptags', :top_tags, :tag, {'user'=>@username}, force)
end

#top_tracks(force = false, period = 'overall') ⇒ Object



133
134
135
# File 'lib/scrobbler/user.rb', line 133

def top_tracks(force=false, period='overall')
  get_instance2('user.gettoptracks', :top_tracks, :track, {'user'=>@username, 'period'=>period}, force)
end