Class: Grooveshark::User

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#cityObject (readonly)

Returns the value of attribute city.



4
5
6
# File 'lib/grooveshark/user.rb', line 4

def city
  @city
end

#countryObject (readonly)

Returns the value of attribute country.



4
5
6
# File 'lib/grooveshark/user.rb', line 4

def country
  @country
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/grooveshark/user.rb', line 3

def data
  @data
end

#emailObject (readonly)

Returns the value of attribute email.



3
4
5
# File 'lib/grooveshark/user.rb', line 3

def email
  @email
end

#favoritesObject (readonly)

Get user favorites



93
94
95
# File 'lib/grooveshark/user.rb', line 93

def favorites
  @favorites
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/grooveshark/user.rb', line 3

def id
  @id
end

#playlistsObject (readonly)

Fetch user playlists



65
66
67
# File 'lib/grooveshark/user.rb', line 65

def playlists
  @playlists
end

#premiumObject (readonly)

Returns the value of attribute premium.



3
4
5
# File 'lib/grooveshark/user.rb', line 3

def premium
  @premium
end

#sexObject (readonly)

Returns the value of attribute sex.



4
5
6
# File 'lib/grooveshark/user.rb', line 4

def sex
  @sex
end

#usernameObject (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

#avatarObject

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

Raises:

  • (ArgumentError)


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_modifiedObject

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