From f220b9c3e1ca21891af68394b39803fe70a3a6e4 Mon Sep 17 00:00:00 2001 From: Jason Kim Date: Wed, 31 Aug 2016 13:32:37 -0700 Subject: [PATCH] https://github.com/Napster/napster-ruby/issues/67 ruby client readme test for self.prepare_refresh_token updated refresh --- README.md | 7 +++++++ lib/napster/client.rb | 27 +++++++++++++++++++++++++++ spec/client_spec_helper.rb | 7 +++++++ spec/lib/napster/client_spec.rb | 29 +++++++++++++++++++++++++++++ spec/lib/napster/me_spec.rb | 2 +- 5 files changed, 71 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5d89d9c..315462d 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,13 @@ it is expired. through authentication after the old access_token expires.* ```ruby +client_hash = { + api_key: 'API_KEY', + api_secret: 'API_SECRET', + refresh_token: 'REFRESH_TOKEN' +} + +client = Napster::Client.new(client_hash) client.refresh # => returns new access_token by refreshing it ``` diff --git a/lib/napster/client.rb b/lib/napster/client.rb index 099bca4..050f301 100644 --- a/lib/napster/client.rb +++ b/lib/napster/client.rb @@ -154,6 +154,17 @@ def me Napster::Me.new(self) end + # Refresh access token + def refresh + validate_refresh + body = post('/oauth/access_token', refresh_post_body, {}) + @access_token = body['access_token'] + @refresh_token = body['refresh_token'] + @expires_in = body['expires_in'] + + self + end + private # Helper method for .new, choose authentication method, and authenticate @@ -194,6 +205,12 @@ def validate_authenticate(auth_method) end end + def validate_refresh + raise Error, 'api_key is missing' unless @api_key + raise Error, 'api_secret is missing' unless @api_secret + raise Error, 'refresh_token is missing' unless @refresh_token + end + def auth_password_grant validate_auth_password_grant body = post('/oauth/token', @@ -215,6 +232,16 @@ def auth_password_grant_post_body } end + def refresh_post_body + { + client_id: @api_key, + client_secret: @api_secret, + response_type: 'code', + grant_type: 'refresh_token', + refresh_token: @refresh_token + } + end + def auth_password_grant_post_options basic_auth = Faraday::Request::BasicAuthentication.header(@api_key, @api_secret) diff --git a/spec/client_spec_helper.rb b/spec/client_spec_helper.rb index d6201c1..cc1eafd 100644 --- a/spec/client_spec_helper.rb +++ b/spec/client_spec_helper.rb @@ -57,4 +57,11 @@ def self.owns_playlists? playlists = client.me.playlists.all(params) !playlists.empty? end + + def self.prepare_refresh_token + includes = %w(username password) + client = ClientSpecHelper.get_client(includes) + client.authenticate(:password_grant) + client.refresh_token + end end diff --git a/spec/lib/napster/client_spec.rb b/spec/lib/napster/client_spec.rb index bac33d3..a5163e8 100644 --- a/spec/lib/napster/client_spec.rb +++ b/spec/lib/napster/client_spec.rb @@ -33,6 +33,20 @@ expect(client.access_token).to_not be_nil end + it 'with api_key, api_secret, refresh_token' do + refresh_token = ClientSpecHelper.prepare_refresh_token + options = { + api_key: config_variables['API_KEY'], + api_secret: config_variables['API_SECRET'], + refresh_token: refresh_token + } + client = Napster::Client.new(options) + + expect(client.api_key).to_not be_nil + expect(client.api_secret).to_not be_nil + expect(client.refresh_token).to_not be_nil + end + it 'without attributes' do options = { api_key: Faker::Lorem.characters(20), @@ -314,4 +328,19 @@ expect(client.me.class).to eql(Napster::Me) end + + it '#refresh' do + refresh_token = ClientSpecHelper.prepare_refresh_token + options = { + api_key: config_variables['API_KEY'], + api_secret: config_variables['API_SECRET'], + refresh_token: refresh_token + } + client = Napster::Client.new(options) + client.refresh + + expect(client.access_token).to_not be_nil + expect(client.refresh_token).to_not be_nil + expect(client.expires_in).to_not be_nil + end end diff --git a/spec/lib/napster/me_spec.rb b/spec/lib/napster/me_spec.rb index cc633dd..d2b6b06 100644 --- a/spec/lib/napster/me_spec.rb +++ b/spec/lib/napster/me_spec.rb @@ -233,7 +233,7 @@ } playlist = client.me.playlists.create(playlist_hash) playlist_ids << playlist.id - privacy_value = rand(0..1) == 0 ? true : false + privacy_value = rand(0..1).zero? ? true : false client.me.playlists.set_private(playlist.id, privacy_value) updated_playlist = client.me.playlists.find(playlist.id)