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

#albums(force = false, options = {}) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/rockstar/user.rb', line 189

def albums(force=false, options = {})
  default_options = {
    :user => @username
  }
  options = default_options.merge(options)

  library = Rockstar::Library.new
  library.albums(false, options)
end

#artists(force = false, options = {}) ⇒ Object

Wrappers for Rockstar::Library



179
180
181
182
183
184
185
186
187
# File 'lib/rockstar/user.rb', line 179

def artists(force=false, options = {})
  default_options = {
    :user => @username
  }
  options = default_options.merge(options)

  library = Rockstar::Library.new
  library.artists(false, options)
end

#charts(force = false) ⇒ Object



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

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

#events(session_key, force = false) ⇒ Object

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



200
201
202
# File 'lib/rockstar/user.rb', line 200

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

#friends(force = false) ⇒ Object



138
139
140
# File 'lib/rockstar/user.rb', line 138

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

#load_profileObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rockstar/user.rb', line 96

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



142
143
144
# File 'lib/rockstar/user.rb', line 142

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

#recent_loved_tracks(force = false) ⇒ Object



150
151
152
# File 'lib/rockstar/user.rb', line 150

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

#recent_tracks(force = false) ⇒ Object



146
147
148
# File 'lib/rockstar/user.rb', line 146

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

The session_key is returned by auth.session.key



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

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

#top_albums(force = false) ⇒ Object



126
127
128
# File 'lib/rockstar/user.rb', line 126

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

#top_artists(force = false, options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/rockstar/user.rb', line 116

def top_artists(force=false, options = {})
  default_options = {
    :period => self.period
  }
  options = default_options.merge(options)
  options[:user] = @username

  get_instance("user.getTopArtists", :top_artists, :artist, options, force)
end

#top_tags(force = false) ⇒ Object



134
135
136
# File 'lib/rockstar/user.rb', line 134

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

#top_tracks(force = false) ⇒ Object



130
131
132
# File 'lib/rockstar/user.rb', line 130

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

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



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

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



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

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



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

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