Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
27 changes: 27 additions & 0 deletions lib/napster/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions spec/client_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions spec/lib/napster/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/lib/napster/me_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down