rdio 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Jeffrey Palm
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,26 @@
1
+ -*- outline -*-
2
+
3
+ * ABOUT
4
+
5
+ This is a ruby implementation of the rd.io API here:
6
+
7
+ http:/developer.rdio.com
8
+
9
+
10
+ * USAGE
11
+
12
+ To use it you have to get an api key and secret here:
13
+
14
+ And then, currently, you can do something like the following:
15
+
16
+ key = <RDIO_KEY>
17
+ secret = <RDIO_SECRET>
18
+ Rdio::init key,sec
19
+ user = Rdio::User.find_by_email 'jeff@jeffpalm.com'
20
+ 'Jeffrey' == user.first_name
21
+ 'Palm' == user.last_name
22
+
23
+
24
+ * TODO
25
+
26
+ Lots
data/lib/rdio.rb ADDED
@@ -0,0 +1,87 @@
1
+ $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
2
+
3
+ # core
4
+
5
+ # stdlib
6
+ require 'logger'
7
+
8
+ # third party
9
+
10
+ # internal requires
11
+ require 'rdio/base'
12
+ require 'rdio/api'
13
+ require 'rdio/oauth'
14
+ require 'rdio/datatypes'
15
+ require 'rdio/types'
16
+
17
+ module Rdio
18
+ VERSION = '0.0.1'
19
+
20
+ RDIO_KEY = 'RDIO_KEY'
21
+ RDIO_SECRET = 'RDIO_SECRET'
22
+
23
+ class << self
24
+ attr_accessor :debug
25
+ attr_accessor :logger
26
+ attr_accessor :log_json
27
+ attr_accessor :log_methods
28
+ attr_accessor :log_symbols
29
+ attr_accessor :log_posts
30
+ def log(str)
31
+ logger.debug { str }
32
+ end
33
+ end
34
+ self.debug = false
35
+ self.log_json = false
36
+ self.log_methods = false
37
+ self.log_symbols = false
38
+ self.log_posts = false
39
+
40
+ @logger ||= ::Logger.new(STDERR)
41
+ @api = nil
42
+
43
+ def self.version
44
+ VERSION
45
+ end
46
+
47
+ # Initializes and returns the shared Api instance and overwrites the
48
+ # existing one.
49
+ def self.init(key,secret)
50
+ @api = Api.new key,secret
51
+ end
52
+
53
+ # Resets -- mainly for oauth
54
+ def self.reset
55
+ @api = nil
56
+ end
57
+
58
+ # Returns the shared Rdio::Api instance. If it hasn't been set, we
59
+ # try to construct it from thte environment
60
+ def self.api
61
+ return @api if @api
62
+ key = ENV[RDIO_KEY]
63
+ secret = ENV[RDIO_SECRET]
64
+ if key and secret
65
+ @api = init key,secret
66
+ else
67
+ msg = <<HERE
68
+ Could not construct an Api instance. You must first call init(key,secret)
69
+ or set the following environmental variables:
70
+ - #{RDIO_KEY}
71
+ - #{RDIO_SECRET}
72
+ HERE
73
+ raise Exception.new msg
74
+ end
75
+ return @api
76
+ end
77
+ end
78
+
79
+ # Silly syntax so you can say Rd::io...blah...blah...blah
80
+ module Rd
81
+
82
+ # Returns the shared Rdio::Api instance from Rdio::api
83
+ def self.io
84
+ Rdio::api
85
+ end
86
+
87
+ end
data/lib/rdio/api.rb ADDED
@@ -0,0 +1,339 @@
1
+ module Rdio
2
+
3
+ # ----------------------------------------------------------------------
4
+ # Provides main API functionality by translating Ruby calls to REST
5
+ # calls to the super class
6
+ # ----------------------------------------------------------------------
7
+ class Api < BaseApi
8
+
9
+ def initialize(key,secret)
10
+ super key,secret
11
+ end
12
+
13
+ # Add a friend to the current user.
14
+ def addFriend(user)
15
+ method = 'addFriend'
16
+ type = true
17
+ args = {:user=>user}
18
+ return_object type,method,args,true
19
+ end
20
+
21
+ # Add tracks or playlists to the current user's collection.
22
+ def addToCollection(objs)
23
+ method = 'addToCollection'
24
+ type = true
25
+ args = {:keys=>keys(objs)}
26
+ return_object type,method,args,true
27
+ end
28
+
29
+ # Add a track to a playlist.
30
+ def addToPlaylist(playlist,tracks)
31
+ method = 'addToPlaylist'
32
+ type = true
33
+ args = {:playlist=>playlist, :tracks=>keys(tracks)}
34
+ return_object type,method,args,true
35
+ end
36
+
37
+ # Create a new playlist in the current user's collection. The new
38
+ # playlist will be returned if the creation is successful, otherwise
39
+ # null will be returned.
40
+ def createPlaylist(name,description,tracks,extras=nil)
41
+ method = 'createPlaylist'
42
+ type = Playlist
43
+ args = {:name=>name,:description=>description,
44
+ :tracks=>keys(tracks)}
45
+ args[:extras] = extras if extras
46
+ return_object type,method,args,true
47
+ end
48
+
49
+ # Get information about the currently logged in user.
50
+ def currentUser(extras=nil)
51
+ method = 'currentUser'
52
+ type = User
53
+ args = {}
54
+ args[:extras] = extras if extras
55
+ return_object type,method,args,true
56
+ end
57
+
58
+ # Delete a playlist.
59
+ def deletePlaylist(playlist)
60
+ method = 'deletePlaylist'
61
+ type = Boolean
62
+ args = {:playlist=>playlist}
63
+ return_object type,method,args,true
64
+ end
65
+
66
+ # Find a user either by email address or by their username. Exactly
67
+ # one of email or vanityName must be supplied.
68
+ def findUserByEmail(email)
69
+ method = 'findUser'
70
+ type = User
71
+ args = {:email=>email}
72
+ return_object type,method,args
73
+ end
74
+
75
+ # Find a user either by email address or by their username. Exactly
76
+ # one of email or vanityName must be supplied.
77
+ def findUserByVanityName(name)
78
+ method = 'findUser'
79
+ type = User
80
+ args = {:vanityName=>name}
81
+ return_object type,method,args
82
+ end
83
+
84
+ # Fetch one or more objects from Rdio.
85
+ def get(objs,type=nil)
86
+ if not objs.is_a? Array
87
+ objs = [objs]
88
+ end
89
+ method = 'get'
90
+ cls = type
91
+ args = {:keys=>keys(objs)}
92
+ json = call method,args
93
+ create_object type,json,true
94
+ end
95
+
96
+ # Get the activity events for a user, a user's friends, or
97
+ # everyone on Rdio.
98
+ def getActivityStream(user,scope,last_id=nil)
99
+ method = 'getActivityStream'
100
+ type = ActivityStream
101
+ args = {:user=>user,:scope=>scope}
102
+ args[:last_id] = last_id if last_id
103
+ return_object type,method,args
104
+ end
105
+
106
+ # Return the albums by (or featuring) an artist.
107
+ def getAlbumsForArtist(artist,featuring=nil,extras=nil,start=nil,count=nil)
108
+ method = 'getAlbumsForArtist'
109
+ type = Album
110
+ args = {:artist=>artist}
111
+ args[:featuring] = featuring if featuring
112
+ args[:extras] = extras if extras
113
+ args[:start] = start if start
114
+ args[:count] = count if count
115
+ return_object type,method,args
116
+ end
117
+
118
+ # Get the albums in the user's collection by a particular artist.
119
+ def getAlbumsForArtistInCollection(artist,user=nil)
120
+ method = 'getAlbumsForArtistInCollection'
121
+ type = Album
122
+ args = {:artist=>artist}
123
+ args[:user] = user if user
124
+ return_object type,method,args
125
+ end
126
+
127
+ # Get all of the albums in the user's collection.
128
+ def getAlbumsInCollection(user=nil,start=nil,count=nil,sort=nil,query=nil)
129
+ method = 'getAlbumsInCollection'
130
+ type = Album
131
+ args = {}
132
+ args[:user] = user if user
133
+ args[:start] = start if start
134
+ args[:count] = count if count
135
+ args[:sort] = sort if sort
136
+ args[:query] = query if query
137
+ return_object type,method,args
138
+ end
139
+
140
+ # Get all of the artist in a user's collection.
141
+ def getArtistsInCollection(user=nil,start=nil,count=nil,sort=nil,query=nil)
142
+ method = 'getArtistsInCollection'
143
+ type = Artist
144
+ args = {}
145
+ args[:user] = user if user
146
+ args[:start] = start if start
147
+ args[:count] = count if count
148
+ args[:sort] = sort if sort
149
+ args[:query] = query if query
150
+ return_object type,method,args
151
+ end
152
+
153
+ # Find the most popular artists or albums for a user, their friends
154
+ # or the whole site.
155
+ def getHeavyRotation(user=nil,type=nil,friends=nil,limit=nil)
156
+ method = 'getHeavyRotation'
157
+ cls = TODO
158
+ if type == 'artist'
159
+ cls = Artist
160
+ elsif type == 'album'
161
+ cls = Album
162
+ end
163
+ args = {}
164
+ args[:user] = user if user
165
+ args[:type] = type if type
166
+ args[:friends] = friends if friends
167
+ args[:limit] = limit if limit
168
+ return_object cls,method,args
169
+ end
170
+
171
+ # Return new albums released across a timeframe.
172
+ def getNewReleases(time=nil,start=nil,count=nil,extras=nil)
173
+ method = 'getNewReleases'
174
+ type = Album
175
+ args = {}
176
+ args[:time] = time if time
177
+ args[:start] = start if start
178
+ args[:count] = count if count
179
+ args[:extras] = extras if extras
180
+ return_object type,method,args
181
+ end
182
+
183
+ # Return the object that the supplied Rdio short-code is a
184
+ # representation of, or null if the short-code is invalid.
185
+ def getObjectFromShortCode(short_code,type=nil)
186
+ method = 'getObjectFromShortCode'
187
+ type = BaseObj if not type
188
+ args = {:short_code=>short_code}
189
+ return_object type,method,args,true
190
+ end
191
+
192
+ # Return the object that the supplied Rdio url is a representation
193
+ # of, or null if the url doesn't represent an object.
194
+ def getObjectFromUrl(url,type=nil)
195
+ method = 'getObjectFromUrl'
196
+ type = BaseObj if not type
197
+ args = {:url=>url}
198
+ return_object type,method,args,true
199
+ end
200
+
201
+ # Get an playback token. If you are using this for web playback you
202
+ # must supply a domain.
203
+ def getPlaybackToken(domain=nil)
204
+ method = 'getPlaybackToken'
205
+ type = String
206
+ args = {}
207
+ args[:domain] = domain if domain
208
+ return_object type,method,args,true
209
+ end
210
+
211
+ # Get the current user's playlists.
212
+ def getPlaylists(extras=nil)
213
+ method = 'getPlaylists'
214
+ type = Playlist
215
+ args = {}
216
+ args[:extras] = extras if extras
217
+ return_object type,method,args,true
218
+ end
219
+
220
+ # Return the site-wide most popular items for a given type.
221
+ def getTopCharts(type,start=nil,count=nil,extras=nil)
222
+ method = 'getTopCharts'
223
+ cls = TODO
224
+ case type
225
+ when 'Artist':
226
+ cls = Artist
227
+ when 'Album':
228
+ cls = Album
229
+ when 'Track':
230
+ cls = Track
231
+ when 'Playlist':
232
+ cls = Playlist
233
+ end
234
+ args = {:type=>type}
235
+ return_object cls,method,args
236
+ end
237
+
238
+ # Which tracks on the given album are in the user's collection.
239
+ def getTracksForAlbumInCollection(album,user=nil,extras=nil)
240
+ method = 'getTracksForAlbumInCollection'
241
+ type = Track
242
+ args = {:album=>album}
243
+ args[:user] = user if user
244
+ args[:extras] = extras if extras
245
+ return_object type,method,args
246
+ end
247
+
248
+ # Get all of the tracks by this artist.
249
+ def getTracksForArtist(artist,appears_on=nil,start=nil,count=nil,extras=nil)
250
+ method = 'getTracksForArtist'
251
+ type = Track
252
+ args = {:artist=>artist}
253
+ args[:appears_on] = appears_on if appears_on
254
+ args[:start] = start if start
255
+ args[:count] = count if count
256
+ args[:extras] = extras if extras
257
+ return_object type,method,args
258
+ end
259
+
260
+ # Which tracks from the given artist are in the user's collection.
261
+ def getTracksForArtistInCollection(artist,user=nil,extras=nil)
262
+ method = 'getTracksForArtistInCollection'
263
+ type = Track
264
+ args = {:artist=>artist}
265
+ args[:user] = user if user
266
+ args[:extras] = extras if extras
267
+ return_object type,method,args
268
+ end
269
+
270
+ # Get all of the tracks in the user's collection.
271
+ def getTracksInCollection(user=nil,start=nil,count=nil,sort=nil,query=nil)
272
+ method = 'getTracksInCollection'
273
+ type = Track
274
+ args = {}
275
+ args[:user] = user if user
276
+ args[:start] = start if start
277
+ args[:count] = count if count
278
+ args[:sort] = sort if sort
279
+ args[:query] = query if query
280
+ return_object type,method,args
281
+ end
282
+
283
+ # Remove a friend from the current user.
284
+ def removeFriend(user)
285
+ method = 'removeFriend'
286
+ type = Boolean
287
+ args = {:user=>user}
288
+ return_object type,method,args,true
289
+ end
290
+
291
+ # Remove tracks or playlists from the current user's collection.
292
+ def removeFromCollection(objs)
293
+ method = 'removeFromCollection'
294
+ type = Boolean
295
+ args = {:keys=>keys(objs)}
296
+ return_object type,method,args
297
+ end
298
+
299
+ # Remove an item from a playlist by its position in the playlist.
300
+ def removeFromPlaylist(playlist,index,count,tracks)
301
+ method = 'removeFromPlaylist'
302
+ type = TODO
303
+ args = {:playlist=>playlist,:index=>index,
304
+ :count=>count,:tracks=>keys(tracks)}
305
+ return_object type,method,args,truex
306
+ end
307
+
308
+ # Search for artists, albums, tracks, users or all kinds of objects.
309
+ def search(query,types=nil,never_or=nil,extras=nil,start=nil,count=nil)
310
+ method = 'search'
311
+ type = TODO
312
+ args = {:query=>query}
313
+ args[:types] = types if types
314
+ args[:never_or] = never_or if never_or
315
+ args[:extras] = extras if extras
316
+ args[:start] = start if start
317
+ args[:count] = count if count
318
+
319
+ json = call method,args
320
+ if Rdio::log_json
321
+ Rdio::log json
322
+ end
323
+ obj = unwrap_json json
324
+ return JSONObj.new obj
325
+ end
326
+
327
+ # Match the supplied prefix against artists, albums, tracks and
328
+ # people in the Rdio system. Return the first ten matches.
329
+ def searchSuggestions(query,extras)
330
+ method = 'searchSuggestions'
331
+ type = TODO
332
+ args = {:query=>query}
333
+ args[:extras] = extras if extras
334
+ return_object type,method,args
335
+ end
336
+
337
+ end
338
+
339
+ end