Class: Rdio::Api

Inherits:
BaseApi show all
Defined in:
lib/rdio/api.rb

Overview


Provides main API functionality by translating Ruby calls to REST calls to the super class


Constant Summary

Constants inherited from BaseApi

BaseApi::PATH

Instance Attribute Summary

Attributes inherited from BaseApi

#oauth

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseApi

#access_token=, #authorize, #call, #get_pin, #get_pin=, #return_object

Constructor Details

#initialize(key = nil, secret = nil) ⇒ Api

Returns a new instance of Api.



9
10
11
# File 'lib/rdio/api.rb', line 9

def initialize(key=nil,secret=nil)
  super key,secret
end

Class Method Details

.from_token(token) ⇒ Object

AccessToken -> Api

Returns a new instance initialized from the passed in access token



18
19
20
21
22
# File 'lib/rdio/api.rb', line 18

def self.from_token(token)
  api = Api.new
  api.access_token = token
  return api
end

Instance Method Details

#addFriend(user) ⇒ Object

Add a friend to the current user.



25
26
27
28
29
30
# File 'lib/rdio/api.rb', line 25

def addFriend(user)
  method = 'addFriend'
  type = true
  args = {:user=>user}
  return_object type,method,args,true
end

#addToCollection(objs) ⇒ Object

Add tracks or playlists to the current user’s collection.



33
34
35
36
37
38
# File 'lib/rdio/api.rb', line 33

def addToCollection(objs)
  method = 'addToCollection'
  type = true
  args = {:keys=>Rdio::keys(objs)}
  return_object type,method,args,true
end

#addToPlaylist(playlist, tracks) ⇒ Object

Add a track to a playlist.



41
42
43
44
45
46
# File 'lib/rdio/api.rb', line 41

def addToPlaylist(playlist,tracks)
  method = 'addToPlaylist'
  type = true
  args = {:playlist=>playlist, :tracks=>Rdio::keys(tracks)}
  return_object type,method,args,true
end

#counts(query, types = nil, never_or = nil, extras = nil, start = nil, count = nil) ⇒ Object



391
392
393
394
# File 'lib/rdio/api.rb', line 391

def counts(query,types=nil,never_or=nil,extras=nil,start=nil,count=nil)
  obj = search_json query,types,never_or,extras,start,count
  return JSONObj.new obj
end

#createPlaylist(name, description, tracks, extras = nil) ⇒ Object

Create a new playlist in the current user’s collection. The new playlist will be returned if the creation is successful, otherwise null will be returned.



51
52
53
54
55
56
57
58
# File 'lib/rdio/api.rb', line 51

def createPlaylist(name,description,tracks,extras=nil)
  method = 'createPlaylist'
  type = Playlist
  args = {:name=>name,:description=>description,
    :tracks=>Rdio::keys(tracks)}
  args[:extras] = extras if extras
  return_object type,method,args,true
end

#currentUser(extras = nil) ⇒ Object

Get information about the currently logged in user.



61
62
63
64
65
66
67
# File 'lib/rdio/api.rb', line 61

def currentUser(extras=nil)
  method = 'currentUser'
  type = User
  args = {}
  args[:extras] = extras if extras
  return_object type,method,args,true
end

#deletePlaylist(playlist) ⇒ Object

Delete a playlist.



70
71
72
73
74
75
# File 'lib/rdio/api.rb', line 70

def deletePlaylist(playlist)
  method = 'deletePlaylist'
  type = Boolean
  args = {:playlist=>playlist}
  return_object type,method,args,true
end

#findUserByEmail(email) ⇒ Object

Find a user either by email address or by their username. Exactly one of email or vanityName must be supplied.



79
80
81
82
83
84
# File 'lib/rdio/api.rb', line 79

def findUserByEmail(email)
  method = 'findUser'
  type = User
  args = {:email=>email}
  return_object type,method,args
end

#findUserByVanityName(name) ⇒ Object

Find a user either by email address or by their username. Exactly one of email or vanityName must be supplied.



88
89
90
91
92
93
# File 'lib/rdio/api.rb', line 88

def findUserByVanityName(name)
  method = 'findUser'
  type = User
  args = {:vanityName=>name}
  return_object type,method,args
end

#get(objs, type = nil, extras = nil) ⇒ Object

Fetch one or more objects from Rdio.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rdio/api.rb', line 96

def get(objs,type=nil,extras=nil)
  if not objs.is_a? Array
    objs = [objs]
  end
  method = 'get'
  cls = type
  args = {:keys=>Rdio::keys(objs)}
  args[:extras] = extras if extras
  json = call method,args
  if Rdio::log_json
    Rdio::log "json: #{json}"
  end
  create_object type,json,true
end

#getActivityStream(user, scope, last_id = nil) ⇒ Object

Get the activity events for a user, a user’s friends, or everyone on Rdio.



113
114
115
116
117
118
119
120
# File 'lib/rdio/api.rb', line 113

def getActivityStream(user,scope,last_id=nil)
  method = 'getActivityStream'
  type = ActivityStream
  args = {:user=>user,:scope=>scope}
  args[:last_id] = last_id if last_id
  auth = user
  return_object type,method,args,auth
end

#getAlbumsForArtist(artist, featuring = nil, extras = nil, start = nil, count = nil) ⇒ Object

Return the albums by (or featuring) an artist.



123
124
125
126
127
128
129
130
131
132
# File 'lib/rdio/api.rb', line 123

def getAlbumsForArtist(artist,featuring=nil,extras=nil,start=nil,count=nil)
  method = 'getAlbumsForArtist'
  type = Album
  args = {:artist=>artist}
  args[:featuring] = featuring if featuring
  args[:extras] = extras if extras
  args[:start] = start if start
  args[:count] = count if count
  return_object type,method,args
end

#getAlbumsForArtistInCollection(artist, user = nil, extras = nil) ⇒ Object

Get the albums in the user’s collection by a particular artist.



135
136
137
138
139
140
141
142
143
# File 'lib/rdio/api.rb', line 135

def getAlbumsForArtistInCollection(artist,user=nil,extras=nil)
  method = 'getAlbumsForArtistInCollection'
  type = Album
  args = {:artist=>artist}
  args[:user] = user if user
  args[:extras] = extras if extras
  auth = !!user
  return_object type,method,args,auth
end

#getAlbumsInCollection(user = nil, start = nil, count = nil, sort = nil, query = nil, extras = nil) ⇒ Object

Get all of the albums in the user’s collection.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rdio/api.rb', line 146

def getAlbumsInCollection(user=nil,start=nil,count=nil,
                          sort=nil,query=nil,extras=nil)
  method = 'getAlbumsInCollection'
  type = Album
  args = {}
  args[:user] = user if user
  args[:start] = start if start
  args[:count] = count if count
  args[:sort] = sort if sort
  args[:query] = query if query
  args[:extras] = extras if extras
  auth = !!user
  return_object type,method,args,auth
end

#getArtistsInCollection(user = nil, start = nil, count = nil, sort = nil, query = nil, extras = nil) ⇒ Object

Get all of the artist in a user’s collection.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rdio/api.rb', line 162

def getArtistsInCollection(user=nil,start=nil,count=nil,sort=nil,
                           query=nil,extras=nil)
  method = 'getArtistsInCollection'
  type = Artist
  args = {}
  args[:user] = user if user
  args[:start] = start if start
  args[:count] = count if count
  args[:sort] = sort if sort
  args[:query] = query if query
  args[:extras] = extras if extras
  auth = !!user
  return_object type,method,args,auth
end

#getHeavyRotation(user = nil, type = nil, friends = nil, limit = nil, extras = nil) ⇒ Object

Find the most popular artists or albums for a user, their friends or the whole site.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rdio/api.rb', line 179

def getHeavyRotation(user=nil,type=nil,friends=nil,limit=nil,extras=nil)
  method = 'getHeavyRotation'
  cls = TODO
  if type == 'artist'
    cls = Artist
  elsif type == 'album'
    cls = Album
  end
  args = {}
  args[:user] = user if user
  args[:type] = type if type
  args[:friends] = friends if friends
  args[:limit] = limit if limit
  args[:extras] = extras if extras
  return_object cls,method,args
end

#getNewReleases(time = nil, start = nil, count = nil, extras = nil) ⇒ Object

Return new albums released across a timeframe.



197
198
199
200
201
202
203
204
205
206
# File 'lib/rdio/api.rb', line 197

def getNewReleases(time=nil,start=nil,count=nil,extras=nil)
  method = 'getNewReleases'
  type = Album
  args = {}
  args[:time] = time if time
  args[:start] = start if start
  args[:count] = count if count
  args[:extras] = extras if extras
  return_object type,method,args
end

#getObjectFromShortCode(short_code, type = nil) ⇒ Object

Return the object that the supplied Rdio short-code is a representation of, or null if the short-code is invalid.



210
211
212
213
214
215
# File 'lib/rdio/api.rb', line 210

def getObjectFromShortCode(short_code,type=nil)
  method = 'getObjectFromShortCode'
  type = BaseObj if not type
  args = {:short_code=>short_code}
  return_object type,method,args,true
end

#getObjectFromUrl(url, type = nil) ⇒ Object

Return the object that the supplied Rdio url is a representation of, or null if the url doesn’t represent an object.



219
220
221
222
223
224
# File 'lib/rdio/api.rb', line 219

def getObjectFromUrl(url,type=nil)
  method = 'getObjectFromUrl'
  type = BaseObj if not type
  args = {:url=>url}
  return_object type,method,args,true
end

#getPlaybackToken(domain = nil) ⇒ Object

Get an playback token. If you are using this for web playback you must supply a domain.



228
229
230
231
232
233
234
# File 'lib/rdio/api.rb', line 228

def getPlaybackToken(domain=nil)
  method = 'getPlaybackToken'
  type = String
  args = {}
  args[:domain] = domain if domain
  return_object type,method,args,true
end

#getPlaylists(extras = nil) ⇒ Object

Get the current user’s playlists.



237
238
239
240
241
242
243
# File 'lib/rdio/api.rb', line 237

def getPlaylists(extras=nil)
  method = 'getPlaylists'
  type = Playlist
  args = {}
  args[:extras] = extras if extras
  return_object type,method,args,true
end

#getTopCharts(type, start = nil, count = nil, extras = nil) ⇒ Object

Return the site-wide most popular items for a given type.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rdio/api.rb', line 246

def getTopCharts(type,start=nil,count=nil,extras=nil)
  method = 'getTopCharts'
  cls = TODO
  case type
  when 'Artist'
    cls = Artist
  when 'Album'
    cls = Album
  when 'Track'
    cls = Track
  when 'Playlist'
    cls = Playlist
  end
  args = {:type=>type}
  return_object cls,method,args
end

#getTracksForAlbumInCollection(album, user = nil, extras = nil) ⇒ Object

Which tracks on the given album are in the user’s collection.



264
265
266
267
268
269
270
271
# File 'lib/rdio/api.rb', line 264

def getTracksForAlbumInCollection(album,user=nil,extras=nil)
  method = 'getTracksForAlbumInCollection'
  type = Track
  args = {:album=>album}
  args[:user] = user if user
  args[:extras] = extras if extras
  return_object type,method,args
end

#getTracksForArtist(artist, appears_on = nil, start = nil, count = nil, extras = nil) ⇒ Object

Get all of the tracks by this artist.



274
275
276
277
278
279
280
281
282
283
# File 'lib/rdio/api.rb', line 274

def getTracksForArtist(artist,appears_on=nil,start=nil,count=nil,extras=nil)
  method = 'getTracksForArtist'
  type = Track
  args = {:artist=>artist}
  args[:appears_on] = appears_on if appears_on
  args[:start] = start if start
  args[:count] = count if count
  args[:extras] = extras if extras
  return_object type,method,args
end

#getTracksForArtistInCollection(artist, user = nil, extras = nil) ⇒ Object

Which tracks from the given artist are in the user’s collection.



286
287
288
289
290
291
292
293
# File 'lib/rdio/api.rb', line 286

def getTracksForArtistInCollection(artist,user=nil,extras=nil)
  method = 'getTracksForArtistInCollection'
  type = Track
  args = {:artist=>artist}
  args[:user] = user if user
  args[:extras] = extras if extras
  return_object type,method,args
end

#getTracksInCollection(user = nil, start = nil, count = nil, sort = nil, query = nil, extras = nil) ⇒ Object

Get all of the tracks in the user’s collection.



296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/rdio/api.rb', line 296

def getTracksInCollection(user=nil,start=nil,count=nil,sort=nil,query=nil,extras=nil)
  method = 'getTracksInCollection'
  type = Track
  args = {}
  args[:user] = user if user
  args[:start] = start if start
  args[:count] = count if count
  args[:sort] = sort if sort
  args[:query] = query if query
  args[:extras] = extras if extras
  return_object type,method,args
end

#removeFriend(user) ⇒ Object

Remove a friend from the current user.



310
311
312
313
314
315
# File 'lib/rdio/api.rb', line 310

def removeFriend(user)
  method = 'removeFriend'
  type = Boolean
  args = {:user=>user}
  return_object type,method,args,true
end

#removeFromCollection(objs) ⇒ Object

Remove tracks or playlists from the current user’s collection.



318
319
320
321
322
323
# File 'lib/rdio/api.rb', line 318

def removeFromCollection(objs)
  method = 'removeFromCollection'
  type = Boolean
  args = {:keys=>Rdio::keys(objs)}
  return_object type,method,args
end

#removeFromPlaylist(playlist, index, count, tracks) ⇒ Object

Remove an item from a playlist by its position in the playlist.



326
327
328
329
330
331
332
# File 'lib/rdio/api.rb', line 326

def removeFromPlaylist(playlist,index,count,tracks)
  method = 'removeFromPlaylist'
  type = TODO
  args = {:playlist=>playlist,:index=>index,
    :count=>count,:tracks=>Rdio::keys(tracks)}
  return_object type,method,args,true
end

#search(query, types = nil, never_or = nil, extras = nil, start = nil, count = nil) ⇒ Object

Search for artists, albums, tracks, users or all kinds of objects.



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/rdio/api.rb', line 369

def search(query,types=nil,never_or=nil,extras=nil,start=nil,count=nil)
  result = search_json query,types,never_or,extras,start,count
  return result if not result
  results = result['results'] || []
  api = self
  #
  # This start out nil, because we need to reference classes in
  # types.rb and this gets loaded after this file.  There's
  # probably a better way to do this.
  #
  if not @@types2classes
    @@types2classes = {
      'r' => Artist,
      'a' => Album,
      's' => User,
      't' => Track,
      'p' => Playlist
    }
  end
  results.map {|o| @@types2classes[o['type']].new(api).fill o}
end

#searchSuggestions(query, extras) ⇒ Object

Match the supplied prefix against artists, albums, tracks and people in the Rdio system. Return the first ten matches.



398
399
400
401
402
403
404
# File 'lib/rdio/api.rb', line 398

def searchSuggestions(query,extras)
  method = 'searchSuggestions'
  type = TODO
  args = {:query=>query}
  args[:extras] = extras if extras
  return_object type,method,args
end

#setPlaylistCollaborating(playlist, collaborating) ⇒ Object

Start or stop collaborating on this playlist.



335
336
337
338
339
340
# File 'lib/rdio/api.rb', line 335

def setPlaylistCollaborating(playlist,collaborating)
  method = 'setPlaylistCollaborating'
  type = Boolean
  args = {:playlist=>playlist,:collaborating=>collaborating}
  return_object type,method,args,true
end

#setPlaylistCollaborationMode(playlist, mode) ⇒ Object

Start or stop collaborating on this playlist.



343
344
345
346
347
348
# File 'lib/rdio/api.rb', line 343

def setPlaylistCollaborationMode(playlist,mode)
  method = 'setPlaylistCollaborationMode'
  type = Boolean
  args = {:playlist=>playlist,:mode=>mode}
  return_object type,method,args,true
end

#setPlaylistFields(playlist, name, description) ⇒ Object

Sets the name and description for a playlist.



361
362
363
364
365
366
# File 'lib/rdio/api.rb', line 361

def setPlaylistFields(playlist,name,description)
  method = 'setPlaylistFields'
  type = Boolean
  args = {:playlist=>playlist,:name=>name,:description=>description}
  return_object type,method,args,true
end

#setPlaylistOrder(playlist, tracks) ⇒ Object

Saves the given order of tracks in a given playlist. The new order must have the same tracks as the previous order (this method may not be used to add/remove tracks).



353
354
355
356
357
358
# File 'lib/rdio/api.rb', line 353

def setPlaylistOrder(playlist,tracks)
  method = 'setPlaylistOrder'
  type = Boolean
  args = {:playlist=>playlist,:tracks=>Rdio::keys(tracks)}
  return_object type,method,args,true
end