Class: Grooveshark::User
- Inherits:
-
Object
- Object
- Grooveshark::User
- Defined in:
- lib/grooveshark/user.rb
Instance Attribute Summary collapse
-
#city ⇒ Object
readonly
Returns the value of attribute city.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#favorites ⇒ Object
readonly
Get user favorites.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#playlists ⇒ Object
readonly
Fetch user playlists.
-
#premium ⇒ Object
readonly
Returns the value of attribute premium.
-
#sex ⇒ Object
readonly
Returns the value of attribute sex.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
-
#add_favorite(song) ⇒ Object
Add song to favorites.
-
#avatar ⇒ Object
Get user avatar URL.
-
#create_playlist(name, description = '', songs = []) ⇒ Object
Create new user playlist.
-
#feed(date = nil) ⇒ Object
Get user activity for the date (COMES AS RAW RESPONSE).
-
#get_playlist(id) ⇒ Object
(also: #playlist)
Get playlist by ID.
-
#initialize(client, data = nil) ⇒ User
constructor
Init user account object.
-
#library(page = 0) ⇒ Object
Fetch songs from library.
-
#library_add(songs = []) ⇒ Object
Add songs to user’s library.
-
#library_remove(song) ⇒ Object
Remove song from user library.
-
#library_ts_modified ⇒ Object
Get library modification time.
-
#remove_favorite(song) ⇒ Object
Remove song from favorites.
Constructor Details
#initialize(client, data = nil) ⇒ User
Init user account object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/grooveshark/user.rb', line 8 def initialize(client, data=nil) if data @data = data @id = data['user_id'] @username = data['username'] @premium = data['is_premium'] @email = data['email'] @city = data['city'] @country = data['country'] @sex = data['sex'] end @client = client end |
Instance Attribute Details
#city ⇒ Object (readonly)
Returns the value of attribute city.
4 5 6 |
# File 'lib/grooveshark/user.rb', line 4 def city @city end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
4 5 6 |
# File 'lib/grooveshark/user.rb', line 4 def country @country end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
3 4 5 |
# File 'lib/grooveshark/user.rb', line 3 def data @data end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
3 4 5 |
# File 'lib/grooveshark/user.rb', line 3 def email @email end |
#favorites ⇒ Object (readonly)
Get user favorites
93 94 95 |
# File 'lib/grooveshark/user.rb', line 93 def favorites @favorites end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/grooveshark/user.rb', line 3 def id @id end |
#playlists ⇒ Object (readonly)
Fetch user playlists
65 66 67 |
# File 'lib/grooveshark/user.rb', line 65 def playlists @playlists end |
#premium ⇒ Object (readonly)
Returns the value of attribute premium.
3 4 5 |
# File 'lib/grooveshark/user.rb', line 3 def premium @premium end |
#sex ⇒ Object (readonly)
Returns the value of attribute sex.
4 5 6 |
# File 'lib/grooveshark/user.rb', line 4 def sex @sex end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
3 4 5 |
# File 'lib/grooveshark/user.rb', line 3 def username @username end |
Instance Method Details
#add_favorite(song) ⇒ Object
Add song to favorites
100 101 102 103 |
# File 'lib/grooveshark/user.rb', line 100 def add_favorite(song) song_id = song.kind_of?(Song) ? song.id : song @client.request('favorite', {:what => 'Song', :ID => song_id}) end |
#avatar ⇒ Object
Get user avatar URL
23 24 25 |
# File 'lib/grooveshark/user.rb', line 23 def avatar "http://images.grooveshark.com/static/userimages/#{@id}.jpg" end |
#create_playlist(name, description = '', songs = []) ⇒ Object
Create new user playlist
80 81 82 83 84 85 86 |
# File 'lib/grooveshark/user.rb', line 80 def create_playlist(name, description='', songs=[]) @client.request('createPlaylist', { 'playlistName' => name, 'playlistAbout' => description, 'songIDs' => songs.map { |s| s.kind_of?(Song) ? s.id : s.to_s } }) end |
#feed(date = nil) ⇒ Object
Get user activity for the date (COMES AS RAW RESPONSE)
28 29 30 31 |
# File 'lib/grooveshark/user.rb', line 28 def feed(date=nil) date = Time.now if date.nil? @client.request('getProcessedUserFeedData', {:userID => @id, :day => date.strftime("%Y%m%d")}) end |
#get_playlist(id) ⇒ Object Also known as: playlist
Get playlist by ID
72 73 74 75 |
# File 'lib/grooveshark/user.rb', line 72 def get_playlist(id) result = playlists.select { |p| p.id == id } result.nil? ? nil : result.first end |
#library(page = 0) ⇒ Object
Fetch songs from library
38 39 40 41 |
# File 'lib/grooveshark/user.rb', line 38 def library(page=0) resp = @client.request('userGetSongsInLibrary', {:userID => @id, :page => page.to_s})['songs'] resp.map { |s| Song.new(s) } end |
#library_add(songs = []) ⇒ Object
Add songs to user’s library
44 45 46 |
# File 'lib/grooveshark/user.rb', line 44 def library_add(songs=[]) @client.request('userAddSongsToLibrary', {:songs => songs.map { |s| s.to_hash }}) end |
#library_remove(song) ⇒ Object
Remove song from user library
49 50 51 52 53 |
# File 'lib/grooveshark/user.rb', line 49 def library_remove(song) raise ArgumentError, 'Song object required' unless song.kind_of?(Song) req = {:userID => @id, :songID => song.id, :albumID => song.album_id, :artistID => song.artist_id} @client.request('userRemoveSongFromLibrary', req) end |
#library_ts_modified ⇒ Object
Get library modification time
56 57 58 |
# File 'lib/grooveshark/user.rb', line 56 def library_ts_modified @client.request('userGetLibraryTSModified', {:userID => @id}) end |
#remove_favorite(song) ⇒ Object
Remove song from favorites
106 107 108 109 |
# File 'lib/grooveshark/user.rb', line 106 def remove_favorite(song) song_id = song.kind_of?(Song) ? song.id : song @client.request('unfavorite', {:what => 'Song', :ID => song_id}) end |