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

connection, fetch_and_parse

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)['username'])
  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_path(version = nil) ⇒ Object



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

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

#charts(force = false) ⇒ Object



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

def charts(force=false)
  get_instance(:weeklychartlist, :charts, :chart, 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) ⇒ Object



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

def friends(force=false)
  get_instance(:friends, :friends, :user, 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_instance(:neighbours, :neighbours, :user, force)
end

#recent_banned_tracks(force = false) ⇒ Object



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

def recent_banned_tracks(force=false)
  get_instance(:recentbannedtracks, :recent_banned_tracks, :track, force)
end

#recent_loved_tracks(force = false) ⇒ Object



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

def recent_loved_tracks(force=false)
  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_instance(:recenttracks, :recent_tracks, :track, force, "2.0")
end

#recommendations(force = false) ⇒ Object



161
162
163
# File 'lib/scrobbler/user.rb', line 161

def recommendations(force=false)
  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) ⇒ Object



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

def top_albums(force=false)
  get_instance(:topalbums, :top_albums, :album, force)
end

#top_artists(force = false) ⇒ Object



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

def top_artists(force=false)
  get_instance(:topartists, :top_artists, :artist, force)
end

#top_tags(force = false) ⇒ Object



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

def top_tags(force=false)
  get_instance(:toptags, :top_tags, :tag, force)
end

#top_tracks(force = false) ⇒ Object



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

def top_tracks(force=false)
  get_instance(:toptracks, :top_tracks, :track, force)
end

#weekly_album_chart(from = nil, to = nil) ⇒ Object



175
176
177
178
179
# File 'lib/scrobbler/user.rb', line 175

def weekly_album_chart(from=nil, to=nil)
  qs  = create_query_string_from_timestamps(from, to)
  doc = self.class.fetch_and_parse("#{api_path}/weeklyalbumchart.xml#{qs}")
  (doc/:album).inject([]) { |elements, el| elements << Album.new_from_xml(el); elements }
end

#weekly_artist_chart(from = nil, to = nil) ⇒ Object



169
170
171
172
173
# File 'lib/scrobbler/user.rb', line 169

def weekly_artist_chart(from=nil, to=nil)
  qs  = create_query_string_from_timestamps(from, to)
  doc = self.class.fetch_and_parse("#{api_path}/weeklyartistchart.xml#{qs}")
  (doc/:artist).inject([]) { |elements, el| elements << Artist.new_from_xml(el); elements }
end

#weekly_track_chart(from = nil, to = nil) ⇒ Object



181
182
183
184
185
# File 'lib/scrobbler/user.rb', line 181

def weekly_track_chart(from=nil, to=nil)
  qs  = create_query_string_from_timestamps(from, to)
  doc = self.class.fetch_and_parse("#{api_path}/weeklytrackchart.xml#{qs}")
  (doc/:track).inject([]) { |elements, el| elements << Track.new_from_xml(el); elements }
end