From 679d79024fc74bfcf00acf9207ec3da17c17b72f Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Wed, 18 May 2016 11:06:18 -0700 Subject: [PATCH 01/31] Created models and controllers. --- app/assets/javascripts/sessions.coffee | 3 ++ app/assets/javascripts/suggestions.coffee | 3 ++ app/assets/stylesheets/sessions.scss | 3 ++ app/assets/stylesheets/suggestions.scss | 3 ++ app/controllers/sessions_controller.rb | 6 ++++ app/controllers/suggestions_controller.rb | 2 ++ app/helpers/sessions_helper.rb | 2 ++ app/helpers/suggestions_helper.rb | 2 ++ app/models/food.rb | 2 ++ app/models/music.rb | 2 ++ app/views/layouts/application.html.erb | 1 + app/views/sessions/index.html.erb | 1 + app/views/suggestions/_suggestion.html.erb | 0 config/routes.rb | 2 +- db/migrate/20160518172252_create_foods.rb | 8 +++++ db/migrate/20160518174405_create_musics.rb | 8 +++++ db/schema.rb | 29 +++++++++++++++++++ test/controllers/sessions_controller_test.rb | 7 +++++ .../suggestions_controller_test.rb | 7 +++++ test/fixtures/foods.yml | 11 +++++++ test/fixtures/musics.yml | 11 +++++++ test/models/food_test.rb | 7 +++++ test/models/music_test.rb | 7 +++++ 23 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/sessions.coffee create mode 100644 app/assets/javascripts/suggestions.coffee create mode 100644 app/assets/stylesheets/sessions.scss create mode 100644 app/assets/stylesheets/suggestions.scss create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/controllers/suggestions_controller.rb create mode 100644 app/helpers/sessions_helper.rb create mode 100644 app/helpers/suggestions_helper.rb create mode 100644 app/models/food.rb create mode 100644 app/models/music.rb create mode 100644 app/views/sessions/index.html.erb create mode 100644 app/views/suggestions/_suggestion.html.erb create mode 100644 db/migrate/20160518172252_create_foods.rb create mode 100644 db/migrate/20160518174405_create_musics.rb create mode 100644 db/schema.rb create mode 100644 test/controllers/sessions_controller_test.rb create mode 100644 test/controllers/suggestions_controller_test.rb create mode 100644 test/fixtures/foods.yml create mode 100644 test/fixtures/musics.yml create mode 100644 test/models/food_test.rb create mode 100644 test/models/music_test.rb diff --git a/app/assets/javascripts/sessions.coffee b/app/assets/javascripts/sessions.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/sessions.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/suggestions.coffee b/app/assets/javascripts/suggestions.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/suggestions.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/sessions.scss b/app/assets/stylesheets/sessions.scss new file mode 100644 index 0000000..ccb1ed2 --- /dev/null +++ b/app/assets/stylesheets/sessions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Sessions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/suggestions.scss b/app/assets/stylesheets/suggestions.scss new file mode 100644 index 0000000..2ac92ee --- /dev/null +++ b/app/assets/stylesheets/suggestions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Suggestions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..92468f9 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,6 @@ +class SessionsController < ApplicationController + + def index + end + +end diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb new file mode 100644 index 0000000..d5b0599 --- /dev/null +++ b/app/controllers/suggestions_controller.rb @@ -0,0 +1,2 @@ +class SuggestionsController < ApplicationController +end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..309f8b2 --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/app/helpers/suggestions_helper.rb b/app/helpers/suggestions_helper.rb new file mode 100644 index 0000000..0e358dd --- /dev/null +++ b/app/helpers/suggestions_helper.rb @@ -0,0 +1,2 @@ +module SuggestionsHelper +end diff --git a/app/models/food.rb b/app/models/food.rb new file mode 100644 index 0000000..3f58467 --- /dev/null +++ b/app/models/food.rb @@ -0,0 +1,2 @@ +class Food < ActiveRecord::Base +end diff --git a/app/models/music.rb b/app/models/music.rb new file mode 100644 index 0000000..427c9c2 --- /dev/null +++ b/app/models/music.rb @@ -0,0 +1,2 @@ +class Music < ActiveRecord::Base +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 509d1a2..eb75e03 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -6,6 +6,7 @@ <%= javascript_include_tag 'application' %> <%= csrf_meta_tags %> +

tunes&takeout

<%= yield %> diff --git a/app/views/sessions/index.html.erb b/app/views/sessions/index.html.erb new file mode 100644 index 0000000..165528c --- /dev/null +++ b/app/views/sessions/index.html.erb @@ -0,0 +1 @@ +

index view

diff --git a/app/views/suggestions/_suggestion.html.erb b/app/views/suggestions/_suggestion.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/config/routes.rb b/config/routes.rb index 3f66539..56d05da 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ # See how all your routes lay out with "rake routes". # You can have the root of your site routed with "root" - # root 'welcome#index' + root 'sessions#index' # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/db/migrate/20160518172252_create_foods.rb b/db/migrate/20160518172252_create_foods.rb new file mode 100644 index 0000000..5d62fcd --- /dev/null +++ b/db/migrate/20160518172252_create_foods.rb @@ -0,0 +1,8 @@ +class CreateFoods < ActiveRecord::Migration + def change + create_table :foods do |t| + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160518174405_create_musics.rb b/db/migrate/20160518174405_create_musics.rb new file mode 100644 index 0000000..2cc03f3 --- /dev/null +++ b/db/migrate/20160518174405_create_musics.rb @@ -0,0 +1,8 @@ +class CreateMusics < ActiveRecord::Migration + def change + create_table :musics do |t| + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..6a94cc0 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,29 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20160518174405) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "foods", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "musics", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb new file mode 100644 index 0000000..d30ebc3 --- /dev/null +++ b/test/controllers/sessions_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SessionsControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/suggestions_controller_test.rb b/test/controllers/suggestions_controller_test.rb new file mode 100644 index 0000000..f4587a5 --- /dev/null +++ b/test/controllers/suggestions_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SuggestionsControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/foods.yml b/test/fixtures/foods.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/foods.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/fixtures/musics.yml b/test/fixtures/musics.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/musics.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/food_test.rb b/test/models/food_test.rb new file mode 100644 index 0000000..8ad7dff --- /dev/null +++ b/test/models/food_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class FoodTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/music_test.rb b/test/models/music_test.rb new file mode 100644 index 0000000..c5c6737 --- /dev/null +++ b/test/models/music_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class MusicTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 9e57fa5e3e89a8272d3771c53cc50bc531cf936c Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Wed, 18 May 2016 15:05:31 -0700 Subject: [PATCH 02/31] Added Spotify API configuration. --- .gitignore | 2 + Gemfile | 15 +++-- Gemfile.lock | 80 +++++++++++++++++++++++++- app/controllers/sessions_controller.rb | 2 - config/initializers/omniauth.rb | 5 ++ config/routes.rb | 5 ++ 6 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 config/initializers/omniauth.rb diff --git a/.gitignore b/.gitignore index 03f3ce7..ef9fa8e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ # Ignore bundler config. /.bundle +/.env + # Ignore all logfiles and tempfiles. /log/* !/log/.keep diff --git a/Gemfile b/Gemfile index 288bb87..517c0b4 100644 --- a/Gemfile +++ b/Gemfile @@ -20,9 +20,11 @@ gem 'jquery-rails' gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc - -# Use ActiveModel has_secure_password -# gem 'bcrypt', '~> 3.1.7' +# Spotify API wrapper +gem 'rspotify' +# Yelp API wrapper +gem 'yelp', require: 'yelp' +gem 'omniauth' # Use Unicorn as the app server # gem 'unicorn' @@ -33,6 +35,12 @@ gem 'sdoc', '~> 0.4.0', group: :doc group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' + gem 'minitest-vcr' + gem 'minitest-reporters' + gem 'pry-rails' + gem 'webmock' + gem 'dotenv-rails' + end group :development do @@ -42,4 +50,3 @@ group :development do # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end - diff --git a/Gemfile.lock b/Gemfile.lock index 6ff0f39..c08f0f7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,11 +36,14 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + addressable (2.4.0) + ansi (1.5.0) arel (6.0.3) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) builder (3.2.2) byebug (8.2.5) + coderay (1.1.1) coffee-rails (4.1.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.1.x) @@ -49,11 +52,23 @@ GEM execjs coffee-script-source (1.10.0) concurrent-ruby (1.0.2) + crack (0.4.3) + safe_yaml (~> 1.0.0) debug_inspector (0.0.2) + dotenv (2.1.1) + dotenv-rails (2.1.1) + dotenv (= 2.1.1) + railties (>= 4.0, < 5.1) erubis (2.7.0) execjs (2.6.0) + faraday (0.9.2) + multipart-post (>= 1.2, < 3) + faraday_middleware (0.10.0) + faraday (>= 0.7.4, < 0.10) globalid (0.3.6) activesupport (>= 4.1.0) + hashdiff (0.3.0) + hashie (3.4.4) i18n (0.7.0) jbuilder (2.4.1) activesupport (>= 3.0.0, < 5.1) @@ -63,19 +78,53 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.3) + jwt (1.5.1) loofah (2.0.3) nokogiri (>= 1.5.9) mail (2.6.4) mime-types (>= 1.16, < 4) + method_source (0.8.2) mime-types (3.0) mime-types-data (~> 3.2015) mime-types-data (3.2016.0221) mini_portile2 (2.0.0) + minispec-metadata (2.0.0) + minitest minitest (5.8.4) + minitest-reporters (1.1.9) + ansi + builder + minitest (>= 5.0) + ruby-progressbar + minitest-vcr (1.4.0) + minispec-metadata (~> 2.0) + minitest (>= 4.7.5) + vcr (>= 2.9) multi_json (1.12.0) + multi_xml (0.5.5) + multipart-post (2.0.0) + netrc (0.7.9) nokogiri (1.6.7.2) mini_portile2 (~> 2.0.0.rc2) + oauth2 (1.1.0) + faraday (>= 0.8, < 0.10) + jwt (~> 1.0, < 1.5.2) + multi_json (~> 1.3) + multi_xml (~> 0.5) + rack (>= 1.2, < 3) + omniauth (1.3.1) + hashie (>= 1.2, < 4) + rack (>= 1.0, < 3) + omniauth-oauth2 (1.4.0) + oauth2 (~> 1.0) + omniauth (~> 1.2) pg (0.18.4) + pry (0.10.3) + coderay (~> 1.1.0) + method_source (~> 0.8.1) + slop (~> 3.4) + pry-rails (0.3.4) + pry (>= 0.9.10) rack (1.6.4) rack-test (0.6.3) rack (>= 1.0) @@ -106,6 +155,13 @@ GEM rake (11.1.2) rdoc (4.2.2) json (~> 1.4) + rest_client (1.8.3) + netrc (~> 0.7.7) + rspotify (1.10.0) + omniauth-oauth2 (~> 1.1) + rest_client (~> 1.8) + ruby-progressbar (1.8.1) + safe_yaml (1.0.4) sass (3.4.22) sass-rails (5.0.4) railties (>= 4.0.0, < 5.0) @@ -116,6 +172,8 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + simple_oauth (0.3.1) + slop (3.6.0) spring (1.7.1) sprockets (3.6.0) concurrent-ruby (~> 1.0) @@ -131,11 +189,20 @@ GEM thread_safe (~> 0.1) uglifier (3.0.0) execjs (>= 0.3.0, < 3) + vcr (3.0.1) web-console (2.3.0) activemodel (>= 4.0) binding_of_caller (>= 0.7.2) railties (>= 4.0) sprockets-rails (>= 2.0, < 4.0) + webmock (2.0.2) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff + yelp (2.1.2) + faraday (~> 0.8, >= 0.8.0) + faraday_middleware (~> 0.8, >= 0.8.0) + simple_oauth (~> 0.3.1) PLATFORMS ruby @@ -143,15 +210,26 @@ PLATFORMS DEPENDENCIES byebug coffee-rails (~> 4.1.0) + dotenv-rails jbuilder (~> 2.0) jquery-rails + minitest-reporters + minitest-vcr + omniauth pg (~> 0.15) + pry-rails rails (= 4.2.6) + rspotify sass-rails (~> 5.0) sdoc (~> 0.4.0) spring uglifier (>= 1.3.0) web-console (~> 2.0) + webmock + yelp + +RUBY VERSION + ruby 2.3.1p112 BUNDLED WITH - 1.12.3 + 1.12.4 diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 92468f9..d10ca97 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,6 +1,4 @@ class SessionsController < ApplicationController - def index - end end diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 0000000..bbdd85e --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,5 @@ +ENV["SPOTIFY_CLIENT_ID"] +ENV["SPOTIFY_SECRET_ID"] + +ENV["YELP_CLIENT_ID"] +ENV["YELP_SECRET_ID"] diff --git a/config/routes.rb b/config/routes.rb index 56d05da..72dca51 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,11 @@ # You can have the root of your site routed with "root" root 'sessions#index' + +# Do I need to add anything else to the below OmniAuth routes?? + get '/auth/:provider' + get '/auth/:provider/callback' + # Example of regular route: # get 'products/:id' => 'catalog#view' From 5e083615f5b36a9b8cde75548fdfa59bf1507639 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Wed, 18 May 2016 15:37:13 -0700 Subject: [PATCH 03/31] Merge commit. --- app/views/layouts/application.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index eb75e03..20b8bf7 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -7,6 +7,7 @@ <%= csrf_meta_tags %>

tunes&takeout

+<%= link_to "Home", root_path %> <%= yield %> From dbefe0fd474ca27e68a0ae8deb7804ba17ba9889 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Thu, 19 May 2016 12:33:06 -0700 Subject: [PATCH 04/31] Completed Spotify redirect and callback. --- Gemfile | 1 + Gemfile.lock | 5 +++-- app/controllers/sessions_controller.rb | 18 ++++++++++++++++++ app/models/user.rb | 7 +++++++ app/views/sessions/index.html.erb | 2 ++ config/initializers/omniauth.rb | 8 +++----- config/routes.rb | 22 +++++----------------- db/migrate/20160519180532_create_users.rb | 8 ++++++++ db/schema.rb | 7 ++++++- test/fixtures/users.yml | 11 +++++++++++ test/models/user_test.rb | 7 +++++++ 11 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 app/models/user.rb create mode 100644 db/migrate/20160519180532_create_users.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb diff --git a/Gemfile b/Gemfile index 517c0b4..761374b 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ ruby '2.3.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.6' +gem 'omniauth-oauth2', '1.3.1' # Use postgresql as the database for Active Record gem 'pg', '~> 0.15' # Use SCSS for stylesheets diff --git a/Gemfile.lock b/Gemfile.lock index c08f0f7..d0e24a0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -100,7 +100,7 @@ GEM minispec-metadata (~> 2.0) minitest (>= 4.7.5) vcr (>= 2.9) - multi_json (1.12.0) + multi_json (1.12.1) multi_xml (0.5.5) multipart-post (2.0.0) netrc (0.7.9) @@ -115,7 +115,7 @@ GEM omniauth (1.3.1) hashie (>= 1.2, < 4) rack (>= 1.0, < 3) - omniauth-oauth2 (1.4.0) + omniauth-oauth2 (1.3.1) oauth2 (~> 1.0) omniauth (~> 1.2) pg (0.18.4) @@ -216,6 +216,7 @@ DEPENDENCIES minitest-reporters minitest-vcr omniauth + omniauth-oauth2 (= 1.3.1) pg (~> 0.15) pry-rails rails (= 4.2.6) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index d10ca97..eaffa7d 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,4 +1,22 @@ class SessionsController < ApplicationController + def create + auth_hash = request.env['omniauth.auth'] + raise + end + # def create + # auth_hash = request.env['omniauth.auth'] + # if auth_hash["uid"] + # @user = User.find_or_create_from_omniauth(auth_hash) + # if @user + # session[:user_id] = @user.id + # redirect_to root_path + # else + # redirect_to root_path, notice: "Failed to save the user" + # end + # else + # redirect_to root_path, notice: "Failed to authenticate" + # end + # end end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..c1ff4a7 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,7 @@ +class User < ActiveRecord::Base + validates :email, :name, :uid, :provider, presence: true + + def self.find_or_create_from_omniauth(auth_hash) + # Find or create a user + end +end diff --git a/app/views/sessions/index.html.erb b/app/views/sessions/index.html.erb index 165528c..9dbe7aa 100644 --- a/app/views/sessions/index.html.erb +++ b/app/views/sessions/index.html.erb @@ -1 +1,3 @@

index view

+ +<%= link_to "Log In With Spotify", "/auth/spotify" %> diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index bbdd85e..fec7c85 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -1,5 +1,3 @@ -ENV["SPOTIFY_CLIENT_ID"] -ENV["SPOTIFY_SECRET_ID"] - -ENV["YELP_CLIENT_ID"] -ENV["YELP_SECRET_ID"] +Rails.application.config.middleware.use OmniAuth::Builder do + provider :spotify, ENV["SPOTIFY_CLIENT_ID"], ENV["SPOTIFY_SECRET_ID"] +end diff --git a/config/routes.rb b/config/routes.rb index 72dca51..dfd36d1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,13 +2,14 @@ # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". - # You can have the root of your site routed with "root" root 'sessions#index' -# Do I need to add anything else to the below OmniAuth routes?? - get '/auth/:provider' - get '/auth/:provider/callback' + get '/auth/spotify', :to => 'sessions#create' + get '/auth/spotify/callback', :to => 'sessions#index' + get '/logout', :to => 'sessions#destroy' + resources :food, :music + # Confirm these are the correct resources. What is this line even doing??????? # Example of regular route: # get 'products/:id' => 'catalog#view' @@ -45,17 +46,4 @@ # end # end - # Example resource route with concerns: - # concern :toggleable do - # post 'toggle' - # end - # resources :posts, concerns: :toggleable - # resources :photos, concerns: :toggleable - - # Example resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end end diff --git a/db/migrate/20160519180532_create_users.rb b/db/migrate/20160519180532_create_users.rb new file mode 100644 index 0000000..e6f0ee5 --- /dev/null +++ b/db/migrate/20160519180532_create_users.rb @@ -0,0 +1,8 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users do |t| + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 6a94cc0..505f7e2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160518174405) do +ActiveRecord::Schema.define(version: 20160519180532) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -26,4 +26,9 @@ t.datetime "updated_at", null: false end + create_table "users", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..82f61e0 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end From 5590266abb6a16dc7008c572d88874226c6be440 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Thu, 19 May 2016 13:02:10 -0700 Subject: [PATCH 05/31] Fixed bug that prevented Spotify UID from showing up. --- Gemfile | 3 ++- Gemfile.lock | 3 +++ app/controllers/sessions_controller.rb | 31 +++++++++++++------------- config/routes.rb | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Gemfile b/Gemfile index 761374b..0b294df 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,7 @@ ruby '2.3.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.6' -gem 'omniauth-oauth2', '1.3.1' +gem 'omniauth-oauth2', '1.3.1' #reverted back to an older version to resolve route issue # Use postgresql as the database for Active Record gem 'pg', '~> 0.15' # Use SCSS for stylesheets @@ -26,6 +26,7 @@ gem 'rspotify' # Yelp API wrapper gem 'yelp', require: 'yelp' gem 'omniauth' +gem 'omniauth-spotify' # Use Unicorn as the app server # gem 'unicorn' diff --git a/Gemfile.lock b/Gemfile.lock index d0e24a0..cc57425 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,6 +118,8 @@ GEM omniauth-oauth2 (1.3.1) oauth2 (~> 1.0) omniauth (~> 1.2) + omniauth-spotify (0.0.9) + omniauth-oauth2 (~> 1.1) pg (0.18.4) pry (0.10.3) coderay (~> 1.1.0) @@ -217,6 +219,7 @@ DEPENDENCIES minitest-vcr omniauth omniauth-oauth2 (= 1.3.1) + omniauth-spotify pg (~> 0.15) pry-rails rails (= 4.2.6) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index eaffa7d..27716f1 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,22 +1,23 @@ class SessionsController < ApplicationController + # def create + # auth_hash = request.env['omniauth.auth'] + # raise + # end + def create auth_hash = request.env['omniauth.auth'] + if auth_hash["uid"] + @user = User.find_or_create_from_omniauth(auth_hash) + if @user + session[:user_id] = @user.id + redirect_to root_path + else + redirect_to root_path, notice: "Failed to save the user" + end + else + redirect_to root_path, notice: "Failed to authenticate" + end raise end - - # def create - # auth_hash = request.env['omniauth.auth'] - # if auth_hash["uid"] - # @user = User.find_or_create_from_omniauth(auth_hash) - # if @user - # session[:user_id] = @user.id - # redirect_to root_path - # else - # redirect_to root_path, notice: "Failed to save the user" - # end - # else - # redirect_to root_path, notice: "Failed to authenticate" - # end - # end end diff --git a/config/routes.rb b/config/routes.rb index dfd36d1..32c5963 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,7 @@ get '/auth/spotify', :to => 'sessions#create' - get '/auth/spotify/callback', :to => 'sessions#index' + get '/auth/spotify/callback', :to => 'sessions#create' get '/logout', :to => 'sessions#destroy' resources :food, :music # Confirm these are the correct resources. What is this line even doing??????? From b224843a98f8a65c3d0cb823ef5a487e2dd2bd6c Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Thu, 19 May 2016 13:54:46 -0700 Subject: [PATCH 06/31] Added columns for required User information. --- app/controllers/sessions_controller.rb | 5 +++++ app/models/user.rb | 6 +++++- db/migrate/20160519201121_add_columns_to_users.rb | 8 ++++++++ db/schema.rb | 6 +++++- 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20160519201121_add_columns_to_users.rb diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 27716f1..4f25d42 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -20,4 +20,9 @@ def create end raise end + + def destroy + session.delete :user_id + redirect_to root_path + end end diff --git a/app/models/user.rb b/app/models/user.rb index c1ff4a7..5f4f6bf 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +2,10 @@ class User < ActiveRecord::Base validates :email, :name, :uid, :provider, presence: true def self.find_or_create_from_omniauth(auth_hash) - # Find or create a user + unless auth = find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"]) + user = User.create :name => auth_hash["user_info"]["name"], :email => auth_hash["user_info"]["email"] + auth = create :user => user, :provider => auth_hash["provider"], :uid => auth_hash["uid"] + end + auth end end diff --git a/db/migrate/20160519201121_add_columns_to_users.rb b/db/migrate/20160519201121_add_columns_to_users.rb new file mode 100644 index 0000000..da27de9 --- /dev/null +++ b/db/migrate/20160519201121_add_columns_to_users.rb @@ -0,0 +1,8 @@ +class AddColumnsToUsers < ActiveRecord::Migration + def change + add_column(:users, :uid, :text, presence: true) + add_column(:users, :name, :text, presence: true) + add_column(:users, :email, :text, presence: true) + add_column(:users, :provider, :text, presence: true) + end +end diff --git a/db/schema.rb b/db/schema.rb index 505f7e2..0971bb8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160519180532) do +ActiveRecord::Schema.define(version: 20160519201121) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -29,6 +29,10 @@ create_table "users", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.text "uid" + t.text "name" + t.text "email" + t.text "provider" end end From 22ed85bb54a88af5e852472bcaea94a0b3fcca0b Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Thu, 19 May 2016 15:59:39 -0700 Subject: [PATCH 07/31] Added login/logout capability. --- app/controllers/application_controller.rb | 5 +++++ app/controllers/sessions_controller.rb | 6 ------ app/models/user.rb | 22 ++++++++++++++++++---- app/views/layouts/application.html.erb | 5 +++-- app/views/sessions/index.html.erb | 13 ++++++++++--- config/initializers/omniauth.rb | 2 +- 6 files changed, 37 insertions(+), 16 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..3fb0a15 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,9 @@ 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 + + def current_user + @current_user ||= User.find_by(id: session[:user_id]) + end + helper_method :current_user end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 4f25d42..9da972b 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,10 +1,5 @@ class SessionsController < ApplicationController - # def create - # auth_hash = request.env['omniauth.auth'] - # raise - # end - def create auth_hash = request.env['omniauth.auth'] if auth_hash["uid"] @@ -18,7 +13,6 @@ def create else redirect_to root_path, notice: "Failed to authenticate" end - raise end def destroy diff --git a/app/models/user.rb b/app/models/user.rb index 5f4f6bf..c8fae25 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,10 +2,24 @@ class User < ActiveRecord::Base validates :email, :name, :uid, :provider, presence: true def self.find_or_create_from_omniauth(auth_hash) - unless auth = find_by_provider_and_uid(auth_hash["provider"], auth_hash["uid"]) - user = User.create :name => auth_hash["user_info"]["name"], :email => auth_hash["user_info"]["email"] - auth = create :user => user, :provider => auth_hash["provider"], :uid => auth_hash["uid"] + # Find or create a user + user = self.find_by(uid: auth_hash["uid"], provider: auth_hash["provider"]) + if user + return user + else + # no user found, do something here + user = User.new + user.uid = auth_hash["uid"] + user.provider = auth_hash["provider"] + user.name = auth_hash["info"]["name"] + user.email = auth_hash["info"]["email"] + + if user.save + return user + else + return nil + end end - auth end + end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 20b8bf7..8523860 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -6,8 +6,9 @@ <%= javascript_include_tag 'application' %> <%= csrf_meta_tags %> -

tunes&takeout

-<%= link_to "Home", root_path %> + +

<%= link_to "tunes&takeout", root_path %>

+ <%= yield %> diff --git a/app/views/sessions/index.html.erb b/app/views/sessions/index.html.erb index 9dbe7aa..d212d82 100644 --- a/app/views/sessions/index.html.erb +++ b/app/views/sessions/index.html.erb @@ -1,3 +1,10 @@ -

index view

- -<%= link_to "Log In With Spotify", "/auth/spotify" %> +<% if current_user %> + +<% else %> + +<% end %> diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index fec7c85..11ac600 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -1,3 +1,3 @@ Rails.application.config.middleware.use OmniAuth::Builder do - provider :spotify, ENV["SPOTIFY_CLIENT_ID"], ENV["SPOTIFY_SECRET_ID"] + provider :spotify, ENV["SPOTIFY_CLIENT_ID"], ENV["SPOTIFY_SECRET_ID"], scope: 'user-read-email' end From f4b31ec188484cef539e3d54e89cc7b55f25d82f Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Fri, 20 May 2016 12:39:36 -0700 Subject: [PATCH 08/31] Added TunesTakeoutWrapper to lib directory. --- app/controllers/application_controller.rb | 3 +- app/controllers/sessions_controller.rb | 1 + app/views/layouts/application.html.erb | 2 +- app/views/sessions/index.html.erb | 1 + config/routes.rb | 37 +---------------------- lib/TunesTakeoutWrapper.rb | 9 ++++++ 6 files changed, 15 insertions(+), 38 deletions(-) create mode 100644 lib/TunesTakeoutWrapper.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3fb0a15..f5e4b4a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,9 +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_by(id: session[:user_id]) end - helper_method :current_user + end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 9da972b..02ec2a3 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -19,4 +19,5 @@ def destroy session.delete :user_id redirect_to root_path end + end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 8523860..5bbee87 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,7 +1,7 @@ - TunesTakeout + TunesTakeoutSpotify Icon <%= stylesheet_link_tag 'application', media: 'all' %> <%= javascript_include_tag 'application' %> <%= csrf_meta_tags %> diff --git a/app/views/sessions/index.html.erb b/app/views/sessions/index.html.erb index d212d82..b1ead74 100644 --- a/app/views/sessions/index.html.erb +++ b/app/views/sessions/index.html.erb @@ -7,4 +7,5 @@ + <% end %> diff --git a/config/routes.rb b/config/routes.rb index 32c5963..1178f1f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ # See how all your routes lay out with "rake routes". root 'sessions#index' + # get '/top_twenty' => 'sessions#top_twenty' get '/auth/spotify', :to => 'sessions#create' @@ -10,40 +11,4 @@ get '/logout', :to => 'sessions#destroy' resources :food, :music # Confirm these are the correct resources. What is this line even doing??????? - - # Example of regular route: - # get 'products/:id' => 'catalog#view' - - # Example of named route that can be invoked with purchase_url(id: product.id) - # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase - - # Example resource route (maps HTTP verbs to controller actions automatically): - # resources :products - - # Example resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end - # - # collection do - # get 'sold' - # end - # end - - # Example resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - - # Example resource route with more complex sub-resources: - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', on: :collection - # end - # end - end diff --git a/lib/TunesTakeoutWrapper.rb b/lib/TunesTakeoutWrapper.rb new file mode 100644 index 0000000..741f0f4 --- /dev/null +++ b/lib/TunesTakeoutWrapper.rb @@ -0,0 +1,9 @@ +require 'httparty' + +class TunesTakeoutWrapper + BASE_URL = "https://tunes-takeout-api.herokuapp.com/" + + def self.search(keyword) + @data = HTTParty.get(BASE_URL + "v1/suggestions/search?query=" + keyword).parsed_response + end +end From fc0936431f6ebaeaa24645a473344609c2e0f6a3 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Fri, 20 May 2016 12:41:21 -0700 Subject: [PATCH 09/31] Added show method to suggestions controller. --- app/controllers/suggestions_controller.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index d5b0599..4833fc6 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -1,2 +1,9 @@ class SuggestionsController < ApplicationController + + def index + end + + def show + @results = TunesTakeoutWrapper.search(params[:keyword]) + end end From 6a51ffa5cbfd339812da93ce8e0fce20dbdacd88 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Fri, 20 May 2016 13:57:45 -0700 Subject: [PATCH 10/31] Created TunesTakeoutWrapper, updated suggestions controller, and created top_twenty index view file. --- Gemfile | 2 +- Gemfile.lock | 4 ++++ app/controllers/suggestions_controller.rb | 3 +++ app/views/sessions/index.html.erb | 1 + app/views/suggestions/index.html.erb | 7 +++++++ config/routes.rb | 2 ++ 6 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 app/views/suggestions/index.html.erb diff --git a/Gemfile b/Gemfile index 0b294df..30e58d3 100644 --- a/Gemfile +++ b/Gemfile @@ -27,7 +27,7 @@ gem 'rspotify' gem 'yelp', require: 'yelp' gem 'omniauth' gem 'omniauth-spotify' - +gem 'httparty' # Use Unicorn as the app server # gem 'unicorn' diff --git a/Gemfile.lock b/Gemfile.lock index cc57425..60656fe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -69,6 +69,9 @@ GEM activesupport (>= 4.1.0) hashdiff (0.3.0) hashie (3.4.4) + httparty (0.13.7) + json (~> 1.8) + multi_xml (>= 0.5.2) i18n (0.7.0) jbuilder (2.4.1) activesupport (>= 3.0.0, < 5.1) @@ -213,6 +216,7 @@ DEPENDENCIES byebug coffee-rails (~> 4.1.0) dotenv-rails + httparty jbuilder (~> 2.0) jquery-rails minitest-reporters diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 4833fc6..1f6cc3e 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -1,3 +1,5 @@ +require_relative "../../lib/TunesTakeoutWrapper" + class SuggestionsController < ApplicationController def index @@ -6,4 +8,5 @@ def index def show @results = TunesTakeoutWrapper.search(params[:keyword]) end + end diff --git a/app/views/sessions/index.html.erb b/app/views/sessions/index.html.erb index b1ead74..270b375 100644 --- a/app/views/sessions/index.html.erb +++ b/app/views/sessions/index.html.erb @@ -6,6 +6,7 @@ <% else %> <% end %> diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb new file mode 100644 index 0000000..8364809 --- /dev/null +++ b/app/views/suggestions/index.html.erb @@ -0,0 +1,7 @@ +<%= form_tag top_twenty_results_path do %> + <%= label_tag(:q, "Search for:") %> + <%= text_field_tag(:q) %> + <%= submit_tag("Search") %> +<% end %> + +<%= @data %> #returns nothing diff --git a/config/routes.rb b/config/routes.rb index 1178f1f..92b63b1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,5 +10,7 @@ get '/auth/spotify/callback', :to => 'sessions#create' get '/logout', :to => 'sessions#destroy' resources :food, :music + get '/top_twenty', :to => 'suggestions#index' + post '/top_twenty/show', :to => 'suggestions#show', as: 'top_twenty_results' # Confirm these are the correct resources. What is this line even doing??????? end From b84aeb4ccf65fe188c5d873a61f9aaa791b46d4f Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Sun, 22 May 2016 21:58:16 -0700 Subject: [PATCH 11/31] PR --- Gemfile | 1 + Gemfile.lock | 4 ++ app/controllers/suggestions_controller.rb | 38 ++++++++++++++++--- app/views/suggestions/index.html.erb | 7 +++- config/application.rb | 2 + lib/TunesTakeoutWrapper.rb | 10 ++++- .../suggestions_controller_test.rb | 7 ++-- test/test_helper.rb | 6 +++ 8 files changed, 65 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index 30e58d3..d5ce21b 100644 --- a/Gemfile +++ b/Gemfile @@ -42,6 +42,7 @@ group :development, :test do gem 'pry-rails' gem 'webmock' gem 'dotenv-rails' + gem 'minitest-rails' end diff --git a/Gemfile.lock b/Gemfile.lock index 60656fe..f81a492 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -94,6 +94,9 @@ GEM minispec-metadata (2.0.0) minitest minitest (5.8.4) + minitest-rails (2.2.1) + minitest (~> 5.7) + railties (~> 4.1) minitest-reporters (1.1.9) ansi builder @@ -219,6 +222,7 @@ DEPENDENCIES httparty jbuilder (~> 2.0) jquery-rails + minitest-rails minitest-reporters minitest-vcr omniauth diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 1f6cc3e..023894e 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -1,12 +1,40 @@ -require_relative "../../lib/TunesTakeoutWrapper" - class SuggestionsController < ApplicationController + + # def show + # @results = TunesTakeoutWrapper.search(params[:keyword]) + # end + def index + @results = get_suggestions end - def show - @results = TunesTakeoutWrapper.search(params[:keyword]) + def get_suggestions + results = TunesTakeoutWrapper.top_twenty + results["suggestions"].map do |suggestion_id| + suggestion = TunesTakeoutWrapper.suggestion_info(suggestion_id)["suggestion"] + logger.info suggestion + + { + music: get_music(suggestion), + food: get_food(suggestion), + } + end end - + + def get_music(suggestion) + case suggestion["music_type"] + when "artist" + RSpotify::Artist.find(suggestion["music_id"]) + when "track" + RSpotify::Track.find(suggestion["music_id"]) + when "album" + RSpotify::Album.find(suggestion["music_id"]) + end + end + + def get_food(suggestion) + "food" + end + end diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index 8364809..0255296 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -4,4 +4,9 @@ <%= submit_tag("Search") %> <% end %> -<%= @data %> #returns nothing +<% @results.each do |pair| %> +
+ <%= pair %> +
+
+<% end %> diff --git a/config/application.rb b/config/application.rb index 9b37a86..19130f9 100644 --- a/config/application.rb +++ b/config/application.rb @@ -1,6 +1,8 @@ require File.expand_path('../boot', __FILE__) require 'rails/all' +require_relative "../lib/TunesTakeoutWrapper" + # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. diff --git a/lib/TunesTakeoutWrapper.rb b/lib/TunesTakeoutWrapper.rb index 741f0f4..2fcb9b8 100644 --- a/lib/TunesTakeoutWrapper.rb +++ b/lib/TunesTakeoutWrapper.rb @@ -4,6 +4,14 @@ class TunesTakeoutWrapper BASE_URL = "https://tunes-takeout-api.herokuapp.com/" def self.search(keyword) - @data = HTTParty.get(BASE_URL + "v1/suggestions/search?query=" + keyword).parsed_response + HTTParty.get(BASE_URL + "v1/suggestions/search?query=" + keyword).parsed_response + end + + def self.top_twenty + HTTParty.get(BASE_URL + "v1/suggestions/top").parsed_response + end + + def self.suggestion_info(id) + HTTParty.get(BASE_URL + "/v1/suggestions/" + id).parsed_response end end diff --git a/test/controllers/suggestions_controller_test.rb b/test/controllers/suggestions_controller_test.rb index f4587a5..98d4091 100644 --- a/test/controllers/suggestions_controller_test.rb +++ b/test/controllers/suggestions_controller_test.rb @@ -1,7 +1,8 @@ require 'test_helper' -class SuggestionsControllerTest < ActionController::TestCase - # test "the truth" do - # assert true +describe SuggestionsController do + # it 'works' do + # get '/top_twenty' + # assert_response :success # end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 92e39b2..e526f65 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,6 +1,12 @@ ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' +require 'minitest/spec' + +VCR.configure do |c| + c.cassette_library_dir = 'test/cassettes' + c.hook_into :webmock +end class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. From 582537ea7ebc47890f29047f79d1905416917b33 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Sun, 29 May 2016 15:51:40 -0700 Subject: [PATCH 12/31] Undo some messy suggestions controller code to take a different approach. Top 20 method currently returns only the pair ID from TunesTakeout API. --- app/controllers/suggestions_controller.rb | 25 ++++++++++++----------- app/views/suggestions/index.html.erb | 4 ++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 023894e..16ab2b3 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -11,15 +11,16 @@ def index def get_suggestions results = TunesTakeoutWrapper.top_twenty - results["suggestions"].map do |suggestion_id| - suggestion = TunesTakeoutWrapper.suggestion_info(suggestion_id)["suggestion"] - logger.info suggestion - - { - music: get_music(suggestion), - food: get_food(suggestion), - } - end + @results = results["suggestions"] + # results["suggestions"].map do |suggestion_id| + # suggestion = TunesTakeoutWrapper.suggestion_info(suggestion_id)["suggestion"] + # logger.info suggestion + # + # { + # music: get_music(suggestion), + # # food: get_food(suggestion), + # } + # end end def get_music(suggestion) @@ -33,8 +34,8 @@ def get_music(suggestion) end end - def get_food(suggestion) - "food" - end + # def get_food(suggestion) + # "food" + # end end diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index 0255296..8d44a2c 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -4,9 +4,9 @@ <%= submit_tag("Search") %> <% end %> -<% @results.each do |pair| %> +<% @results.each do |pair_id| %>
- <%= pair %> + <%= pair_id %>

<% end %> From a39730a6ad66dc395c16b75d6a6ab46efaaff04e Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Sun, 29 May 2016 15:57:14 -0700 Subject: [PATCH 13/31] Refactored suggestions controller methods. --- app/controllers/suggestions_controller.rb | 28 +++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 16ab2b3..0894991 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -5,11 +5,11 @@ class SuggestionsController < ApplicationController # @results = TunesTakeoutWrapper.search(params[:keyword]) # end - def index - @results = get_suggestions - end + # def index + # @results = get_suggestions + # end - def get_suggestions + def index results = TunesTakeoutWrapper.top_twenty @results = results["suggestions"] # results["suggestions"].map do |suggestion_id| @@ -23,16 +23,16 @@ def get_suggestions # end end - def get_music(suggestion) - case suggestion["music_type"] - when "artist" - RSpotify::Artist.find(suggestion["music_id"]) - when "track" - RSpotify::Track.find(suggestion["music_id"]) - when "album" - RSpotify::Album.find(suggestion["music_id"]) - end - end + # def get_music(suggestion) + # case suggestion["music_type"] + # when "artist" + # RSpotify::Artist.find(suggestion["music_id"]) + # when "track" + # RSpotify::Track.find(suggestion["music_id"]) + # when "album" + # RSpotify::Album.find(suggestion["music_id"]) + # end + # end # def get_food(suggestion) # "food" From 06e64259fce66323349fa067bb6c48c926043cbe Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Sun, 29 May 2016 16:03:36 -0700 Subject: [PATCH 14/31] Updated Top 20 method and view to show only suggestion info. --- app/controllers/suggestions_controller.rb | 6 +----- app/views/suggestions/index.html.erb | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 0894991..df485b6 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -5,10 +5,6 @@ class SuggestionsController < ApplicationController # @results = TunesTakeoutWrapper.search(params[:keyword]) # end - # def index - # @results = get_suggestions - # end - def index results = TunesTakeoutWrapper.top_twenty @results = results["suggestions"] @@ -23,7 +19,7 @@ def index # end end - # def get_music(suggestion) + # def get_music(suggestion, music_type) # case suggestion["music_type"] # when "artist" # RSpotify::Artist.find(suggestion["music_id"]) diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index 8d44a2c..76916f0 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -6,7 +6,7 @@ <% @results.each do |pair_id| %>
- <%= pair_id %> + <%= TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %>

<% end %> From 73b302ececb3374fcadbfee917e09fb4bfc81e7d Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Mon, 30 May 2016 14:54:36 -0700 Subject: [PATCH 15/31] Created Spotify wrapper. --- lib/tasks/SpotifyWrapper.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 lib/tasks/SpotifyWrapper.rb diff --git a/lib/tasks/SpotifyWrapper.rb b/lib/tasks/SpotifyWrapper.rb new file mode 100644 index 0000000..2d3b32a --- /dev/null +++ b/lib/tasks/SpotifyWrapper.rb @@ -0,0 +1,19 @@ +require 'httparty' + +class SpotifyWrapper + BASE_URL = "https://tunes-takeout-api.herokuapp.com/" + + def self.find_album(music_id) + album = RSpotify::Album.find(music_id) + end + + def self.find_artist(music_id) + album = RSpotify::Artist.find(music_id) + # HTTParty.get(BASE_URL + "v1/suggestions/search?query=" + keyword).parsed_response + end + + def self.find_track(music_id) + album = RSpotify::Track.find(music_id) + # HTTParty.get(BASE_URL + "v1/suggestions/search?query=" + keyword).parsed_response + end +end From 11e23759854b1182e4a9fea19b656a309400e862 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Mon, 30 May 2016 15:29:59 -0700 Subject: [PATCH 16/31] Top twenty now returns names of artists, albums, and tracks. --- app/views/suggestions/index.html.erb | 22 +++++++++++++++++---- lib/tasks/SpotifyWrapper.rb | 29 +++++++++++++++++----------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index 76916f0..dba72b3 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -5,8 +5,22 @@ <% end %> <% @results.each do |pair_id| %> -
- <%= TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> -
-
+

+ <% music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> + <% if music["music_type"] == "album" %> + <%= RSpotify::Album.find(music["music_id"]).name %> + + <% elsif music["music_type"] == "artist" %> + <%= RSpotify::Artist.find(music["music_id"]).name %> + <%= RSpotify::Artist.find(music["music_id"]).followers %> + + + <% elsif music["music_type"] == "track" %> + <%= RSpotify::Track.find(music["music_id"]).name %> + <%= RSpotify::Track.find(music["music_id"]).preview_url %> + + <% else %> + <%= "Invalid music type." %> + <% end %> +

<% end %> diff --git a/lib/tasks/SpotifyWrapper.rb b/lib/tasks/SpotifyWrapper.rb index 2d3b32a..fb8b61c 100644 --- a/lib/tasks/SpotifyWrapper.rb +++ b/lib/tasks/SpotifyWrapper.rb @@ -1,19 +1,26 @@ require 'httparty' +require 'rspotify' class SpotifyWrapper BASE_URL = "https://tunes-takeout-api.herokuapp.com/" - def self.find_album(music_id) - album = RSpotify::Album.find(music_id) + def self.hello + artists = RSpotify::Artist.search('Arctic Monkeys') + arctic_monkeys = artists.first + arctic_monkeys.popularity end - def self.find_artist(music_id) - album = RSpotify::Artist.find(music_id) - # HTTParty.get(BASE_URL + "v1/suggestions/search?query=" + keyword).parsed_response - end - - def self.find_track(music_id) - album = RSpotify::Track.find(music_id) - # HTTParty.get(BASE_URL + "v1/suggestions/search?query=" + keyword).parsed_response - end + # def self.find_album(music_id) + # RSpotify::Album.find(music_id) + # end + # + # def self.find_artist(music_id) + # RSpotify::Artist.find(music_id) + # # HTTParty.get(BASE_URL + "v1/suggestions/search?query=" + keyword).parsed_response + # end + # + # def self.find_track(music_id) + # RSpotify::Track.find(music_id) + # # HTTParty.get(BASE_URL + "v1/suggestions/search?query=" + keyword).parsed_response + # end end From 1b12e5cc2708fa6744809e468fd5a85d74610fd3 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Mon, 30 May 2016 15:33:13 -0700 Subject: [PATCH 17/31] Top twenty view shows total number of artist followers(favorites) and tracks' preview URLs. --- app/views/suggestions/index.html.erb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index dba72b3..9ba7381 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -10,13 +10,14 @@ <% if music["music_type"] == "album" %> <%= RSpotify::Album.find(music["music_id"]).name %> + <% elsif music["music_type"] == "artist" %> - <%= RSpotify::Artist.find(music["music_id"]).name %> - <%= RSpotify::Artist.find(music["music_id"]).followers %> + <%= RSpotify::Artist.find(music["music_id"]).name %>, + <%= RSpotify::Artist.find(music["music_id"]).followers["total"] %> <% elsif music["music_type"] == "track" %> - <%= RSpotify::Track.find(music["music_id"]).name %> + <%= RSpotify::Track.find(music["music_id"]).name %>, <%= RSpotify::Track.find(music["music_id"]).preview_url %> <% else %> From ddfbedbd1363b8602d0bfe8653647607772721b0 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Wed, 1 Jun 2016 16:21:27 -0700 Subject: [PATCH 18/31] Added all required album info to the top 20 view. --- app/views/suggestions/index.html.erb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index 9ba7381..bc46cf9 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -8,7 +8,12 @@

<% music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> <% if music["music_type"] == "album" %> - <%= RSpotify::Album.find(music["music_id"]).name %> + <%= RSpotify::Album.find(music["music_id"]).name %>, + <%= RSpotify::Album.find(music["music_id"]).uri %>, + <%= RSpotify::Album.find(music["music_id"]).images[0]["url"] %>, + <%= RSpotify::Album.find(music["music_id"]).album_type %>, + <%= RSpotify::Album.find(music["music_id"]).external_urls['spotify'] %> + <% elsif music["music_type"] == "artist" %> @@ -17,7 +22,7 @@ <% elsif music["music_type"] == "track" %> - <%= RSpotify::Track.find(music["music_id"]).name %>, + <%= RSpotify::Track.find(music["music_id"]).name %>, <%= RSpotify::Track.find(music["music_id"]).preview_url %> <% else %> From ffef0e3e052f84fb906b3ac602b35891f2bb99ad Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Wed, 1 Jun 2016 16:40:39 -0700 Subject: [PATCH 19/31] Added all required artist info to the top 20 view. Unable to pull the image URL from the artist image hash for some reason...? --- app/views/suggestions/index.html.erb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index bc46cf9..a7068f3 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -18,7 +18,11 @@ <% elsif music["music_type"] == "artist" %> <%= RSpotify::Artist.find(music["music_id"]).name %>, - <%= RSpotify::Artist.find(music["music_id"]).followers["total"] %> + <%= RSpotify::Artist.find(music["music_id"]).uri %>, + <%= RSpotify::Artist.find(music["music_id"]).images[0] %>, + <%= RSpotify::Artist.find(music["music_id"]).type %>, + <%= RSpotify::Artist.find(music["music_id"]).external_urls['spotify'] %> + <% elsif music["music_type"] == "track" %> From 034c40ee09138f153b2ca9bcec470dcf6bc6b848 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Wed, 1 Jun 2016 16:49:41 -0700 Subject: [PATCH 20/31] Added all required track info to the top 20 view. No image URL for tracks. --- app/views/suggestions/index.html.erb | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index a7068f3..13a111a 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -4,30 +4,36 @@ <%= submit_tag("Search") %> <% end %> +<%= @results %> + <% @results.each do |pair_id| %>

<% music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> <% if music["music_type"] == "album" %> - <%= RSpotify::Album.find(music["music_id"]).name %>, - <%= RSpotify::Album.find(music["music_id"]).uri %>, - <%= RSpotify::Album.find(music["music_id"]).images[0]["url"] %>, - <%= RSpotify::Album.find(music["music_id"]).album_type %>, - <%= RSpotify::Album.find(music["music_id"]).external_urls['spotify'] %> + <%= RSpotify::Album.find(music["music_id"]).name %>
+ <%= RSpotify::Album.find(music["music_id"]).uri %>
+ <%= RSpotify::Album.find(music["music_id"]).images[0]["url"] %>
+ <%= RSpotify::Album.find(music["music_id"]).album_type %>
+ <%= RSpotify::Album.find(music["music_id"]).external_urls['spotify'] %>


<% elsif music["music_type"] == "artist" %> - <%= RSpotify::Artist.find(music["music_id"]).name %>, - <%= RSpotify::Artist.find(music["music_id"]).uri %>, - <%= RSpotify::Artist.find(music["music_id"]).images[0] %>, - <%= RSpotify::Artist.find(music["music_id"]).type %>, + <%= RSpotify::Artist.find(music["music_id"]).name %>
+ <%= RSpotify::Artist.find(music["music_id"]).uri %>
+ <%= RSpotify::Artist.find(music["music_id"]).images[0] %>
+ <%= RSpotify::Artist.find(music["music_id"]).type %>
<%= RSpotify::Artist.find(music["music_id"]).external_urls['spotify'] %> +


<% elsif music["music_type"] == "track" %> - <%= RSpotify::Track.find(music["music_id"]).name %>, - <%= RSpotify::Track.find(music["music_id"]).preview_url %> + <%= RSpotify::Track.find(music["music_id"]).name %>
+ <%= RSpotify::Track.find(music["music_id"]).uri %>
+ <%= RSpotify::Track.find(music["music_id"]).type %>
+ <%= RSpotify::Track.find(music["music_id"]).external_urls['spotify'] %> +


<% else %> <%= "Invalid music type." %> From f43c5bc7b3cd4d779a528301f2aa13a87cf607ea Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Wed, 1 Jun 2016 20:43:30 -0700 Subject: [PATCH 21/31] Attempted to move the Spotify info gathering from the top 20 view page to the Spotify wrapper and failed miserably. Moved everything back to the view.-_- --- app/views/suggestions/index.html.erb | 62 +++++++++++++--------------- lib/TunesTakeoutWrapper.rb | 1 + lib/tasks/SpotifyWrapper.rb | 30 ++++++++++++++ 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index 13a111a..094aec6 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -4,39 +4,33 @@ <%= submit_tag("Search") %> <% end %> -<%= @results %> - <% @results.each do |pair_id| %> -

- <% music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> - <% if music["music_type"] == "album" %> - <%= RSpotify::Album.find(music["music_id"]).name %>
- <%= RSpotify::Album.find(music["music_id"]).uri %>
- <%= RSpotify::Album.find(music["music_id"]).images[0]["url"] %>
- <%= RSpotify::Album.find(music["music_id"]).album_type %>
- <%= RSpotify::Album.find(music["music_id"]).external_urls['spotify'] %>


- - - - <% elsif music["music_type"] == "artist" %> - <%= RSpotify::Artist.find(music["music_id"]).name %>
- <%= RSpotify::Artist.find(music["music_id"]).uri %>
- <%= RSpotify::Artist.find(music["music_id"]).images[0] %>
- <%= RSpotify::Artist.find(music["music_id"]).type %>
- <%= RSpotify::Artist.find(music["music_id"]).external_urls['spotify'] %> -


- - - - <% elsif music["music_type"] == "track" %> - <%= RSpotify::Track.find(music["music_id"]).name %>
- <%= RSpotify::Track.find(music["music_id"]).uri %>
- <%= RSpotify::Track.find(music["music_id"]).type %>
- <%= RSpotify::Track.find(music["music_id"]).external_urls['spotify'] %> -


- - <% else %> - <%= "Invalid music type." %> - <% end %> -

+

+ <% music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> + <% if music["music_type"] == "album" %> + <%= RSpotify::Album.find(music["music_id"]).name %>
+ <%= RSpotify::Album.find(music["music_id"]).uri %>
+ <%= RSpotify::Album.find(music["music_id"]).images[0]["url"] %>
+ <%= RSpotify::Album.find(music["music_id"]).album_type %>
+ <%= RSpotify::Album.find(music["music_id"]).external_urls['spotify'] %>


+ + <% elsif music["music_type"] == "artist" %> + <%= RSpotify::Artist.find(music["music_id"]).name %>
+ <%= RSpotify::Artist.find(music["music_id"]).uri %>
+ <%= RSpotify::Artist.find(music["music_id"]).images[0] %>
+ <%= RSpotify::Artist.find(music["music_id"]).type %>
+ <%= RSpotify::Artist.find(music["music_id"]).external_urls['spotify'] %> +


+ + <% elsif music["music_type"] == "track" %> + <%= RSpotify::Track.find(music["music_id"]).name %>
+ <%= RSpotify::Track.find(music["music_id"]).uri %>
+ <%= RSpotify::Track.find(music["music_id"]).type %>
+ <%= RSpotify::Track.find(music["music_id"]).external_urls['spotify'] %> +


+ + <% else %> + <%= "Invalid music type." %> + <% end %> +

<% end %> diff --git a/lib/TunesTakeoutWrapper.rb b/lib/TunesTakeoutWrapper.rb index 2fcb9b8..0643edd 100644 --- a/lib/TunesTakeoutWrapper.rb +++ b/lib/TunesTakeoutWrapper.rb @@ -13,5 +13,6 @@ def self.top_twenty def self.suggestion_info(id) HTTParty.get(BASE_URL + "/v1/suggestions/" + id).parsed_response + # SpotifyWrapper.top_twenty end end diff --git a/lib/tasks/SpotifyWrapper.rb b/lib/tasks/SpotifyWrapper.rb index fb8b61c..fea55cb 100644 --- a/lib/tasks/SpotifyWrapper.rb +++ b/lib/tasks/SpotifyWrapper.rb @@ -10,6 +10,36 @@ def self.hello arctic_monkeys.popularity end + # def self.top_twenty + # @results.each do |pair_id| + # music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] + # + # if music["music_type"] == "album" + # @album_name = RSpotify::Album.find(music["music_id"]).name + # @album_uri = RSpotify::Album.find(music["music_id"]).uri + # @album_image = RSpotify::Album.find(music["music_id"]).images[0]["url"] + # @album_type = RSpotify::Album.find(music["music_id"]).album_type + # @album_url = RSpotify::Album.find(music["music_id"]).external_urls['spotify'] + # + # elsif music["music_type"] == "artist" + # @artist_name = RSpotify::Artist.find(music["music_id"]).name + # @artist_uri = RSpotify::Artist.find(music["music_id"]).uri + # @artist_image = RSpotify::Artist.find(music["music_id"]).images[0] + # @artist_type = RSpotify::Artist.find(music["music_id"]).type + # @artist_url = RSpotify::Artist.find(music["music_id"]).external_urls['spotify'] + # + # elsif music["music_type"] == "track" + # @track_name = RSpotify::Track.find(music["music_id"]).name + # @track_uri = RSpotify::Track.find(music["music_id"]).uri + # @track_type = RSpotify::Track.find(music["music_id"]).type + # @track_url = RSpotify::Track.find(music["music_id"]).external_urls['spotify'] + # + # else + # "Invalid music type." + # end + # end + # end + # def self.find_album(music_id) # RSpotify::Album.find(music_id) # end From 02c9208c2226eb8c6b8825b84eb3132ea1474c04 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Fri, 3 Jun 2016 13:09:09 -0700 Subject: [PATCH 22/31] Added required Yelp business info to view. --- app/views/layouts/application.html.erb | 18 +++++++++ app/views/sessions/index.html.erb | 11 ------ app/views/suggestions/index.html.erb | 21 ++++++----- config/initializers/yelp.rb | 8 ++++ config/routes.rb | 2 +- lib/tasks/YelpWrapper.rb | 51 ++++++++++++++++++++++++++ 6 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 config/initializers/yelp.rb create mode 100644 lib/tasks/YelpWrapper.rb diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 5bbee87..59e9fb3 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -9,6 +9,24 @@

<%= link_to "tunes&takeout", root_path %>

+<% if current_user %> + + <%= form_tag top_twenty_results_path do %> + <%= label_tag(:q, "Search for:") %> + <%= text_field_tag(:q) %> + <%= submit_tag("Search") %> + <% end %> +<% else %> + + +<% end %> + <%= yield %> diff --git a/app/views/sessions/index.html.erb b/app/views/sessions/index.html.erb index 270b375..8b13789 100644 --- a/app/views/sessions/index.html.erb +++ b/app/views/sessions/index.html.erb @@ -1,12 +1 @@ -<% if current_user %> - -<% else %> - -<% end %> diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index 094aec6..be04330 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -1,33 +1,34 @@ -<%= form_tag top_twenty_results_path do %> - <%= label_tag(:q, "Search for:") %> - <%= text_field_tag(:q) %> - <%= submit_tag("Search") %> -<% end %> +

Top Twenty Suggested Pairings

<% @results.each do |pair_id| %>

<% music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> + <%= Yelp.client.business(music["food_id"]).business.name %> + <%= Yelp.client.business(music["food_id"]).business.id %> + <%= Yelp.client.business(music["food_id"]).business.image_url %> + <%= Yelp.client.business(music["food_id"]).business.rating %> + <%= Yelp.client.business(music["food_id"]).business.url %> + <% if music["music_type"] == "album" %> <%= RSpotify::Album.find(music["music_id"]).name %>
<%= RSpotify::Album.find(music["music_id"]).uri %>
<%= RSpotify::Album.find(music["music_id"]).images[0]["url"] %>
<%= RSpotify::Album.find(music["music_id"]).album_type %>
- <%= RSpotify::Album.find(music["music_id"]).external_urls['spotify'] %>


+ <%= RSpotify::Album.find(music["music_id"]).external_urls['spotify'] %> +


<% elsif music["music_type"] == "artist" %> <%= RSpotify::Artist.find(music["music_id"]).name %>
<%= RSpotify::Artist.find(music["music_id"]).uri %>
<%= RSpotify::Artist.find(music["music_id"]).images[0] %>
<%= RSpotify::Artist.find(music["music_id"]).type %>
- <%= RSpotify::Artist.find(music["music_id"]).external_urls['spotify'] %> -


+ <%= RSpotify::Artist.find(music["music_id"]).external_urls['spotify'] %>


<% elsif music["music_type"] == "track" %> <%= RSpotify::Track.find(music["music_id"]).name %>
<%= RSpotify::Track.find(music["music_id"]).uri %>
<%= RSpotify::Track.find(music["music_id"]).type %>
- <%= RSpotify::Track.find(music["music_id"]).external_urls['spotify'] %> -


+ <%= RSpotify::Track.find(music["music_id"]).external_urls['spotify'] %>


<% else %> <%= "Invalid music type." %> diff --git a/config/initializers/yelp.rb b/config/initializers/yelp.rb new file mode 100644 index 0000000..e1bb5ad --- /dev/null +++ b/config/initializers/yelp.rb @@ -0,0 +1,8 @@ +require 'yelp' + +Yelp.client.configure do |config| + config.consumer_key = ENV["YELP_CONSUMER_KEY"] + config.consumer_secret = ENV["YELP_CONSUMER_SECRET"] + config.token = ENV["YELP_TOKEN"] + config.token_secret = ENV["YELP_TOKEN_SECRET"] +end diff --git a/config/routes.rb b/config/routes.rb index 92b63b1..61c8f04 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". - root 'sessions#index' + root 'suggestions#index' # get '/top_twenty' => 'sessions#top_twenty' diff --git a/lib/tasks/YelpWrapper.rb b/lib/tasks/YelpWrapper.rb new file mode 100644 index 0000000..24dbf63 --- /dev/null +++ b/lib/tasks/YelpWrapper.rb @@ -0,0 +1,51 @@ +require 'httparty' +require 'yelp' + +class YelpWrapper + BASE_URL = "https://tunes-takeout-api.herokuapp.com/" + + + # def self.top_twenty + # @results.each do |pair_id| + # music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] + # + # if music["music_type"] == "album" + # @album_name = RSpotify::Album.find(music["music_id"]).name + # @album_uri = RSpotify::Album.find(music["music_id"]).uri + # @album_image = RSpotify::Album.find(music["music_id"]).images[0]["url"] + # @album_type = RSpotify::Album.find(music["music_id"]).album_type + # @album_url = RSpotify::Album.find(music["music_id"]).external_urls['spotify'] + # + # elsif music["music_type"] == "artist" + # @artist_name = RSpotify::Artist.find(music["music_id"]).name + # @artist_uri = RSpotify::Artist.find(music["music_id"]).uri + # @artist_image = RSpotify::Artist.find(music["music_id"]).images[0] + # @artist_type = RSpotify::Artist.find(music["music_id"]).type + # @artist_url = RSpotify::Artist.find(music["music_id"]).external_urls['spotify'] + # + # elsif music["music_type"] == "track" + # @track_name = RSpotify::Track.find(music["music_id"]).name + # @track_uri = RSpotify::Track.find(music["music_id"]).uri + # @track_type = RSpotify::Track.find(music["music_id"]).type + # @track_url = RSpotify::Track.find(music["music_id"]).external_urls['spotify'] + # + # else + # "Invalid music type." + # end + # end + # end + + # def self.find_album(music_id) + # RSpotify::Album.find(music_id) + # end + # + # def self.find_artist(music_id) + # RSpotify::Artist.find(music_id) + # # HTTParty.get(BASE_URL + "v1/suggestions/search?query=" + keyword).parsed_response + # end + # + # def self.find_track(music_id) + # RSpotify::Track.find(music_id) + # # HTTParty.get(BASE_URL + "v1/suggestions/search?query=" + keyword).parsed_response + # end +end From 0030548a086021c30820e7934fefa06e54bf0932 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Fri, 3 Jun 2016 13:42:30 -0700 Subject: [PATCH 23/31] Push to Charles. --- app/views/layouts/application.html.erb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 59e9fb3..b1e2a98 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,17 +1,24 @@ - TunesTakeoutSpotify Icon - <%= stylesheet_link_tag 'application', media: 'all' %> - <%= javascript_include_tag 'application' %> - <%= csrf_meta_tags %> + TunesTakeout + + Spotify Icon + <%= stylesheet_link_tag 'application', media: 'all' %> + <%= javascript_include_tag 'application' %> + <%= csrf_meta_tags %> +

<%= link_to "tunes&takeout", root_path %>

<% if current_user %> <%= form_tag top_twenty_results_path do %> From 8db83963eb1d38f52a9afe3d2361bb6f0728d79a Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Fri, 3 Jun 2016 15:27:37 -0700 Subject: [PATCH 24/31] As a user, I can search pairings. --- Gemfile | 2 ++ Gemfile.lock | 5 +++ app/controllers/suggestions_controller.rb | 32 ++--------------- app/views/layouts/application.html.erb | 9 ++--- app/views/suggestions/_suggestion.html.erb | 0 app/views/suggestions/search.html.erb | 40 ++++++++++++++++++++++ config/routes.rb | 5 ++- lib/TunesTakeoutWrapper.rb | 3 +- 8 files changed, 58 insertions(+), 38 deletions(-) delete mode 100644 app/views/suggestions/_suggestion.html.erb create mode 100644 app/views/suggestions/search.html.erb diff --git a/Gemfile b/Gemfile index d5ce21b..c01d275 100644 --- a/Gemfile +++ b/Gemfile @@ -47,6 +47,8 @@ group :development, :test do end group :development do + gem "better_errors" + # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' diff --git a/Gemfile.lock b/Gemfile.lock index f81a492..edcd450 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -39,6 +39,10 @@ GEM addressable (2.4.0) ansi (1.5.0) arel (6.0.3) + better_errors (2.1.1) + coderay (>= 1.0.0) + erubis (>= 2.6.6) + rack (>= 0.9.0) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) builder (3.2.2) @@ -216,6 +220,7 @@ PLATFORMS ruby DEPENDENCIES + better_errors byebug coffee-rails (~> 4.1.0) dotenv-rails diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index df485b6..8f6be24 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -1,37 +1,11 @@ class SuggestionsController < ApplicationController - - # def show - # @results = TunesTakeoutWrapper.search(params[:keyword]) - # end - def index results = TunesTakeoutWrapper.top_twenty @results = results["suggestions"] - # results["suggestions"].map do |suggestion_id| - # suggestion = TunesTakeoutWrapper.suggestion_info(suggestion_id)["suggestion"] - # logger.info suggestion - # - # { - # music: get_music(suggestion), - # # food: get_food(suggestion), - # } - # end end - # def get_music(suggestion, music_type) - # case suggestion["music_type"] - # when "artist" - # RSpotify::Artist.find(suggestion["music_id"]) - # when "track" - # RSpotify::Track.find(suggestion["music_id"]) - # when "album" - # RSpotify::Album.find(suggestion["music_id"]) - # end - # end - - # def get_food(suggestion) - # "food" - # end - + def search + @search_results = TunesTakeoutWrapper.search(params[:q])["suggestions"] + end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index b1e2a98..a37b5a9 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,14 +14,11 @@ <% if current_user %> - <%= form_tag top_twenty_results_path do %> + <%= form_tag search_path do %> <%= label_tag(:q, "Search for:") %> <%= text_field_tag(:q) %> <%= submit_tag("Search") %> diff --git a/app/views/suggestions/_suggestion.html.erb b/app/views/suggestions/_suggestion.html.erb deleted file mode 100644 index e69de29..0000000 diff --git a/app/views/suggestions/search.html.erb b/app/views/suggestions/search.html.erb new file mode 100644 index 0000000..257c72c --- /dev/null +++ b/app/views/suggestions/search.html.erb @@ -0,0 +1,40 @@ +

Results

+ +<% @search_results.each do |pair| %> +

+ <% business = Yelp.client.business(pair["food_id"]).business %> + <%= business.name %> + <%= business.id %> + <%= business.image_url %> + <%= business.rating %> + <%= business.url %> + + <% if pair["music_type"] == "album" %> + <% album = RSpotify::Album.find(pair["music_id"]) %> + <%= album.name %>
+ <%= album.uri %>
+ <%= album.images[0]["url"] %>
+ <%= album.album_type %>
+ <%= album.external_urls['spotify'] %> +


+ + <% elsif pair["music_type"] == "artist" %> + <% artist = RSpotify::Artist.find(pair["music_id"]) %> + <%= artist.name %>
+ <%= artist.uri %>
+ <%= artist.images[0] %>
+ <%= artist.type %>
+ <%= artist.external_urls['spotify'] %>


+ + <% elsif pair["music_type"] == "track" %> + <% track = RSpotify::Track.find(pair["music_id"]) %> + <%= track.name %>
+ <%= track.uri %>
+ <%= track.type %>
+ <%= track.external_urls['spotify'] %>


+ + <% else %> + <%= "Invalid music type." %> + <% end %> +

+<% end %> diff --git a/config/routes.rb b/config/routes.rb index 61c8f04..deb45d3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,7 +10,10 @@ get '/auth/spotify/callback', :to => 'sessions#create' get '/logout', :to => 'sessions#destroy' resources :food, :music + # Confirm these are the correct resources. What is this line even doing??????? + get '/top_twenty', :to => 'suggestions#index' post '/top_twenty/show', :to => 'suggestions#show', as: 'top_twenty_results' - # Confirm these are the correct resources. What is this line even doing??????? + + post '/v1/suggestions/search', :to => 'suggestions#search', as: 'search' end diff --git a/lib/TunesTakeoutWrapper.rb b/lib/TunesTakeoutWrapper.rb index 0643edd..bb6d666 100644 --- a/lib/TunesTakeoutWrapper.rb +++ b/lib/TunesTakeoutWrapper.rb @@ -12,7 +12,6 @@ def self.top_twenty end def self.suggestion_info(id) - HTTParty.get(BASE_URL + "/v1/suggestions/" + id).parsed_response - # SpotifyWrapper.top_twenty + HTTParty.get(BASE_URL + "/v1/suggestions/" + id["id"]).parsed_response end end From c3381d3f051201b5b11f077f1b6bdb5a3f4a7e96 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Fri, 3 Jun 2016 15:48:06 -0700 Subject: [PATCH 25/31] Refactored view. --- app/controllers/suggestions_controller.rb | 16 +++++++++ app/views/suggestions/favorites.html.erb | 1 + app/views/suggestions/index.html.erb | 42 +++++++++++++---------- config/routes.rb | 2 ++ lib/TunesTakeoutWrapper.rb | 7 +++- 5 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 app/views/suggestions/favorites.html.erb diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 8f6be24..18c232d 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -8,4 +8,20 @@ def index def search @search_results = TunesTakeoutWrapper.search(params[:q])["suggestions"] end + + def show + # shows individual pairing info + end + + def favorites + @favorites = TunesTakeoutWrapper.favorites + end + + def unfavorite + end + + def favorites + # shows all suggestions favorited by the signed-in User + end + end diff --git a/app/views/suggestions/favorites.html.erb b/app/views/suggestions/favorites.html.erb new file mode 100644 index 0000000..f6a5da4 --- /dev/null +++ b/app/views/suggestions/favorites.html.erb @@ -0,0 +1 @@ +<%= @favorites %> diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index be04330..fa9a6bb 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -3,32 +3,36 @@ <% @results.each do |pair_id| %>

<% music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> - <%= Yelp.client.business(music["food_id"]).business.name %> - <%= Yelp.client.business(music["food_id"]).business.id %> - <%= Yelp.client.business(music["food_id"]).business.image_url %> - <%= Yelp.client.business(music["food_id"]).business.rating %> - <%= Yelp.client.business(music["food_id"]).business.url %> + <% info = Yelp.client.business(music["food_id"]).business %> + <%= info.name %> + <%= info.id %> + <%= info.image_url %> + <%= info.rating %> + <%= info.url %> <% if music["music_type"] == "album" %> - <%= RSpotify::Album.find(music["music_id"]).name %>
- <%= RSpotify::Album.find(music["music_id"]).uri %>
- <%= RSpotify::Album.find(music["music_id"]).images[0]["url"] %>
- <%= RSpotify::Album.find(music["music_id"]).album_type %>
- <%= RSpotify::Album.find(music["music_id"]).external_urls['spotify'] %> + <% album = RSpotify::Album.find(music["music_id"]) %> + <%= album.name %>
+ <%= album.uri %>
+ <%= album.images[0]["url"] %>
+ <%= album.album_type %>
+ <%= album.external_urls['spotify'] %>


<% elsif music["music_type"] == "artist" %> - <%= RSpotify::Artist.find(music["music_id"]).name %>
- <%= RSpotify::Artist.find(music["music_id"]).uri %>
- <%= RSpotify::Artist.find(music["music_id"]).images[0] %>
- <%= RSpotify::Artist.find(music["music_id"]).type %>
- <%= RSpotify::Artist.find(music["music_id"]).external_urls['spotify'] %>


+ <% artist = RSpotify::Artist.find(music["music_id"]) %> + <%= artist.name %>
+ <%= artist.uri %>
+ <%= artist.images[0] %>
+ <%= artist.type %>
+ <%= artist.external_urls['spotify'] %>


<% elsif music["music_type"] == "track" %> - <%= RSpotify::Track.find(music["music_id"]).name %>
- <%= RSpotify::Track.find(music["music_id"]).uri %>
- <%= RSpotify::Track.find(music["music_id"]).type %>
- <%= RSpotify::Track.find(music["music_id"]).external_urls['spotify'] %>


+ <% track = RSpotify::Track.find(music["music_id"]) %> + <%= track.name %>
+ <%= track.uri %>
+ <%= track.type %>
+ <%= track.external_urls['spotify'] %>


<% else %> <%= "Invalid music type." %> diff --git a/config/routes.rb b/config/routes.rb index deb45d3..e6d4a00 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,4 +16,6 @@ post '/top_twenty/show', :to => 'suggestions#show', as: 'top_twenty_results' post '/v1/suggestions/search', :to => 'suggestions#search', as: 'search' + + get '/favorites', :to => 'suggestions#favorites', as: 'favorites' end diff --git a/lib/TunesTakeoutWrapper.rb b/lib/TunesTakeoutWrapper.rb index bb6d666..9abf9ef 100644 --- a/lib/TunesTakeoutWrapper.rb +++ b/lib/TunesTakeoutWrapper.rb @@ -12,6 +12,11 @@ def self.top_twenty end def self.suggestion_info(id) - HTTParty.get(BASE_URL + "/v1/suggestions/" + id["id"]).parsed_response + HTTParty.get(BASE_URL + "/v1/suggestions/" + id).parsed_response end + + def self.favorites + HTTParty.get(BASE_URL + "/v1/users/:#{RSpotify::User.find(current_user.name).id}/favorites").parsed_response + end + end From 80c13c58e18773a4444fddbc6de91eab96735ee3 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Fri, 3 Jun 2016 15:57:38 -0700 Subject: [PATCH 26/31] Created favorites routes, methods, and view. --- app/models/user.rb | 15 ++++++++++++++- app/views/suggestions/favorites.html.erb | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index c8fae25..36be0e5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,13 +1,16 @@ class User < ActiveRecord::Base validates :email, :name, :uid, :provider, presence: true + def initialize + @favorites = [] + end + def self.find_or_create_from_omniauth(auth_hash) # Find or create a user user = self.find_by(uid: auth_hash["uid"], provider: auth_hash["provider"]) if user return user else - # no user found, do something here user = User.new user.uid = auth_hash["uid"] user.provider = auth_hash["provider"] @@ -22,4 +25,14 @@ def self.find_or_create_from_omniauth(auth_hash) end end + def add_favorite(pair_id) + @favorites << pair_id + end + + def unfavorite(id) + @favorites.each do |pair| + if pair["id"] == id + # remove the pair ID from the array here + end + end diff --git a/app/views/suggestions/favorites.html.erb b/app/views/suggestions/favorites.html.erb index f6a5da4..a0cedbc 100644 --- a/app/views/suggestions/favorites.html.erb +++ b/app/views/suggestions/favorites.html.erb @@ -1 +1,3 @@ <%= @favorites %> + +Hello From 1ddca0d483178ad01c75a6551f7d7956cd455e51 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Fri, 3 Jun 2016 16:13:15 -0700 Subject: [PATCH 27/31] Fixed nav and heading links. --- app/models/user.rb | 2 ++ app/views/layouts/application.html.erb | 3 +-- app/views/suggestions/favorites.html.erb | 10 +++++++--- app/views/suggestions/show.html.erb | 0 config/routes.rb | 2 ++ lib/TunesTakeoutWrapper.rb | 1 + 6 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 app/views/suggestions/show.html.erb diff --git a/app/models/user.rb b/app/models/user.rb index 36be0e5..376606f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -33,6 +33,8 @@ def unfavorite(id) @favorites.each do |pair| if pair["id"] == id # remove the pair ID from the array here + end + end end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index a37b5a9..8244886 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,8 +14,7 @@ <% if current_user %>

<%= form_tag search_path do %> diff --git a/app/views/suggestions/favorites.html.erb b/app/views/suggestions/favorites.html.erb index a0cedbc..31046b5 100644 --- a/app/views/suggestions/favorites.html.erb +++ b/app/views/suggestions/favorites.html.erb @@ -1,3 +1,7 @@ -<%= @favorites %> - -Hello +<% if @favorites %> + <% @favorites.each do |fave| %> + <%= fave %> + <% end %> +<% else %> +

You have no favorites right now!

+<% end %> diff --git a/app/views/suggestions/show.html.erb b/app/views/suggestions/show.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/config/routes.rb b/config/routes.rb index e6d4a00..565914e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,4 +18,6 @@ post '/v1/suggestions/search', :to => 'suggestions#search', as: 'search' get '/favorites', :to => 'suggestions#favorites', as: 'favorites' + + get '/show/:id', :to => 'suggestions#show', as: 'show' end diff --git a/lib/TunesTakeoutWrapper.rb b/lib/TunesTakeoutWrapper.rb index 9abf9ef..f562d6a 100644 --- a/lib/TunesTakeoutWrapper.rb +++ b/lib/TunesTakeoutWrapper.rb @@ -17,6 +17,7 @@ def self.suggestion_info(id) def self.favorites HTTParty.get(BASE_URL + "/v1/users/:#{RSpotify::User.find(current_user.name).id}/favorites").parsed_response + # Returns a list of pair IDs from Charles' API end end From bac4293c69ee29b2efe9a3656f3638d72de08e01 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Sat, 4 Jun 2016 16:00:13 -0700 Subject: [PATCH 28/31] Got the favorite method working. --- app/controllers/application_controller.rb | 5 +++++ app/controllers/suggestions_controller.rb | 7 ++++--- app/views/layouts/application.html.erb | 1 - app/views/suggestions/index.html.erb | 2 ++ lib/TunesTakeoutWrapper.rb | 12 ++++++++---- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f5e4b4a..4aceccf 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,4 +8,9 @@ def current_user @current_user ||= User.find_by(id: session[:user_id]) end + def current_user_name + @current_user ||= User.find_by(id: session[:user_id]) + @current_user.name + end + end diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 18c232d..f75389c 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -10,18 +10,19 @@ def search end def show - # shows individual pairing info + end def favorites @favorites = TunesTakeoutWrapper.favorites + # shows all suggestions favorited by the signed-in User end def unfavorite end - def favorites - # shows all suggestions favorited by the signed-in User + def favorite + TunesTakeoutWrapper.favorite(params[:id]) end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 8244886..3efd1dd 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -27,7 +27,6 @@ <%= link_to "Log In", "/auth/spotify" %> <%= link_to "Top Pairings", "/top_twenty" %> - <% end %> diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index fa9a6bb..3336715 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -2,6 +2,8 @@ <% @results.each do |pair_id| %>

+ <%= hidden_field_tag :favorite, TunesTakeoutWrapper.favorite(pair_id) %> + <% music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> <% info = Yelp.client.business(music["food_id"]).business %> <%= info.name %> diff --git a/lib/TunesTakeoutWrapper.rb b/lib/TunesTakeoutWrapper.rb index f562d6a..4360027 100644 --- a/lib/TunesTakeoutWrapper.rb +++ b/lib/TunesTakeoutWrapper.rb @@ -15,9 +15,13 @@ def self.suggestion_info(id) HTTParty.get(BASE_URL + "/v1/suggestions/" + id).parsed_response end - def self.favorites - HTTParty.get(BASE_URL + "/v1/users/:#{RSpotify::User.find(current_user.name).id}/favorites").parsed_response - # Returns a list of pair IDs from Charles' API + # def self.favorites + # HTTParty.get(BASE_URL + "/v1/users/:#{RSpotify::User.uid}/favorites").parsed_response + # # Returns a list of pair IDs from Charles' API + # end + # + def self.favorite(pair_id) + HTTParty.post(BASE_URL + "/v1/users/:#{User.last.uid}/favorites", body: {"suggestions": pair_id}.to_json) + # work on this end - end From 2864e2f6bbe74aa8eac849a15496b9be516b17e6 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Sat, 4 Jun 2016 16:11:46 -0700 Subject: [PATCH 29/31] User can now favorite a suggestion. --- app/views/suggestions/favorite.html.erb | 1 + app/views/suggestions/index.html.erb | 8 ++++++-- config/routes.rb | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 app/views/suggestions/favorite.html.erb diff --git a/app/views/suggestions/favorite.html.erb b/app/views/suggestions/favorite.html.erb new file mode 100644 index 0000000..5341dc1 --- /dev/null +++ b/app/views/suggestions/favorite.html.erb @@ -0,0 +1 @@ +

Added to Favorites!

diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index 3336715..cfb83c0 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -2,8 +2,6 @@ <% @results.each do |pair_id| %>

- <%= hidden_field_tag :favorite, TunesTakeoutWrapper.favorite(pair_id) %> - <% music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> <% info = Yelp.client.business(music["food_id"]).business %> <%= info.name %> @@ -39,5 +37,11 @@ <% else %> <%= "Invalid music type." %> <% end %> + + <%= hidden_field_tag :favorite, TunesTakeoutWrapper.favorite(pair_id) %> + <%= form_tag favorite_path do %> + <%= hidden_field_tag "pair_id", pair_id %> + <%= submit_tag "Favorite" %> + <% end %>

<% end %> diff --git a/config/routes.rb b/config/routes.rb index 565914e..4b59270 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,4 +20,6 @@ get '/favorites', :to => 'suggestions#favorites', as: 'favorites' get '/show/:id', :to => 'suggestions#show', as: 'show' + + post '/favorited', :to => 'suggestions#favorite', as: 'favorite' end From 380517c920fd7c0e37ebdf1080bbc9fc073fa1c7 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Sat, 4 Jun 2016 16:54:19 -0700 Subject: [PATCH 30/31] Refactor. --- app/views/suggestions/favorites.html.erb | 2 ++ lib/TunesTakeoutWrapper.rb | 12 +++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/views/suggestions/favorites.html.erb b/app/views/suggestions/favorites.html.erb index 31046b5..8f69f27 100644 --- a/app/views/suggestions/favorites.html.erb +++ b/app/views/suggestions/favorites.html.erb @@ -5,3 +5,5 @@ <% else %>

You have no favorites right now!

<% end %> + +<%= @favorites %> diff --git a/lib/TunesTakeoutWrapper.rb b/lib/TunesTakeoutWrapper.rb index 4360027..058b642 100644 --- a/lib/TunesTakeoutWrapper.rb +++ b/lib/TunesTakeoutWrapper.rb @@ -15,13 +15,15 @@ def self.suggestion_info(id) HTTParty.get(BASE_URL + "/v1/suggestions/" + id).parsed_response end - # def self.favorites - # HTTParty.get(BASE_URL + "/v1/users/:#{RSpotify::User.uid}/favorites").parsed_response - # # Returns a list of pair IDs from Charles' API - # end - # + def self.favorites + HTTParty.get(BASE_URL + "/v1/users/:#{User.last.uid}/favorites").parsed_response + # Returns a list of pair IDs from Charles' API + end + def self.favorite(pair_id) HTTParty.post(BASE_URL + "/v1/users/:#{User.last.uid}/favorites", body: {"suggestions": pair_id}.to_json) + # Currently not saving the favorite in Charles' API, returns an empty array + raise # work on this end end From 70835c5f9b76138bda815e35ae61ae2b5e06ff45 Mon Sep 17 00:00:00 2001 From: Alysia Brown Date: Sat, 4 Jun 2016 21:15:22 -0700 Subject: [PATCH 31/31] Removed option to favorite a pair from the guest view. --- app/views/suggestions/index.html.erb | 82 ++++++++++++++-------------- lib/TunesTakeoutWrapper.rb | 1 - 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index cfb83c0..e6d6fd1 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -1,47 +1,49 @@

Top Twenty Suggested Pairings

-<% @results.each do |pair_id| %> -

- <% music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> - <% info = Yelp.client.business(music["food_id"]).business %> - <%= info.name %> - <%= info.id %> - <%= info.image_url %> - <%= info.rating %> - <%= info.url %> + <% @results.each do |pair_id| %> +

+ <% music = TunesTakeoutWrapper.suggestion_info(pair_id)["suggestion"] %> + <% info = Yelp.client.business(music["food_id"]).business %> + <%= info.name %> + <%= info.id %> + <%= info.image_url %> + <%= info.rating %> + <%= info.url %> - <% if music["music_type"] == "album" %> - <% album = RSpotify::Album.find(music["music_id"]) %> - <%= album.name %>
- <%= album.uri %>
- <%= album.images[0]["url"] %>
- <%= album.album_type %>
- <%= album.external_urls['spotify'] %> -


+ <% if music["music_type"] == "album" %> + <% album = RSpotify::Album.find(music["music_id"]) %> + <%= album.name %>
+ <%= album.uri %>
+ <%= album.images[0]["url"] %>
+ <%= album.album_type %>
+ <%= album.external_urls['spotify'] %> +


- <% elsif music["music_type"] == "artist" %> - <% artist = RSpotify::Artist.find(music["music_id"]) %> - <%= artist.name %>
- <%= artist.uri %>
- <%= artist.images[0] %>
- <%= artist.type %>
- <%= artist.external_urls['spotify'] %>


+ <% elsif music["music_type"] == "artist" %> + <% artist = RSpotify::Artist.find(music["music_id"]) %> + <%= artist.name %>
+ <%= artist.uri %>
+ <%= artist.images[0] %>
+ <%= artist.type %>
+ <%= artist.external_urls['spotify'] %>


- <% elsif music["music_type"] == "track" %> - <% track = RSpotify::Track.find(music["music_id"]) %> - <%= track.name %>
- <%= track.uri %>
- <%= track.type %>
- <%= track.external_urls['spotify'] %>


+ <% elsif music["music_type"] == "track" %> + <% track = RSpotify::Track.find(music["music_id"]) %> + <%= track.name %>
+ <%= track.uri %>
+ <%= track.type %>
+ <%= track.external_urls['spotify'] %>


- <% else %> - <%= "Invalid music type." %> - <% end %> + <% else %> + <%= "Invalid music type." %> + <% end %> - <%= hidden_field_tag :favorite, TunesTakeoutWrapper.favorite(pair_id) %> - <%= form_tag favorite_path do %> - <%= hidden_field_tag "pair_id", pair_id %> - <%= submit_tag "Favorite" %> - <% end %> -

-<% end %> + <% if current_user %> + <%= hidden_field_tag :favorite, TunesTakeoutWrapper.favorite(pair_id) %> + <%= form_tag favorite_path do %> + <%= hidden_field_tag "pair_id", pair_id %> + <%= submit_tag "Favorite" %> + <% end %> + <% end %> +

+ <% end %> diff --git a/lib/TunesTakeoutWrapper.rb b/lib/TunesTakeoutWrapper.rb index 058b642..8a30b82 100644 --- a/lib/TunesTakeoutWrapper.rb +++ b/lib/TunesTakeoutWrapper.rb @@ -23,7 +23,6 @@ def self.favorites def self.favorite(pair_id) HTTParty.post(BASE_URL + "/v1/users/:#{User.last.uid}/favorites", body: {"suggestions": pair_id}.to_json) # Currently not saving the favorite in Charles' API, returns an empty array - raise # work on this end end