- Notifications
You must be signed in to change notification settings - Fork2
Napster/napster-ruby
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A Ruby interface to theNapster API.
Add this line to your application's Gemfile:
gem'napster'
And then execute:
$ bundle
Or install it yourself as:
$ gem install napster
Ruby version should be 2.0 or greater.
Aclient prepares you to make calls to Napster API.Here is an example code for setting up a client usingimplicit method.
require'napster'options={api_key:'API_KEY',api_secret:'API_SECRET',}client=Napster::Client.new(options)
You can still set up a client with just an access token. However, you willnot be able to refresh the access token and you won't be able to make anymetadata calls. Only authenticated member calls will be allowed with thisclient.
options={access_token:'ACCESS_TOKEN'}client=Napster::Client.new(options)
client_hash={api_key:'API_KEY',api_secret:'API_SECRET',username:'USERNAME',password:'PASSWORD'}# Just by instantiating with api_key, api_secret, username, and password# you can authenticate by password_grant.client=Napster::Client.new(client_hash)client.authentication.access_token# => returns access_tokenclient.authentication.refresh_tokenclient.authentication.expires_in
client_hash={api_key:'API_KEY',api_secret:'API_SECRET'}client=Napster::Client.new(client_hash)client.username='USERNAME'client.password='PASSWORD'client.connectclient.authentication.access_token# => returns access_tokenclient.authentication.refresh_tokenclient.authentication.expires_in
client_hash={api_key:'API_KEY',api_secret:'API_SECRET',redirect_uri:'REDIRECT_URI',auth_code:'AUTH_CODE'}client=Napster::Client.new(client_hash)client.connectclient.authentication.access_token# => returns access_tokenclient.authentication.refresh_tokenclient.authentication.expires_in
Napster API'saccess_token
expires in 24 hours after it is issued.You need to use therefresh_token
to generate a newaccess_token
whenit is expired.
It is not recommended to get a new access_token - refresh_tokenthrough authentication after the old access_token expires.
client.refresh# => returns new access_token by refreshing it
Metadata endpoints do not need the client to be authenticated.First, set up a client withapi_key
andapi_secret
.Then you can call metadata endpoints following this pattern.
# takes a form of client.[resources].[method]# albumsclient.albums.new_releases(limit:10)client.albums.staff_picks(limit:10)client.albums.top(limit:10)client.albums.find(artist_id)# => returns an albumclient.albums.find(artist_name)# => returns an albumclient.albums.find(artist_id).tracks(limit:10)# => returns an albumclient.albums.find(artist_name).tracks(limit:10)# => returns an album# artistsclient.artists.top(limit:5)client.artists.find(artist_id)# => returns an artistclient.artists.find(artist_name)# => returns an artistclient.artists.find(artist_id).albums(offset:5)client.artists.find(artist_id).new_albums(offset:5)client.artists.find(artist_id).tracks(limit:5)client.artists.find(artist_id).top_tracks(limit:5)# favoritesclient.favorites.members_who_favorited_albums('Alb.5153820')client.favorites.members_who_favorited_artists('Art.954')client.favorites.member_favorites_for('Tra.5156528')# membersclient.members.playlists_for('D877082A5CBC5AC7E040960A390313EF',limit:2)client.members.favorites_for('D877082A5CBC5AC7E040960A390313EF',limit:2)client.members.favorite_playlists_for('D877082A5CBC5AC7E040960A390313EF',limit:2)client.members.chart_for('D877082A5CBC5AC7E040960A390313EF',limit:2)client.members.find('D877082A5CBC5AC7E040960A390313EF')client.members.find('dduda')client.members.screenname_available?('dduda')client.members.find('dduda').playlists(limit:5)client.members.find('dduda').favorites(limit:5)client.members.find('dduda').favorite_playlists(limit:5)client.members.find('dduda').chart(limit:5)# playlistsclient.playlists.playlists_of_the_day(limit:3)client.playlists.featured(limit:3)client.playlists.find('pp.125821370')client.playlists.find('pp.125821370').tracks(limit:10)client.playlists.find('pp.125821370').tags# tagsclient.tags.allclient.tags.find('tag.156763217')# tracksclient.tracks.find('Tra.5156528')client.tracks.find_by_name('Marvins Room')
Authenticated member endpoints require the client to be authenticated.First, set up a client withapi_key
andapi_secret
.Authenticate the client by going through password grant method orOAuth2 method.Ensure that the client has access_token and refresh_token.Then you can call metadata endpoints following this pattern.
# takes a form of client.me.[resources].[method]# favoritesclient.me.favorites.get(limit:5)client.me.favorites.status(['Art.954','Alb.5153820','Tra.5156528'])client.me.favorites.add(['Art.954','Alb.5153820','Tra.5156528'])client.me.favorites.remove('Art.954')# followersclient.me.followers.members(limit:5)client.me.followers.by?(guids)# followingclient.me.following.members(limit:5)client.me.following.by?(guids)client.me.following.follow(guids)client.me.following.unfollow(guids)# libraryclient.me.library.artists(limit:10)client.me.library.artist_albums('Art.954',limit:10)client.me.library.artist_tracks('Art.954',limit:10)client.me.library.albums(limit:10)client.me.library.album_tracks('Alb.5153820',limit:10)client.me.library.tracks(limit:10)client.me.library.add_track(['Tra.5156528'])client.me.library.remove_track('Tra.5156528')client.me.library.last_updated_date# listening historyclient.me.listening_history(limit:10)# playlistsclient.me.playlists.all(limit:10)client.me.playlists.create({'name'=>'hiphop playlist'})client.me.playlists.find('mp.123123')client.me.playlists.update('mp.123123',{'name'=>'hiphop playlist 2'})client.me.playlists.delete('mp.123123')client.me.playlists.set_private('mp.123123','public')client.me.playlists.set_private('mp.123123','private')client.me.playlists.add_tracks('mp.123123',['Tra.5156528'])client.me.playlists.recommended_tracks('mp.123123')client.me.playlists.uploaded_images(id:'mp.123123',size:500)# id and size in pxclient.me.playlists.sourced_by('my_own_playlists',{artists:['art.123','art.234'],tags:['tag.123','tag.234'],guid:'xyz',sort:'alpha_asc',include_private:true,limit:10,offset:5})client.me.playlists.find('mp.123123').tracks(limit:10)client.me.playlists.find('mp.123123').tagsclient.me.playlists.find('mp.123123').uploaded_images(500)# size in px# profileclient.me.profile.getclient.me.profile.update({'me'=>{'bio'=>Faker::Lorem.word}})# tagsclient.me.tags.contents('favorite','',{})
client.artists.top(limit:5,offset:5)
request_hash={body:{name:'name of the playlist',tags:['tag.1','tag.2'],privacy:'public',tracks:['tra.1','tra.2']}}client.me.create_playlist(request_hash)
Napster gem providesResponseError
which wraps response errors from NapsterAPI. You can inspect error attributes as shown below.
beginclient.playlists.find('pp.125821370').tracks({})# problematic requestrescueException=>errorputserror.http_status# => 400putserror.response_body# => {"code":"BadRequestError","message":"limit query parameter is required"}putserror.faraday_response.inspect# => #<Faraday::Response:0x007fe9bc957150 @on_complete_callbacks=[], ...end
Napster gem uses RSpec and FactoryGirl.
Get the API key and API secret fromNapster Developers site.
Create a file called
config.yml
inspec
directory.Add the following with the correct API key and API secret
config_variables:API_KEY:'API_KEY'API_SECRET:'API_SECRET'USERNAME:'USERNAME'PASSWORD:'PASSWORD'REDIRECT_URI:'REDIRECT_URI'
$ bundle install
$ rspec
We useyard gem for documentation. In order to run the documenation server,run,$ bundle exec yard doc
.
Bug reports and pull requests are welcome on GitHub athttps://github.com/napster/napster-ruby.
The gem is available as open source under the terms of theMIT License.
Napster and Napster logo are registered and unregistered trademarks of Rhapsody International in the United States and/or other countries. All company, product and service names used in this website are for identification purposes only. All product names, logos, and brands are property of their respective owners.