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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
/log/*
!/log/.keep
/tmp

# Ignore application configuration
/config/application.yml
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ gem 'sdoc', '~> 0.4.0', group: :doc
# Use Unicorn as the app server
gem 'unicorn'

gem 'omniauth-oauth2'

gem 'figaro'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

Expand Down
22 changes: 22 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ GEM
factory_girl_rails (4.7.0)
factory_girl (~> 4.7.0)
railties (>= 3.0.0)
faraday (0.10.1)
multipart-post (>= 1.2, < 3)
figaro (1.1.1)
thor (~> 0.14)
globalid (0.3.7)
activesupport (>= 4.1.0)
hashie (3.5.1)
i18n (0.8.0)
jbuilder (2.6.1)
activesupport (>= 3.0.0, < 5.1)
Expand All @@ -79,6 +84,7 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.6)
jwt (1.5.6)
kgio (2.11.0)
launchy (2.4.3)
addressable (~> 2.3)
Expand All @@ -92,8 +98,22 @@ GEM
mini_portile2 (2.1.0)
minitest (5.10.1)
multi_json (1.12.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
nokogiri (1.7.0.1)
mini_portile2 (~> 2.1.0)
oauth2 (1.3.0)
faraday (>= 0.8, < 0.11)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.4.1)
hashie (>= 1.2, < 4)
rack (>= 1.0, < 3)
omniauth-oauth2 (1.4.0)
oauth2 (~> 1.0)
omniauth (~> 1.2)
pg (0.19.0)
public_suffix (2.0.5)
rack (1.6.5)
Expand Down Expand Up @@ -194,9 +214,11 @@ DEPENDENCIES
coffee-rails (~> 4.1.0)
database_cleaner
factory_girl_rails
figaro
jbuilder (~> 2.0)
jquery-rails
launchy
omniauth-oauth2
pg (~> 0.15)
rails (= 4.2.6)
rails_12factor
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

helper_method :current_user

def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
14 changes: 14 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class SessionsController < ApplicationController
def create
@user = User.find_or_create_from_auth(request.env['omniauth.auth'])
if @user
session[:user_id] = @user.id
redirect_to playlists_path
end
end

def destroy
session.clear
redirect_to playlists_path
end
end
3 changes: 3 additions & 0 deletions app/models/playlist.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Playlist < ActiveRecord::Base
validates :name, presence: true, uniqueness: true

has_many :playlist_songs
has_many :songs, through: :playlist_songs

end
9 changes: 9 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class User < ActiveRecord::Base
def self.find_or_create_from_auth(auth)
User.find_or_create_by(
provider: auth["provider"],
uid: auth["info"]["id"],
name: auth["info"]["display_name"]
)
end
end
6 changes: 6 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
</head>
<body>

<% if current_user %>
<h3>Welcome, <%= current_user.name %></h3>
<%= link_to "Sign out", logout_path, method: :delete %>
<% else %>
<%= link_to "Sign in with Spotify", "/auth/spotify" %>
<% end %>
<%= yield %>

</body>
Expand Down
27 changes: 27 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'omniauth-oauth2'

module OmniAuth
module Strategies
class Spotify < OmniAuth::Strategies::OAuth2
option :name, 'spotify'

option :client_options, {
site: 'https://api.spotify.com/v1/',
authorize_url: 'https://accounts.spotify.com/authorize',
token_url: 'https://accounts.spotify.com/api/token'
}

def info
@raw_info ||= access_token.get('me').parsed
end

def callback_url
full_host + script_name + callback_path
end
end
end
end

Rails.application.config.middleware.use OmniAuth::Builder do
provider :spotify, ENV['SPOTIFY_KEY'], ENV['SPOTIFY_SECRET']
end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

resources :songs, only: [:show, :edit, :update]
resources :playlists

get '/auth/spotify/callback', to: 'sessions#create'

delete '/logout', to: 'sessions#destroy'
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20170209152757_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :provider
t.string :uid

t.timestamps null: false
end
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170209023045) do
ActiveRecord::Schema.define(version: 20170209152757) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -46,6 +46,14 @@
t.integer "artist_id"
end

create_table "users", force: :cascade do |t|
t.string "name"
t.string "provider"
t.string "uid"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_foreign_key "playlist_songs", "playlists"
add_foreign_key "playlist_songs", "songs"
end
123 changes: 123 additions & 0 deletions spec/controllers/artists_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
require 'rails_helper'

RSpec.describe ArtistsController, type: :controller do
describe "GET #index" do
it "assigns all artists as @artists and renders the index template" do
artist = create(:artist)

get(:index)

expect(assigns(:artists)).to eq([artist])
expect(response).to render_template("index")
end
end

describe "GET#show" do
it "assigns the requested artist as @artist and renders the show template" do
artist = create(:artist)
get(:show, {:id => artist.to_param})
expect(assigns(:artist)).to eq(artist)
expect(response).to render_template("show")
end
end

describe "GET#new" do
it "assigns the new artist as @artist and renders the show template" do
get(:new)
expect(assigns(:artist)).to be_a_new(Artist)
end
end

describe "GET#edit" do
it "assigns the requested artist as @artist" do
artist = create(:artist)
get(:edit, {:id => artist.to_param})
expect(assigns(:artist)).to eq(artist)
end
end

describe "POST#create" do
context "with valid params" do
it "creates a new artist" do
expect {
post :create, {:artist => attributes_for(:artist)}
}.to change(Artist, :count).by(1)
end

it "assigns a newly created artist as @artist" do
post :create, {:artist => attributes_for(:artist)}
expect(assigns(:artist)).to be_a(Artist)
expect(assigns(:artist)).to be_persisted
end

it "redirects to the created artist" do
post :create, {:artist => attributes_for(:artist)}
expect(response).to redirect_to(Artist.last)
end

context "with invalid params" do
it "assigns a newly created but unsaved artist as @artist" do
post :create, {:artist => attributes_for(:artist, name: nil)}
expect(assigns(:artist)).to be_a_new(Artist)
end

it "re-renders the 'new' template" do
post :create, {:artist => attributes_for(:artist, name: nil)}
expect(response).to render_template(:new)
end
end
end
end

describe "PUT#update" do
context "with valid params" do
it "updates the requested artist" do
artist = create(:artist)
put :update, {:id => artist.to_param, :artist => attributes_for(:artist, name: "New name")}
artist.reload
expect(artist.name).to eq("New name")
end

it "assigns the requested artist as @artist" do
artist = create(:artist)
put :update, {:id => artist.to_param, :artist => attributes_for(:artist, name: "New name")}
expect(assigns(:artist)).to eq(artist)
end

it "redirects to the artist" do
artist = create(:artist)
put :update, {:id => artist.to_param, :artist => attributes_for(:artist, name: "New name")}
expect(response).to redirect_to(artist)
end
end

context "with invalid params" do
it "assigns the artist as @artist" do
artist = create(:artist)
put :update, {:id => artist.to_param, :artist => attributes_for(:artist, name:nil)}
expect(assigns(:artist)).to eq(artist)
end

it "re-render the 'edit' template" do
artist = create(:artist)
put :update, {:id => artist.to_param, :artist => attributes_for(:artist, name: nil)}
expect(response).to render_template("edit")
end
end
end

describe "DELETE#destroy" do
it "destroys the requested artist" do
artist = create(:artist)
expect {
delete :destroy, {:id => artist.to_param}
}.to change(Artist, :count).by(-1)
end

it "redirects to the artists list" do
artist = create(:artist)
delete :destroy, {:id => artist.to_param}
expect(response).to redirect_to(artists_path)
end
end
end
Loading