Class: Grooveshark::User
- Inherits:
-
Object
- Object
- Grooveshark::User
- Defined in:
- lib/grooveshark/user.rb
Overview
User class
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.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#playlists ⇒ Object
readonly
Fetch user playlists.
-
#premium ⇒ Object
readonly
Returns the value of attribute premium.
-
#sex ⇒ Object
readonly
Returns the value of attribute sex.
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
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/grooveshark/user.rb', line 10 def initialize(client, data = nil) if data @data = data @id = data['user_id'] @name = data['f_name'] @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.
6 7 8 |
# File 'lib/grooveshark/user.rb', line 6 def city @city end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
6 7 8 |
# File 'lib/grooveshark/user.rb', line 6 def country @country end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/grooveshark/user.rb', line 5 def data @data end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
5 6 7 |
# File 'lib/grooveshark/user.rb', line 5 def email @email end |
#favorites ⇒ Object (readonly)
Get user favorites
109 110 111 |
# File 'lib/grooveshark/user.rb', line 109 def favorites @favorites end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/grooveshark/user.rb', line 5 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/grooveshark/user.rb', line 5 def name @name end |
#playlists ⇒ Object (readonly)
Fetch user playlists
78 79 80 |
# File 'lib/grooveshark/user.rb', line 78 def playlists @playlists end |
#premium ⇒ Object (readonly)
Returns the value of attribute premium.
5 6 7 |
# File 'lib/grooveshark/user.rb', line 5 def premium @premium end |
#sex ⇒ Object (readonly)
Returns the value of attribute sex.
6 7 8 |
# File 'lib/grooveshark/user.rb', line 6 def sex @sex end |
Instance Method Details
#add_favorite(song) ⇒ Object
Add song to favorites
116 117 118 119 |
# File 'lib/grooveshark/user.rb', line 116 def add_favorite(song) song_id = song.is_a?(Song) ? song.id : song @client.request('favorite', what: 'Song', ID: song_id) end |
#avatar ⇒ Object
Get user avatar URL
25 26 27 |
# File 'lib/grooveshark/user.rb', line 25 def avatar "http://images.grooveshark.com/static/userimages/#{@id}.jpg" end |
#create_playlist(name, description = '', songs = []) ⇒ Object
Create new user playlist
95 96 97 98 99 100 101 102 |
# File 'lib/grooveshark/user.rb', line 95 def create_playlist(name, description = '', songs = []) @client.request('createPlaylist', 'playlistName' => name, 'playlistAbout' => description, 'songIDs' => songs.map do |s| s.is_a?(Song) ? s.id : s.to_s end) end |
#feed(date = nil) ⇒ Object
Get user activity for the date (COMES AS RAW RESPONSE)
30 31 32 33 34 35 |
# File 'lib/grooveshark/user.rb', line 30 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
87 88 89 90 |
# File 'lib/grooveshark/user.rb', line 87 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
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/grooveshark/user.rb', line 42 def library(page = 0) songs = [] resp = @client.request('userGetSongsInLibrary', userID: @id, page: page.to_s) songs = resp['songs'].map do |song| Song.new song end if resp.key?('songs') songs end |
#library_add(songs = []) ⇒ Object
Add songs to user’s library
54 55 56 |
# File 'lib/grooveshark/user.rb', line 54 def library_add(songs = []) @client.request('userAddSongsToLibrary', songs: songs.map(&:to_hash)) end |
#library_remove(song) ⇒ Object
Remove song from user library
59 60 61 62 63 64 65 66 |
# File 'lib/grooveshark/user.rb', line 59 def library_remove(song) fail ArgumentError, 'Song object required' unless song.is_a?(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
69 70 71 |
# File 'lib/grooveshark/user.rb', line 69 def library_ts_modified @client.request('userGetLibraryTSModified', userID: @id) end |
#remove_favorite(song) ⇒ Object
Remove song from favorites
122 123 124 125 |
# File 'lib/grooveshark/user.rb', line 122 def remove_favorite(song) song_id = song.is_a?(Song) ? song.id : song @client.request('unfavorite', what: 'Song', ID: song_id) end |