Class: Rockstar::User

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connection, fetch_and_parse, get_instance

Constructor Details

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

Returns a new instance of User.

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
# File 'lib/rockstar/user.rb', line 88

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

Instance Attribute Details

#ageObject

Returns the value of attribute age.



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

def age
  @age
end

#avatarObject

Returns the value of attribute avatar.



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

def avatar
  @avatar
end

#clusterObject

profile attributes



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

def cluster
  @cluster
end

#countryObject

Returns the value of attribute country.



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

def country
  @country
end

#genderObject

Returns the value of attribute gender.



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

def gender
  @gender
end

#idObject

profile attributes



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

def id
  @id
end

#imagesObject

Returns the value of attribute images.



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

def images
  @images
end

#matchObject

neighbor attributes



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

def match
  @match
end

#mbox_sha1sumObject

profile attributes



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

def mbox_sha1sum
  @mbox_sha1sum
end

#periodObject (readonly)

attributes needed to initialize



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

def period
  @period
end

#playcountObject

Returns the value of attribute playcount.



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

def playcount
  @playcount
end

#realnameObject

profile attributes



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

def realname
  @realname
end

#registeredObject

profile attributes



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

def registered
  @registered
end

#registered_unixtimeObject

Returns the value of attribute registered_unixtime.



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

def registered_unixtime
  @registered_unixtime
end

#urlObject

profile attributes



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

def url
  @url
end

#usernameObject (readonly)

attributes needed to initialize



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

def username
  @username
end

#weightObject

track fans attributes



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

def weight
  @weight
end

Class Method Details

.find(*args) ⇒ Object



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

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
71
72
73
74
75
76
77
78
# File 'lib/rockstar/user.rb', line 63

def new_from_xml(xml, doc=nil)
  u        = User.new((xml).at(:name).inner_html)
  u.url    = Base.fix_url((xml).at(:url).inner_html)    if (xml).at(:url)
  
  u.images = {}
  (xml/'image').each {|image|
    u.images[image['size']] = image.inner_html if u.images[image['size']].nil?
  }
  
  u.avatar = u.images['small']
  
  u.weight = (xml).at(:weight).inner_html if (xml).at(:weight)
  u.match  = (xml).at(:match).inner_html  if (xml).at(:match)
  u.realname= (xml).at(:realname).inner_html    if (xml).at(:realname)
  u
end

Instance Method Details

#charts(force = false) ⇒ Object



175
176
177
# File 'lib/rockstar/user.rb', line 175

def charts(force=false)
  get_instance("user.getWeeklyChartList", :charts, :chart, {:user => @username}, force)
end

#current_events(format = :ics) ⇒ Object



96
97
98
# File 'lib/rockstar/user.rb', line 96

def current_events(format=:ics)
  warn "[DEPRECATION] `current_events` is deprecated. The current api doesn't offer ics/ical formatted data."
end

#events(session_key, force = false) ⇒ Object

Get the recommendated events for the user, auth.session.key needed.



195
196
197
# File 'lib/rockstar/user.rb', line 195

def events(session_key, force = false)
  get_instance("user.getEvents", :events, :event, {:user => @username, :sk => session_key}, force)
end

#friends(force = false) ⇒ Object



144
145
146
# File 'lib/rockstar/user.rb', line 144

def friends(force=false)
  get_instance("user.getFriends", :friends, :user, {:user => @username}, force)
end

#friends_events(format = :ics) ⇒ Object



100
101
102
# File 'lib/rockstar/user.rb', line 100

def friends_events(format=:ics)
  warn "[DEPRECATION] `friends_events` is deprecated. The current api doesn't offer ics/ical formatted data."
end

#load_profileObject



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

def load_profile
  doc                  = self.class.fetch_and_parse("user.getInfo", {:user => @username})
  @id                  = (doc).at(:id).inner_html
  @url                 = Base.fix_url((doc).at(:url).inner_html)
  @realname            = (doc).at(:realname).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
  
  @images = {}
  (doc/'image').each {|image|
    @images[image['size']] = image.inner_html
  }
  
  @avatar              = @images["small"]
end

#neighbours(force = false) ⇒ Object



148
149
150
# File 'lib/rockstar/user.rb', line 148

def neighbours(force=false)
  get_instance("user.getNeighbours", :neighbours, :user, {:user => @username}, force)
end

#recent_banned_tracks(force = false) ⇒ Object



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

def recent_banned_tracks(force=false)
  warn "[DEPRECATION] `recent_banned_tracks` is deprecated. The current api doesn't offer this function"
  []
end

#recent_loved_tracks(force = false) ⇒ Object



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

def recent_loved_tracks(force=false)
  get_instance("user.getLovedTracks", :recent_loved_tracks, :track, {:user => @username}, force)
end

#recent_tracks(force = false) ⇒ Object



152
153
154
# File 'lib/rockstar/user.rb', line 152

def recent_tracks(force=false)
  get_instance("user.getRecentTracks", :recent_tracks, :track, {:user => @username}, force)
end

#recommendations(force = false) ⇒ Object



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

def recommendations(force=false)
  warn "[DEPRECATION] `recommendations` is deprecated. Please use recommended_artists"
  []
end

The session_key is returned by auth.session.key



171
172
173
# File 'lib/rockstar/user.rb', line 171

def recommended_artists(session_key, force=false)
  get_instance("user.getRecommendedArtists", :recommendations, :artist, {:user => @username, :sk => session_key}, force)
end


104
105
106
# File 'lib/rockstar/user.rb', line 104

def recommended_events(format=:ics)
  warn "[DEPRECATION] `recommended_events` is deprecated. The current api doesn't offer ics/ical formatted data."
end

#top_albums(force = false) ⇒ Object



132
133
134
# File 'lib/rockstar/user.rb', line 132

def top_albums(force=false)
  get_instance("user.getTopAlbums", :top_albums, :album, {:user => @username}, force)
end

#top_artists(force = false) ⇒ Object



128
129
130
# File 'lib/rockstar/user.rb', line 128

def top_artists(force=false)
  get_instance("user.getTopArtists", :top_artists, :artist, {:user => @username, :period => @period}, force)
end

#top_tags(force = false) ⇒ Object



140
141
142
# File 'lib/rockstar/user.rb', line 140

def top_tags(force=false)
  get_instance("user.getTopTags", :top_tags, :tag, {:user => @username}, force)
end

#top_tracks(force = false) ⇒ Object



136
137
138
# File 'lib/rockstar/user.rb', line 136

def top_tracks(force=false)
  get_instance("user.getTopTracks", :top_tracks, :track, {:user => @username}, force)
end

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



184
185
186
187
# File 'lib/rockstar/user.rb', line 184

def weekly_album_chart(from=nil, to=nil)
  doc = self.class.fetch_and_parse("user.getWeeklyAlbumChart", {:user => @username, :from => from, :to => to})
  (doc/:album).inject([]) { |elements, el| elements << Album.new_from_xml(el); elements }
end

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



179
180
181
182
# File 'lib/rockstar/user.rb', line 179

def weekly_artist_chart(from=nil, to=nil)
  doc = self.class.fetch_and_parse("user.getWeeklyArtistChart", {:user => @username, :from => from, :to => to})
  (doc/:artist).inject([]) { |elements, el| elements << Artist.new_from_xml(el); elements }
end

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



189
190
191
192
# File 'lib/rockstar/user.rb', line 189

def weekly_track_chart(from=nil, to=nil)
  doc = self.class.fetch_and_parse("user.getWeeklyTrackChart", {:user => @username, :from => from, :to => to})
  (doc/:track).inject([]) { |elements, el| elements << Track.new_from_xml(el); elements }
end