Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

Commit

Permalink
Omniauth Strategy for Napster oAuth (#1)
Browse files Browse the repository at this point in the history
* Omniauth Strategy for Napster oAuth

This strategy can be verified by installing the gem and using the
[OmniAuth Test Harness](https://github.com/PracticallyGreen/omniauth-test-harness)

This gem has NOT been pushed to RubyGems yet.  We need to wait for the rename.

* fix circular reference in requires

* Bundle install will work on clean build

Change homepage to reference the new home at github.com/Napster

* Override only `token_url`

Updated rspec to reflect that we're not overriding defaults for `authorize_url`
  • Loading branch information
Richard Luck authored Jul 12, 2016
1 parent dd264a4 commit 057cb98
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.gem
.bundle
.config
Gemfile.lock
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source 'https://rubygems.org'
gemspec
102 changes: 100 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,100 @@
# omniauth-napster
Omniauth Strategy for Napster Music
<!--
[![Gem Version](https://badge.fury.io/rb/omniauth-napster.svg)](https://badge.fury.io/rb/omniauth-napster)
-->

# Napster Music OmniAuth Strategy

This gem provides the [OmniAuth](https://github.com/intridea/omniauth) strategy for the [Napster API](https://developer.napster.com).

## Installation

Add this line to your application's Gemfile:

```$ gem 'omniauth-napster'```

And then execute:

```$ bundle```

Or install it yourself as:

```$ gem install omniauth-napster```

## Usage

You'll need to register your app with Napster. Visit https://developer.napster.com/developer/apps/new to get started.

Using this gem is similar to other OmniAuth strategies. Add your app credentials to `config/initializers/omniauth.rb`:

```ruby
Rails.application.config.middleware.use OmniAuth::Builder do
provider :napster, ENV['NAPSTER_API_KEY'], ENV['NAPSTER_API_SECRET']
end
```

Or with Devise in `config/initializers/devise.rb`:

```ruby
config.omniauth :napster, ENV['NAPSTER_API_KEY'], ENV['NAPSTER_API_SECRET']
```

## Auth Hash Schema

Here's an example auth hash, available in `request.env['omniauth.auth']`:
```
{
"provider":"napster",
"uid":"295DC739BEDA4BB8E050960A380358BC",
"info": {
"uid":"295DC739BEDA4BB8E050960A380358BC",
"name":"Testy Testerson",
"username":"NAPIcat",
"image":"http://direct.rhapsody.com/imageserver/v2/external/aHR0cHM6Ly9yaGFwc29keS1uYXBpLnMzLmFtYXpvbmF3cy5jb20vbWVtYmVyLzI5NURDNzM5QkVEQTRCQjhFMDUwOTYwQTM4MDM1OEJDL2F2YXRhci9vcmlnaW5hbA%3D%3D/images/108x108.jpg"
},
"credentials": {
"token":"NTQ3NjI5NDktNWIyZC00MDVlLTg2MDMtZDhhMGQ2YTYzMTk3",
"refresh_token":"0b4bdad8-1c82-44d0-8cdb-dd8d2b6e6331",
"expires_at":1463103543,
"expires":true
},
"extra": {
"raw_info": {
"me": {
"id": "295DC739BEDA4BB8E050960A380358BC",
"realName": "Testy Testerson",
"screenName": "NAPIcat",
"bio": "The cat is back!",
"location":"Seattle, WA",
"visibility": "public",
"type": "member",
"href": "http://api.rhapsody.com/members/295DC739BEDA4BB8E050960A380358BC",
"favoriteAlbumsCount": 0,
"favoriteArtistsCount": 0,
"favoriteTracksCount": 0,
"playlistsTotalCount": 0,
"playlistsPublishedCount": 0,
"stationsCount": 0,
"radioCount": 0,
"followingCount": 2,
"followerCount": 10,
"avatar": "http://direct.rhapsody.com/imageserver/v2/external/aHR0cHM6Ly9yaGFwc29keS1uYXBpLnMzLmFtYXpvbmF3cy5jb20vbWVtYmVyLzI5NURDNzM5QkVEQTRCQjhFMDUwOTYwQTM4MDM1OEJDL2F2YXRhci9vcmlnaW5hbA%3D%3D/images/108x108.jpg",
"avatarId": "aHR0cHM6Ly9yaGFwc29keS1uYXBpLnMzLmFtYXpvbmF3cy5jb20vbWVtYmVyLzI5NURDNzM5QkVEQTRCQjhFMDUwOTYwQTM4MDM1OEJDL2F2YXRhci9vcmlnaW5hbA%3D%3D",
"defaultAvatar": "false",
"avatarVersion": 1465508896140
}
}
}
}
```

## More

This gem is brought to you by the NAPI (Napster API) development team. Please [reach out to us](https://groups.google.com/forum/#!forum/rhapsody-api) if you have any questions.

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "bundler/gem_tasks"
2 changes: 2 additions & 0 deletions lib/omniauth-napster.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "omniauth/napster"
require "omniauth/strategies/napster"
5 changes: 5 additions & 0 deletions lib/omniauth/napster.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module OmniAuth
module Napster
VERSION = "0.1.0"
end
end
36 changes: 36 additions & 0 deletions lib/omniauth/strategies/napster.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'omniauth-oauth2'

module OmniAuth
module Strategies
class Napster < OmniAuth::Strategies::OAuth2
include OmniAuth::Strategy
option :name, 'napster'

option :client_options, {
:site => 'https://api.napster.com',
:token_url => '/oauth/access_token'
}

uid{ raw_info['me']['id'] }

info do
{
:uid => raw_info['me']['id'],
:name => raw_info['me']['realName'],
:username => raw_info['me']['screenName'],
:image => raw_info['me']['avatar']
}
end

extra do
{
'raw_info' => raw_info
}
end

def raw_info
@raw_info ||= access_token.get('/me').parsed
end
end
end
end
21 changes: 21 additions & 0 deletions omniauth-napster.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require 'omniauth/napster'

Gem::Specification.new do |s|
s.name = "omniauth-napster"
s.version = OmniAuth::Napster::VERSION
s.authors = ["Richard Luck"]
s.email = ["rluck@rhapsody.com"]
s.description = %q{This gem provides the OmniAuth strategy for connecting to the Napster API.}
s.summary = %q{OmniAuth strategy for Napster Music Service}
s.homepage = "https://github.com/Napster/omniauth-napster.git"
s.required_ruby_version = '~> 2.0'
s.license = 'MIT'
s.files = `git ls-files`.split($/)
s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
s.test_files = s.files.grep(%r{^(test|spec|features)/})
s.require_paths = ["lib"]
s.add_runtime_dependency 'omniauth-oauth2', '~> 1.3'
s.add_development_dependency 'rspec', '~> 3.0'
end
6 changes: 6 additions & 0 deletions spec/omniauth/napster_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'spec_helper'
describe 'OmniAuth::Napster::VERSION' do
it "should return version number" do
expect(OmniAuth::Napster::VERSION).to eq('0.1.0')
end
end
21 changes: 21 additions & 0 deletions spec/omniauth/strategies/napster_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'
describe 'OmniAuth::Strategies::Napster' do
subject do
OmniAuth::Strategies::Napster.new({})
end

context "client options" do
it 'should have correct name' do
expect(subject.options.name).to eq("napster")
end

it 'should have correct site' do
expect(subject.options.client_options.site).to eq('https://api.napster.com')
end

it 'should have correct token url' do
expect(subject.options.client_options.token_url).to eq('/oauth/access_token')
end

end
end
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'bundler/setup'
Bundler.setup

require 'omniauth-napster'

RSpec.configure do |config|
# some (optional) config here
end

0 comments on commit 057cb98

Please sign in to comment.