Class: Grooveshark::User

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

Overview

User class

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#cityObject (readonly)

Returns the value of attribute city.



6
7
8
# File 'lib/grooveshark/user.rb', line 6

def city
  @city
end

#countryObject (readonly)

Returns the value of attribute country.



6
7
8
# File 'lib/grooveshark/user.rb', line 6

def country
  @country
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#emailObject (readonly)

Returns the value of attribute email.



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

def email
  @email
end

#favoritesObject (readonly)

Get user favorites



109
110
111
# File 'lib/grooveshark/user.rb', line 109

def favorites
  @favorites
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#playlistsObject (readonly)

Fetch user playlists



78
79
80
# File 'lib/grooveshark/user.rb', line 78

def playlists
  @playlists
end

#premiumObject (readonly)

Returns the value of attribute premium.



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

def premium
  @premium
end

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

#avatarObject

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_modifiedObject

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