From 9ef022dd7d53ceee74b545b4de29679817724d34 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Mon, 16 May 2016 15:55:22 -0700 Subject: [PATCH 01/45] First commit, pre-bundle --- Gemfile | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 288bb87..c1d342d 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,11 @@ gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc +gem 'httparty' + +gem 'rspotify' + +gem 'yelp', require: 'yelp' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' @@ -33,6 +38,10 @@ 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 'simplecov', '~> 0.11.2' + gem 'pry-rails' + gem "better_errors" + gem "binding_of_caller" end group :development do @@ -42,4 +51,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 - From bba87ea8f239ab4b4acc1e3a45902e4671f4f754 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Mon, 16 May 2016 15:56:32 -0700 Subject: [PATCH 02/45] gitignore update --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 03f3ce7..a1c69cb 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ /test/tmp/ /test/version_tmp/ /tmp/ +/config/secrets.yml # Used by dotenv library to load environment variables. # .env From c86895801384792add95c9dcaa4a31a5d7a4db73 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Mon, 16 May 2016 16:12:11 -0700 Subject: [PATCH 03/45] gitignore fixed hopefully --- Gemfile.lock | 65 +++++++++++++++++++++++++++++++++++++++++++++- config/secrets.yml | 22 ---------------- 2 files changed, 64 insertions(+), 23 deletions(-) delete mode 100644 config/secrets.yml diff --git a/Gemfile.lock b/Gemfile.lock index 6ff0f39..b9fe9a8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,10 +37,15 @@ GEM thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) 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) 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) @@ -50,10 +55,19 @@ GEM coffee-script-source (1.10.0) concurrent-ruby (1.0.2) debug_inspector (0.0.2) + docile (1.1.5) 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) + 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) @@ -63,19 +77,42 @@ 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) minitest (5.8.4) 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 +143,11 @@ 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) sass (3.4.22) sass-rails (5.0.4) railties (>= 4.0.0, < 5.0) @@ -116,6 +158,13 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + simple_oauth (0.3.1) + simplecov (0.11.2) + docile (~> 1.1.0) + json (~> 1.8) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.0) + slop (3.6.0) spring (1.7.1) sprockets (3.6.0) concurrent-ruby (~> 1.0) @@ -136,22 +185,36 @@ GEM binding_of_caller (>= 0.7.2) railties (>= 4.0) sprockets-rails (>= 2.0, < 4.0) + yelp (2.1.2) + faraday (~> 0.8, >= 0.8.0) + faraday_middleware (~> 0.8, >= 0.8.0) + simple_oauth (~> 0.3.1) PLATFORMS ruby DEPENDENCIES + better_errors + binding_of_caller byebug coffee-rails (~> 4.1.0) + httparty jbuilder (~> 2.0) jquery-rails pg (~> 0.15) + pry-rails rails (= 4.2.6) + rspotify sass-rails (~> 5.0) sdoc (~> 0.4.0) + simplecov (~> 0.11.2) spring uglifier (>= 1.3.0) web-console (~> 2.0) + yelp + +RUBY VERSION + ruby 2.3.1p112 BUNDLED WITH - 1.12.3 + 1.12.4 diff --git a/config/secrets.yml b/config/secrets.yml deleted file mode 100644 index be83593..0000000 --- a/config/secrets.yml +++ /dev/null @@ -1,22 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Your secret key is used for verifying the integrity of signed cookies. -# If you change this key, all old signed cookies will become invalid! - -# Make sure the secret is at least 30 characters and all random, -# no regular words or you'll be exposed to dictionary attacks. -# You can use `rake secret` to generate a secure secret key. - -# Make sure the secrets in this file are kept private -# if you're sharing your code publicly. - -development: - secret_key_base: 553149b03d6ba63414be540733245f323d241c1c9a37f3266d1216d93effd901fefb43f0782e4d2b739d61a3de1488c681a90435b22318114f638d9fc6e0f8c1 - -test: - secret_key_base: 0c9e2ff02abb06b715fccfbd8a7fb390a12595cf1c4d8b58cae707903a17a4a58ea0762f4a2125da40cf59b211620b81e702e13d58c4a50e4591f48dcbf1cbaa - -# Do not keep production secrets in the repository, -# instead read values from the environment. -production: - secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> From d37dc0b205a861fb96c87b9bafc1409c48064405 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Mon, 16 May 2016 16:53:21 -0700 Subject: [PATCH 04/45] env update --- Gemfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index c1d342d..bf21fea 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,8 @@ gem 'httparty' gem 'rspotify' gem 'yelp', require: 'yelp' + + # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' @@ -42,12 +44,12 @@ group :development, :test do gem 'pry-rails' gem "better_errors" gem "binding_of_caller" + gem "dotenv-rails" end group :development do # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' - # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end From 16190a52f090e21cfbb347e14cd0d4c98a2bfff8 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Mon, 16 May 2016 16:57:19 -0700 Subject: [PATCH 05/45] fixed gitignore for secrets and env --- .gitignore | 1 - test/fixtures/foods.yml | 7 +++++++ test/fixtures/musics.yml | 9 +++++++++ test/fixtures/users.yml | 11 +++++++++++ test/models/food_test.rb | 7 +++++++ test/models/music_test.rb | 7 +++++++ test/models/user_test.rb | 7 +++++++ 7 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/foods.yml create mode 100644 test/fixtures/musics.yml create mode 100644 test/fixtures/users.yml create mode 100644 test/models/food_test.rb create mode 100644 test/models/music_test.rb create mode 100644 test/models/user_test.rb diff --git a/.gitignore b/.gitignore index a1c69cb..03f3ce7 100644 --- a/.gitignore +++ b/.gitignore @@ -24,7 +24,6 @@ /test/tmp/ /test/version_tmp/ /tmp/ -/config/secrets.yml # Used by dotenv library to load environment variables. # .env diff --git a/test/fixtures/foods.yml b/test/fixtures/foods.yml new file mode 100644 index 0000000..82a42f5 --- /dev/null +++ b/test/fixtures/foods.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + restaurant: MyString + +two: + restaurant: MyString diff --git a/test/fixtures/musics.yml b/test/fixtures/musics.yml new file mode 100644 index 0000000..6f08ce6 --- /dev/null +++ b/test/fixtures/musics.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + object: MyString + type: + +two: + object: MyString + type: diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..2f72a66 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + provider: MyString + uid: MyString + name: MyString + +two: + provider: MyString + uid: MyString + name: MyString 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 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 d98e525d55a3f4a3aa48d64cbd63f3e34dcb88ad Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Mon, 16 May 2016 16:57:45 -0700 Subject: [PATCH 06/45] generated models, foods, musics, users --- app/models/food.rb | 2 ++ app/models/music.rb | 2 ++ app/models/user.rb | 2 ++ 3 files changed, 6 insertions(+) create mode 100644 app/models/food.rb create mode 100644 app/models/music.rb create mode 100644 app/models/user.rb 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/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..4a57cf0 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,2 @@ +class User < ActiveRecord::Base +end From a968ded9636aa5ec578b6dac44fb922db2ba0f07 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Mon, 16 May 2016 17:00:21 -0700 Subject: [PATCH 07/45] made my super secret secrets visible because charles says so --- config/secrets.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 config/secrets.yml diff --git a/config/secrets.yml b/config/secrets.yml new file mode 100644 index 0000000..9de716b --- /dev/null +++ b/config/secrets.yml @@ -0,0 +1,22 @@ +# Be sure to restart your server when you modify this file. + +# Your secret key is used for verifying the integrity of signed cookies. +# If you change this key, all old signed cookies will become invalid! + +# Make sure the secret is at least 30 characters and all random, +# no regular words or you'll be exposed to dictionary attacks. +# You can use `rake secret` to generate a secure secret key. + +# Make sure the secrets in this file are kept private +# if you're sharing your code publicly. + +development: + secret_key_base: 79252673745894b33179f707df65b3248f272f2e46bfa2078120f8c252f7e59331033872fe13be2b2259f0b01fc7d73d6d8e58c3b54969ff67d348bef69f959f + +test: + secret_key_base: 51273e88b99c66c8b3c1bfec07acd6decb4c824853659d0323bf3fd0927909240544f0f6a6bb7f2bed8e8761fd8439a43ae31f2a06571c8ad95282f73ed15c84 + +# Do not keep production secrets in the repository, +# instead read values from the environment. +production: + secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> From ec7ed4069dbbf6953484ff8a4b5f1e5eec36b3a9 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Mon, 16 May 2016 17:00:52 -0700 Subject: [PATCH 08/45] migrations created --- db/migrate/20160516231845_create_users.rb | 11 +++++++++++ db/migrate/20160516232252_create_foods.rb | 9 +++++++++ db/migrate/20160516232327_create_musics.rb | 10 ++++++++++ 3 files changed, 30 insertions(+) create mode 100644 db/migrate/20160516231845_create_users.rb create mode 100644 db/migrate/20160516232252_create_foods.rb create mode 100644 db/migrate/20160516232327_create_musics.rb diff --git a/db/migrate/20160516231845_create_users.rb b/db/migrate/20160516231845_create_users.rb new file mode 100644 index 0000000..fc91a6e --- /dev/null +++ b/db/migrate/20160516231845_create_users.rb @@ -0,0 +1,11 @@ +class CreateUsers < ActiveRecord::Migration + def change + create_table :users do |t| + t.string :provider, null: false + t.string :uid, null: false + t.string :name, null: false + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160516232252_create_foods.rb b/db/migrate/20160516232252_create_foods.rb new file mode 100644 index 0000000..7e72f05 --- /dev/null +++ b/db/migrate/20160516232252_create_foods.rb @@ -0,0 +1,9 @@ +class CreateFoods < ActiveRecord::Migration + def change + create_table :foods do |t| + t.string :restaurant + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160516232327_create_musics.rb b/db/migrate/20160516232327_create_musics.rb new file mode 100644 index 0000000..6df76b5 --- /dev/null +++ b/db/migrate/20160516232327_create_musics.rb @@ -0,0 +1,10 @@ +class CreateMusics < ActiveRecord::Migration + def change + create_table :musics do |t| + t.string :object + t.string :type + + t.timestamps null: false + end + end +end From ddebf58abb54306d05546320aca826b782bb087e Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Mon, 16 May 2016 17:06:24 -0700 Subject: [PATCH 09/45] updated gitignore, BEGIN IGNORING --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 03f3ce7..5eadf6f 100644 --- a/.gitignore +++ b/.gitignore @@ -26,7 +26,7 @@ /tmp/ # Used by dotenv library to load environment variables. -# .env +#.env ## Specific to RubyMotion: .dat* From 6324e9537d8e5075d4d3ebb1f6d5a1eab1eebceb Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Tue, 17 May 2016 11:20:39 -0700 Subject: [PATCH 10/45] TunesTakeoutWrapper complete --- lib/TunesTakeoutWrapper.rb | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lib/TunesTakeoutWrapper.rb diff --git a/lib/TunesTakeoutWrapper.rb b/lib/TunesTakeoutWrapper.rb new file mode 100644 index 0000000..f03743d --- /dev/null +++ b/lib/TunesTakeoutWrapper.rb @@ -0,0 +1,48 @@ +require 'httparty' + +class TunesTakeout + BASE_URL = "https://tunes-takeout-api.herokuapp.com/" + attr_reader :suggestions + + def initialize(data) + if data["suggestions"] + @suggestions = data["suggestions"] + elsif data["suggestion"] + @suggestion = data["suggestion"] + else + @errors = "No data returned" + end + end + # + # def favorite_move + # HTTParty.get(moves.sample["move"]["url"]).parsed_response + # end + + def self.search(query) + data = HTTParty.get(BASE_URL + "/v1/suggestions/search?query=#{query}").parsed_response + self.new(data) + end + + def self.limited_search(query, n) + data = HTTParty.get(BASE_URL + "/v1/suggestions/search?query=#{query}&limit=#{n}").parsed_response + self.new(data) + end + + def self.random(query, n, seed = random(99999)) + data = HTTParty.get(BASE_URL + "/v1/suggestions/search?query=#{query}&limit=#{n}&seed=#{seed}").parsed_response + self.new(data) + end + + def self.specific_suggestion(suggestion_id) + data = HTTParty.get(BASE_URL + "/v1/suggestions/#{suggestion_id}").parsed_response + self.new(data) + end + + def self.user_favorites(user_id) #returns "suggestions":["VzoxXvLQUmT7dPJ5", "VzoxXvLQUmT7dPJ6"] + data = HTTParty.get(BASE_URL + "/v1/users/#{user_id}/favorites").parsed_response + self.new(data) + end + + private + +end From 04e36545f10727520407e6449435098e4eebe1c7 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Tue, 17 May 2016 11:25:10 -0700 Subject: [PATCH 11/45] Sessions controller with skeleton actions --- app/controllers/sessions_controller.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 app/controllers/sessions_controller.rb diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..5aee585 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,18 @@ +class SessionsController < ApplicationController + + def new + #invoke OAuth sign-in (link?) + render :new + end + + def create + @spotify_user = 0 #spotify user information + @user = @spotify_user.id ||= User.new(@spotify_user) + session[:user_id] = @user.id + end + + def destroy + session[:user_id] = nil + end + +end From a9b0318f1e04b4cc8bb033a7461381683951e119 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Tue, 17 May 2016 11:31:45 -0700 Subject: [PATCH 12/45] Suggestions controller with skeleton actions --- app/controllers/suggestions_controller.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 app/controllers/suggestions_controller.rb diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb new file mode 100644 index 0000000..ddb1b6c --- /dev/null +++ b/app/controllers/suggestions_controller.rb @@ -0,0 +1,9 @@ +class SuggestionsController < ApplicationController + + def index + #shows top 20 suggestions, ranked by total number of favorites + end +favorites: shows all suggestions favorited by the signed-in User +favorite: adds a suggestion into the favorite list for the signed-in User. This requires interaction with the Tunes & Takeout API. +unfavorite: removes a suggestion from the favorite list for the signed-in User. This requires interaction with the Tunes & Takeout API. +end From 18cc8abcedd38e01e5e5ad659ab2be7094f02f44 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Tue, 17 May 2016 11:32:28 -0700 Subject: [PATCH 13/45] test controllers --- test/controllers/sessions_controller_test.rb | 7 +++++++ test/controllers/suggestions_controller_test.rb | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 test/controllers/sessions_controller_test.rb create mode 100644 test/controllers/suggestions_controller_test.rb 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 From d4c34477a7594f5546b69fcf470c002092cdefa8 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Tue, 17 May 2016 11:38:28 -0700 Subject: [PATCH 14/45] incomplete but started suggestions controller --- app/controllers/suggestions_controller.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index ddb1b6c..aad5eb8 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -3,6 +3,14 @@ class SuggestionsController < ApplicationController def index #shows top 20 suggestions, ranked by total number of favorites end + + def favorites + @user = #set user, and set user id + @favorites = ::TunesTakeoutWrapper.user_favorites(user_id) #returns array of pairing id's, FIX CALL + end + + + favorites: shows all suggestions favorited by the signed-in User favorite: adds a suggestion into the favorite list for the signed-in User. This requires interaction with the Tunes & Takeout API. unfavorite: removes a suggestion from the favorite list for the signed-in User. This requires interaction with the Tunes & Takeout API. From f98cd01ccd0bd87cbc359e442a5ad5fe0d29170c Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Tue, 17 May 2016 16:01:54 -0700 Subject: [PATCH 15/45] Gemfile updates for omniauth --- .gitignore | 3 ++- Gemfile | 6 ++++++ Gemfile.lock | 5 +++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5eadf6f..2b147d1 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ /log/* !/log/.keep /tmp +.DS_Store # From https://github.com/github/gitignore/blob/master/Ruby.gitignore *.gem @@ -26,7 +27,7 @@ /tmp/ # Used by dotenv library to load environment variables. -#.env +.env ## Specific to RubyMotion: .dat* diff --git a/Gemfile b/Gemfile index bf21fea..b2987f1 100644 --- a/Gemfile +++ b/Gemfile @@ -27,6 +27,12 @@ gem 'rspotify' gem 'yelp', require: 'yelp' +gem "omniauth" + +gem "omniauth-github" + +gem 'omniauth-oauth2', '~> 1.3.1' + # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' diff --git a/Gemfile.lock b/Gemfile.lock index b9fe9a8..eb65d5e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,6 +56,10 @@ GEM concurrent-ruby (1.0.2) debug_inspector (0.0.2) docile (1.1.5) + 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) @@ -198,6 +202,7 @@ DEPENDENCIES binding_of_caller byebug coffee-rails (~> 4.1.0) + dotenv-rails httparty jbuilder (~> 2.0) jquery-rails From fd84e9e6944d7e769c04ba055446888f3f322186 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Tue, 17 May 2016 16:02:53 -0700 Subject: [PATCH 16/45] Controller updates with known omniauth information. Still needs tunestakeoutwrapper functionality around favorites' --- app/controllers/sessions_controller.rb | 24 +++++++++++++++++------ app/controllers/suggestions_controller.rb | 9 +++++---- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 5aee585..d0cb957 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,18 +1,30 @@ class SessionsController < ApplicationController + skip_before_action :require_login, only: [:new, :create] def new - #invoke OAuth sign-in (link?) - render :new + #probs not right - direct link on layout/nav? + render '/auth/spotify' end def create - @spotify_user = 0 #spotify user information - @user = @spotify_user.id ||= User.new(@spotify_user) - session[:user_id] = @user.id + 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 save the user" + end end + def destroy - session[:user_id] = nil + session.delete :user_id + redirect_to root_path end end diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index aad5eb8..07aa488 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -1,3 +1,4 @@ +require '/lib/tunes_takeout_wrapper' class SuggestionsController < ApplicationController def index @@ -6,12 +7,12 @@ def index def favorites @user = #set user, and set user id - @favorites = ::TunesTakeoutWrapper.user_favorites(user_id) #returns array of pairing id's, FIX CALL + @favorites = TunesTakeout.user_favorites(user_id) #returns array of pairing id's, FIX CALL end -favorites: shows all suggestions favorited by the signed-in User -favorite: adds a suggestion into the favorite list for the signed-in User. This requires interaction with the Tunes & Takeout API. -unfavorite: removes a suggestion from the favorite list for the signed-in User. This requires interaction with the Tunes & Takeout API. +# favorites: shows all suggestions favorited by the signed-in User +# favorite: adds a suggestion into the favorite list for the signed-in User. This requires interaction with the Tunes & Takeout API. +# unfavorite: removes a suggestion from the favorite list for the signed-in User. This requires interaction with the Tunes & Takeout API. end From de0ba2d4ea5f088a50b874cf8fa2fa8a4aec9e0e Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Tue, 17 May 2016 16:03:19 -0700 Subject: [PATCH 17/45] User model for omniauth functionality --- app/models/user.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 4a57cf0..7cb4f69 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,2 +1,16 @@ class User < ActiveRecord::Base + validates :name, :uid, :provider, presence: true + + def self.find_or_create_from_omniauth(auth_hash) + user = User.find_by(uid: auth_hash["uid"], provider: auth_hash["provider"]) + if user.nil? + user = User.new(uid: auth_hash["uid"], provider: auth_hash["provider"], name: auth_hash["name"]) + if user.save + return user + else + return nil + end + end + end + end From e824a6154e4f1abf172c8da776a7f44d2723341c Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Tue, 17 May 2016 16:03:49 -0700 Subject: [PATCH 18/45] Route and rename lib/tunestakeoutwrapper.rb --- 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/helpers/sessions_helper.rb | 2 ++ app/helpers/suggestions_helper.rb | 2 ++ config/initializers/omniauth.rb | 3 +++ config/routes.rb | 3 +++ lib/{TunesTakeoutWrapper.rb => tunes_takeout_wrapper.rb} | 0 9 files changed, 22 insertions(+) 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/helpers/sessions_helper.rb create mode 100644 app/helpers/suggestions_helper.rb create mode 100644 config/initializers/omniauth.rb rename lib/{TunesTakeoutWrapper.rb => tunes_takeout_wrapper.rb} (100%) 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/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/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 0000000..cc0c5f5 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,3 @@ +Rails.application.config.middleware.use OmniAuth::Builder do + provider :spotify, ENV["SPOTIFY_CLIENT_ID"], ENV["SPOTIFY_CLIENT_SECRET"] +end diff --git a/config/routes.rb b/config/routes.rb index 3f66539..5e319a7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Rails.application.routes.draw do + + # get '/auth/:provider' => 'session#login' + get '/auth/:provider/callback' => 'session#create' # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/lib/TunesTakeoutWrapper.rb b/lib/tunes_takeout_wrapper.rb similarity index 100% rename from lib/TunesTakeoutWrapper.rb rename to lib/tunes_takeout_wrapper.rb From b3d1b051d1dc4e576f9f8bf56fcc0d97e7096d37 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Wed, 18 May 2016 09:19:54 -0700 Subject: [PATCH 19/45] suggestion index view, migrations ran -> schema --- Gemfile.lock | 8 ++++- app/controllers/application_controller.rb | 14 ++++++++ app/controllers/sessions_controller.rb | 5 +-- app/controllers/suggestions_controller.rb | 2 +- app/models/user.rb | 4 +-- app/views/layouts/application.html.erb | 13 +++++++- app/views/suggestions/index.html.erb | 1 + config/application.rb | 1 + config/routes.rb | 3 +- db/schema.rb | 40 +++++++++++++++++++++++ 10 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 app/views/suggestions/index.html.erb create mode 100644 db/schema.rb diff --git a/Gemfile.lock b/Gemfile.lock index eb65d5e..c289227 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -107,7 +107,10 @@ GEM omniauth (1.3.1) hashie (>= 1.2, < 4) rack (>= 1.0, < 3) - omniauth-oauth2 (1.4.0) + omniauth-github (1.1.2) + omniauth (~> 1.0) + omniauth-oauth2 (~> 1.1) + omniauth-oauth2 (1.3.1) oauth2 (~> 1.0) omniauth (~> 1.2) pg (0.18.4) @@ -206,6 +209,9 @@ DEPENDENCIES httparty jbuilder (~> 2.0) jquery-rails + omniauth + omniauth-github + omniauth-oauth2 (~> 1.3.1) pg (~> 0.15) pry-rails rails (= 4.2.6) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..e9dd4fd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,18 @@ 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 + + # before_action :require_login + + def current_user + @current_user ||= User.find_by(id: session[:user_id]) + end + + def require_login + if current_user.nil? + flash[:error] = "You must be logged in to view this section" + redirect_to root_path + end + end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index d0cb957..5e34d9a 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -2,14 +2,15 @@ class SessionsController < ApplicationController skip_before_action :require_login, only: [:new, :create] def new - #probs not right - direct link on layout/nav? - render '/auth/spotify' + end def create auth_hash = request.env['omniauth.auth'] if auth_hash["uid"] @user = User.find_or_create_from_omniauth(auth_hash) + + binding.pry if @user session[:user_id] = user.id redirect_to root_path diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 07aa488..32c275c 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -1,4 +1,4 @@ -require '/lib/tunes_takeout_wrapper' +# require '/lib/tunes_takeout_wrapper' class SuggestionsController < ApplicationController def index diff --git a/app/models/user.rb b/app/models/user.rb index 7cb4f69..68877fe 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,9 +2,9 @@ class User < ActiveRecord::Base validates :name, :uid, :provider, presence: true def self.find_or_create_from_omniauth(auth_hash) - user = User.find_by(uid: auth_hash["uid"], provider: auth_hash["provider"]) + user = User.find_by(uid: auth_hash["info"]["id"], provider: auth_hash["provider"]) if user.nil? - user = User.new(uid: auth_hash["uid"], provider: auth_hash["provider"], name: auth_hash["name"]) + user = User.new(uid: auth_hash["info"]["id"], provider: auth_hash["provider"], name: auth_hash["info"]["display_name"]) if user.save return user else diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 509d1a2..7fad871 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,12 +1,23 @@ - TunesTakeout + TakeoutTunes <%= stylesheet_link_tag 'application', media: 'all' %> <%= javascript_include_tag 'application' %> <%= csrf_meta_tags %> +
+ +
<%= yield %> diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb new file mode 100644 index 0000000..c12efb3 --- /dev/null +++ b/app/views/suggestions/index.html.erb @@ -0,0 +1 @@ +

This is the suggestions index page

diff --git a/config/application.rb b/config/application.rb index 9b37a86..eb05571 100644 --- a/config/application.rb +++ b/config/application.rb @@ -8,6 +8,7 @@ module TunesTakeout class Application < Rails::Application + RSpotify::authenticate("", "") # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. diff --git a/config/routes.rb b/config/routes.rb index 5e319a7..261e1ef 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,8 @@ Rails.application.routes.draw do + root 'suggestions#index' # get '/auth/:provider' => 'session#login' - get '/auth/:provider/callback' => 'session#create' + get '/auth/:provider/callback' => 'sessions#create' # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..206f970 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,40 @@ +# 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: 20160516232327) 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.string "restaurant" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "musics", force: :cascade do |t| + t.string "object" + t.string "type" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "users", force: :cascade do |t| + t.string "provider", null: false + t.string "uid", null: false + t.string "name", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end From 868ee96aafbea7d6a712e008a7323bbdda5961ea Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Wed, 18 May 2016 09:42:30 -0700 Subject: [PATCH 20/45] pre-re-install, thanks rails/spotify fuck --- Gemfile | 7 +++++-- Gemfile.lock | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index b2987f1..09b2074 100644 --- a/Gemfile +++ b/Gemfile @@ -24,7 +24,8 @@ gem 'sdoc', '~> 0.4.0', group: :doc gem 'httparty' gem 'rspotify' - + gem 'rest-client' + gem 'yelp', require: 'yelp' gem "omniauth" @@ -34,6 +35,8 @@ gem "omniauth-github" gem 'omniauth-oauth2', '~> 1.3.1' + + # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' @@ -57,5 +60,5 @@ group :development do # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring - gem 'spring' + # gem 'spring' end diff --git a/Gemfile.lock b/Gemfile.lock index c289227..5b8e83f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -150,6 +150,8 @@ GEM rake (11.1.2) rdoc (4.2.2) json (~> 1.4) + rest-client (1.6.7) + mime-types (>= 1.16) rest_client (1.8.3) netrc (~> 0.7.7) rspotify (1.10.0) @@ -172,7 +174,6 @@ GEM simplecov-html (~> 0.10.0) simplecov-html (0.10.0) slop (3.6.0) - spring (1.7.1) sprockets (3.6.0) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -215,11 +216,11 @@ DEPENDENCIES pg (~> 0.15) pry-rails rails (= 4.2.6) + rest-client rspotify sass-rails (~> 5.0) sdoc (~> 0.4.0) simplecov (~> 0.11.2) - spring uglifier (>= 1.3.0) web-console (~> 2.0) yelp From 5350af35116281513de86fddfa6063c8b9ecdb8c Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Thu, 19 May 2016 10:41:55 -0700 Subject: [PATCH 21/45] Gemfile final(ish) in attempt to fix rest_client deprecation --- Gemfile | 3 +-- Gemfile.lock | 3 --- app/views/suggestions/index.html.erb | 1 + 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 09b2074..5ae6920 100644 --- a/Gemfile +++ b/Gemfile @@ -24,8 +24,7 @@ gem 'sdoc', '~> 0.4.0', group: :doc gem 'httparty' gem 'rspotify' - gem 'rest-client' - + gem 'yelp', require: 'yelp' gem "omniauth" diff --git a/Gemfile.lock b/Gemfile.lock index 5b8e83f..a276890 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -150,8 +150,6 @@ GEM rake (11.1.2) rdoc (4.2.2) json (~> 1.4) - rest-client (1.6.7) - mime-types (>= 1.16) rest_client (1.8.3) netrc (~> 0.7.7) rspotify (1.10.0) @@ -216,7 +214,6 @@ DEPENDENCIES pg (~> 0.15) pry-rails rails (= 4.2.6) - rest-client rspotify sass-rails (~> 5.0) sdoc (~> 0.4.0) diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index c12efb3..8020549 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -1 +1,2 @@

This is the suggestions index page

+<%= params%> From 92d2db6b8cd5f1959baf04413f27e03f186151e2 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Thu, 19 May 2016 10:42:30 -0700 Subject: [PATCH 22/45] All functionality around logging in and out complete and working --- app/controllers/application_controller.rb | 2 +- app/controllers/sessions_controller.rb | 6 ++---- app/controllers/suggestions_controller.rb | 2 +- app/models/user.rb | 9 +++++---- config/application.rb | 2 +- config/routes.rb | 1 + 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e9dd4fd..f0b3e08 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception helper_method :current_user - # before_action :require_login + before_action :require_login def current_user @current_user ||= User.find_by(id: session[:user_id]) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 5e34d9a..b40e57e 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -7,12 +7,10 @@ def new def create auth_hash = request.env['omniauth.auth'] - if auth_hash["uid"] + if auth_hash["info"]["id"] @user = User.find_or_create_from_omniauth(auth_hash) - - binding.pry if @user - session[:user_id] = user.id + session[:user_id] = @user.id redirect_to root_path else redirect_to root_path, notice: "Failed to save the user" diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 32c275c..8f0b68a 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -1,6 +1,6 @@ # require '/lib/tunes_takeout_wrapper' class SuggestionsController < ApplicationController - + skip_before_action :require_login, only: :index def index #shows top 20 suggestions, ranked by total number of favorites end diff --git a/app/models/user.rb b/app/models/user.rb index 68877fe..40d8782 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,16 +1,17 @@ class User < ActiveRecord::Base - validates :name, :uid, :provider, presence: true + validates :name, :id, :provider, presence: true def self.find_or_create_from_omniauth(auth_hash) - user = User.find_by(uid: auth_hash["info"]["id"], provider: auth_hash["provider"]) + user = User.find_by(uid: auth_hash["uid"] ||=auth_hash["info"]["id"], provider: auth_hash["provider"]) if user.nil? - user = User.new(uid: auth_hash["info"]["id"], provider: auth_hash["provider"], name: auth_hash["info"]["display_name"]) - if user.save + user = User.new(uid: auth_hash["uid"] ||=auth_hash["info"]["id"], provider: auth_hash["provider"], name: auth_hash["info"]["display_name"]) + if user.save! return user else return nil end end + return user end end diff --git a/config/application.rb b/config/application.rb index eb05571..5316b1a 100644 --- a/config/application.rb +++ b/config/application.rb @@ -8,7 +8,7 @@ module TunesTakeout class Application < Rails::Application - RSpotify::authenticate("", "") + # RSpotify::authenticate(ENV["SPOTIFY_CLIENT_ID"], ENV["SPOTIFY_CLIENT_SECRET"]) # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. diff --git a/config/routes.rb b/config/routes.rb index 261e1ef..131163b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ root 'suggestions#index' # get '/auth/:provider' => 'session#login' get '/auth/:provider/callback' => 'sessions#create' + delete '/logout' => 'sessions#destroy', as: "logout" # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 670d66c088fba2a7e00f00ccb47f32bdb9b93552 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Thu, 19 May 2016 14:40:12 -0700 Subject: [PATCH 23/45] added a bunch of non-functional tests because I have no idea what I'm doing --- app/controllers/suggestions_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 8f0b68a..9d2b824 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -6,7 +6,7 @@ def index end def favorites - @user = #set user, and set user id + @user = current_user @favorites = TunesTakeout.user_favorites(user_id) #returns array of pairing id's, FIX CALL end From bf152dc86c41cfa7d038f01a5341902d582b4b26 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Fri, 20 May 2016 12:02:14 -0700 Subject: [PATCH 24/45] Gemfile with reporters, minitest stuff, other testing junk --- Gemfile | 4 ++++ Gemfile.lock | 26 ++++++++++++++++++++++++++ config/application.rb | 3 +++ 3 files changed, 33 insertions(+) diff --git a/Gemfile b/Gemfile index 5ae6920..1eb3904 100644 --- a/Gemfile +++ b/Gemfile @@ -53,6 +53,10 @@ group :development, :test do gem "better_errors" gem "binding_of_caller" gem "dotenv-rails" + # gem 'vcr' + gem "minitest-vcr" + gem "webmock" + gem "minitest-reporters" end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index a276890..9b1d353 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,6 +36,8 @@ 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) better_errors (2.1.1) coderay (>= 1.0.0) @@ -54,6 +56,8 @@ 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) docile (1.1.5) dotenv (2.1.1) @@ -68,6 +72,7 @@ GEM faraday (>= 0.7.4, < 0.10) globalid (0.3.6) activesupport (>= 4.1.0) + hashdiff (0.3.0) hashie (3.4.4) httparty (0.13.7) json (~> 1.8) @@ -91,7 +96,18 @@ GEM 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) @@ -155,6 +171,8 @@ GEM 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) @@ -186,11 +204,16 @@ 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) @@ -208,6 +231,8 @@ DEPENDENCIES httparty jbuilder (~> 2.0) jquery-rails + minitest-reporters + minitest-vcr omniauth omniauth-github omniauth-oauth2 (~> 1.3.1) @@ -220,6 +245,7 @@ DEPENDENCIES simplecov (~> 0.11.2) uglifier (>= 1.3.0) web-console (~> 2.0) + webmock yelp RUBY VERSION diff --git a/config/application.rb b/config/application.rb index 5316b1a..086fdaf 100644 --- a/config/application.rb +++ b/config/application.rb @@ -23,5 +23,8 @@ class Application < Rails::Application # Do not swallow errors in after_commit/after_rollback callbacks. config.active_record.raise_in_transactional_callbacks = true + + config.autoload_paths += %W(#{config.root}/lib) + config.autoload_paths += %W(#{config.root}/extras) end end From 8cddc20d7b614b6c5336edb5a1772878adb930e5 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Fri, 20 May 2016 12:04:04 -0700 Subject: [PATCH 25/45] favorites in controller --- app/controllers/suggestions_controller.rb | 30 +++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 9d2b824..9c10654 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -2,12 +2,38 @@ class SuggestionsController < ApplicationController skip_before_action :require_login, only: :index def index + @top = TunesTakeoutWrapper.top(4) #shows top 20 suggestions, ranked by total number of favorites + @user = current_user + # if current_user + # @favorites = TunesTakeoutWrapper.user_favorites(@user.user_id) + # end + end + + def favorites #display user favorites + @user = current_user + @favorites = TunesTakeoutWrapper.user_favorites(@user.user_id) #returns array of pairing id's, FIX CALL + end + + def favorite + @user = current_user + @suggestion = TunesTakeoutWrapper.favorite(@user.user_id, suggestion_id) + end + + def unfavorite + @user = current_user + @suggestion = TunesTakeoutWrapper.unfavorite(@user.user_id, suggestion_id) end - def favorites + def search @user = current_user - @favorites = TunesTakeout.user_favorites(user_id) #returns array of pairing id's, FIX CALL + @suggestions = TunesTakeoutWrapper.search(params[:query]).suggestions + @suggestions.each do |suggestion_hash| + food = suggestion_hash["food_id"] + suggestion_hash["food_id"]= Food.retrieve(food) + suggestion_hash["music_id"] = Music.retrieve(suggestion_hash["music_id"], suggestion_hash["music_type"]) + end + render :results end From 69f285a3d27974b658036c2ba1f419b18e4c5742 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Fri, 20 May 2016 12:05:10 -0700 Subject: [PATCH 26/45] Application controller, maybe unncessary methods to extract food and music from pairing'' --- app/controllers/application_controller.rb | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f0b3e08..85d94ce 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -16,4 +16,30 @@ def require_login redirect_to root_path end end + + def get_food(suggestions) + if suggestions.count == 1 + food = Food.find(id) + end + food_array = [] + suggestions.each do |hash| + hash["food_id"] = id + food = Food.find(id) + food_array << food + end + return food_array + end + + def get_music(suggestions) + if suggestions.count == 1 + music = Music.find(id) + end + music_array = [] + suggestions.each do |hash| + hash["music_id"] = id + music = Music.find(id) + music_array << music + end + return music_array + end end From aaf439849dd9eaf8a56150491cf066e4a3e6358c Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Fri, 20 May 2016 12:06:56 -0700 Subject: [PATCH 27/45] changing yelp.rb and food.rb to function correctly and retrieve instances of food/music --- app/models/food.rb | 13 ++++++++++++- app/models/music.rb | 21 ++++++++++++++++++++- lib/tunes_takeout_wrapper.rb | 17 ++++++++++++++--- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/app/models/food.rb b/app/models/food.rb index 3f58467..347d7cc 100644 --- a/app/models/food.rb +++ b/app/models/food.rb @@ -1,2 +1,13 @@ -class Food < ActiveRecord::Base +class Food +attr_reader :food + + def initialize(food) + @food = food + end + + def self.retrieve(id) + # food = HTTParty.get(YELP_URL + "/v2/business/#{id}") + food = Yelp.client.business(id).business + Food.new(food) + end end diff --git a/app/models/music.rb b/app/models/music.rb index 427c9c2..e78579f 100644 --- a/app/models/music.rb +++ b/app/models/music.rb @@ -1,2 +1,21 @@ -class Music < ActiveRecord::Base +class Music + SPOTIFY_URL = "https://api.spotify.com" + + def initialize(music) + @music = music + end + + def self.retrieve(id, type) + if type.downcase == "track" + music = RSpotify::Track.find(id) + elsif type.downcase == "album" + music = RSpotify::Album.find(id) + elsif type.downcase == "artist" + music = RSpotify::Artist.find(id) + else + music = "OTHER TYPE" + end + # music = HTTParty.get(SPOTIFY_URL + "/v1/#{type}/#{id}").parsed_response + Music.new(music) + end end diff --git a/lib/tunes_takeout_wrapper.rb b/lib/tunes_takeout_wrapper.rb index f03743d..3646ed4 100644 --- a/lib/tunes_takeout_wrapper.rb +++ b/lib/tunes_takeout_wrapper.rb @@ -1,8 +1,8 @@ require 'httparty' -class TunesTakeout +class TunesTakeoutWrapper BASE_URL = "https://tunes-takeout-api.herokuapp.com/" - attr_reader :suggestions + attr_reader :suggestions, :suggestion def initialize(data) if data["suggestions"] @@ -43,6 +43,17 @@ def self.user_favorites(user_id) #returns "suggestions":["VzoxXvLQUmT7dPJ5", "Vz self.new(data) end - private + def self.favorite(user_id, suggestion_id) + HTTParty.post(BASE_URL + "/v1/users/#{user_id}/favorites", { body: {"suggestion": suggestion_id}}.to_json) + end + + def self.unfavorite(user_id, suggestion_id) + HTTParty.delete(BASE_URL + "/v1/users/#{user_id}/favorites", { body: {"suggestion": suggestion_id}}.to_json) + end + + def self.top(limit = 20) + data = HTTParty.get(BASE_URL + "/v1/suggestions/top?limit=#{limit}").parsed_response + self.new(data) + end end From 42335334e67a468c056033da11966f261a8d42b3 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Fri, 20 May 2016 12:08:07 -0700 Subject: [PATCH 28/45] controller method to retrieve top pairings --- app/views/suggestions/index.html.erb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index 8020549..4ff0983 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -1,2 +1,12 @@

This is the suggestions index page

<%= params%> +<%= @top%> + +
+ <%= form_tag search_path(:query), method: 'get', class: 'navbar-form', role: 'search' do %> +
+ <%= text_field_tag :query, params[:query], size: 150, placeholder: "Search", class: "form-control"%> +
+ <%= submit_tag 'Search', class: 'btn btn-success' %> + <% end %> +
From 40ec8704d005791a37efa94f66d578883563a2cf Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Fri, 20 May 2016 12:08:41 -0700 Subject: [PATCH 29/45] Updated routes for suggestions, search results, revisiting specific suggestions --- config/routes.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index 131163b..6b575bf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,12 @@ # get '/auth/:provider' => 'session#login' get '/auth/:provider/callback' => 'sessions#create' delete '/logout' => 'sessions#destroy', as: "logout" + get '/search/:query' => 'suggestions#search', as: "search" + get '/suggestion/:id' => 'suggestions#pair', as: "specific_suggestion" + get '/user/favorites' => 'suggestions#favorites', as: "favorites" + post '/user/suggestion' => 'suggestions#favorite', as: "add_fave" + delete '/user/suggestion' => 'suggestions#unfavorite', as: "unfave" + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From b6c832b4f2f948ef307dbe686e0a60b8d5bb4780 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Fri, 20 May 2016 12:09:11 -0700 Subject: [PATCH 30/45] Completely useless testing garbage that was a huge waste of time. --- test/fixtures/users.yml | 16 ++++++------- test/models/user_test.rb | 52 +++++++++++++++++++++++++++++++++++++--- test/test_helper.rb | 16 +++++++++++++ 3 files changed, 73 insertions(+), 11 deletions(-) diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 2f72a66..451c27a 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,11 +1,11 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -one: - provider: MyString - uid: MyString - name: MyString +known_user: + provider: spotify + uid: known_user + name: known_user -two: - provider: MyString - uid: MyString - name: MyString +spotiy_unknown: + provider: spotify + name: testusertwo + uid: fgdo235 diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 82f61e0..92c2b5c 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,7 +1,53 @@ require 'test_helper' class UserTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + + describe "API" do + before do + @suggestions = TunesTakeout.search(1) + end + + it "has the right data type for an array of suggestions", :vcr do + assert_instance_of Array, @suggestions + end + + it "isn't an empty array of suggestions", :vcr do + refute @suggestions.empty? + end + + it "has a hash in the suggestion array element", :vcr do + assert_instance_of Hash, @suggestions[1] + end + end + + + def setup + # @known = OmniAuth.config.mock_auth[:spotify_known] + @known = { "provider" => 'spotify', "info" => { "id" => "known_user", "display_name" => "known user" } } + @unknown = OmniAuth.config.mock_auth[:spotify_unknown] + @unknown_with_uid = OmniAuth.config.mock_auth[:spotify_uid] + end + + test "can find an existing user given an oauth spotify hash" do + assert_equal users(:known_user), User.find_or_create_from_omniauth(@known) + end + + test "can make a new user given the oauth spotify hash of an unknown user" do + assert_difference 'User.count', 1 do + @user = User.find_or_create_from_omniauth @unknown + end + end + + test "uses oauth data to set user name, provider and uid for new users" do + user = User.find_or_create_from_omniauth @unknown + + assert_equal @unknown['info']['display_name'], user.name + assert_equal @unknown['provider'], user.provider + assert_equal @unknown['info']['id'], user.uid + end + + test "prefers a top-level uid over a nested id when creating an oauth user" do + user = User.find_or_create_from_omniauth @unknown_with_uid + assert_equal @unknown_with_uid['uid'], user.uid + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 92e39b2..9989d9a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,6 +2,22 @@ require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' +require 'minitest/spec' +require "minispec-metadata" +require 'vcr' +require 'minitest-vcr' +require 'webmock/minitest' +require "minitest/reporters" + + +VCR.configure do |c| + c.cassette_library_dir = 'test/cassettes' + c.hook_into :webmock +end + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +MinitestVcr::Spec.configure! + class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all From 86954cb9f5242b24fc2e1f4733b851835d2ff630 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Fri, 20 May 2016 12:11:17 -0700 Subject: [PATCH 31/45] Search results view - unstyles, retrieving results correctly but no display info yet --- app/views/suggestions/results.html.erb | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 app/views/suggestions/results.html.erb diff --git a/app/views/suggestions/results.html.erb b/app/views/suggestions/results.html.erb new file mode 100644 index 0000000..cda782a --- /dev/null +++ b/app/views/suggestions/results.html.erb @@ -0,0 +1,3 @@ +<% @suggestions.each do |hash|%> + <%= hash %> +<%end%> From 621b94119c43f73f2d209ce6c4d607d02af8d7de Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Fri, 20 May 2016 15:05:26 -0700 Subject: [PATCH 32/45] Fixed music model to return most of necessary information. Fucking finally - only took 4 hours --- app/models/music.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/models/music.rb b/app/models/music.rb index e78579f..eb6d1c0 100644 --- a/app/models/music.rb +++ b/app/models/music.rb @@ -1,7 +1,12 @@ class Music - SPOTIFY_URL = "https://api.spotify.com" - def initialize(music) + def initialize(music, type) + @name = music.name + @artist = music.artists unless type == "artists" + @url = music.artists[0].external_urls["spotify"] + # @tracks = music.tracks_cache + @album = music.album unless type == "album" + # @cover = cover_art(music) @music = music end @@ -16,6 +21,11 @@ def self.retrieve(id, type) music = "OTHER TYPE" end # music = HTTParty.get(SPOTIFY_URL + "/v1/#{type}/#{id}").parsed_response - Music.new(music) + Music.new(music, type) + end + + def cover_art(music) + cover_art = music.images[0]["url"] + # cover_art = music.images[1]["url"] end end From d708f29e1a70c71ec2fd30b90a9834e415ca8b1d Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Fri, 20 May 2016 16:47:51 -0700 Subject: [PATCH 33/45] BARSAVE yardhouse style --- app/models/food.rb | 2 +- app/models/music.rb | 6 +++--- app/views/suggestions/results.html.erb | 8 +++++++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/models/food.rb b/app/models/food.rb index 347d7cc..cf34148 100644 --- a/app/models/food.rb +++ b/app/models/food.rb @@ -7,7 +7,7 @@ def initialize(food) def self.retrieve(id) # food = HTTParty.get(YELP_URL + "/v2/business/#{id}") - food = Yelp.client.business(id).business + food = Yelp.client.business(id.parameterize).business Food.new(food) end end diff --git a/app/models/music.rb b/app/models/music.rb index eb6d1c0..28a0936 100644 --- a/app/models/music.rb +++ b/app/models/music.rb @@ -1,9 +1,9 @@ class Music - +attr_reader :name, :artist, :url, :album, :music def initialize(music, type) @name = music.name - @artist = music.artists unless type == "artists" - @url = music.artists[0].external_urls["spotify"] + @artist = music.artists unless type == "artist" + @url = music.artists[0].external_urls["spotify"] unless type =="artist" # @tracks = music.tracks_cache @album = music.album unless type == "album" # @cover = cover_art(music) diff --git a/app/views/suggestions/results.html.erb b/app/views/suggestions/results.html.erb index cda782a..7d96d97 100644 --- a/app/views/suggestions/results.html.erb +++ b/app/views/suggestions/results.html.erb @@ -1,3 +1,9 @@ <% @suggestions.each do |hash|%> - <%= hash %> +
+ <% food = hash["food_id"]%> + <%= food.food.name %> + <% sound = hash["music_id"]%> + <%= sound.music.name %> + +
<%end%> From 99a761d5bdcd866dc96dced699ca5717f3491dff Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sat, 21 May 2016 17:32:49 -0700 Subject: [PATCH 34/45] most basic of styling for suggestion pairings --- app/assets/stylesheets/application.css | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index f9cd5b3..be6c843 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -13,3 +13,16 @@ *= require_tree . *= require_self */ +.float-right { + float: right; +} + +.covers img{ + height: 170px; + width: 170px; +} + +.suggestion { + clear: both; + float: left: +} From dccb1a1887177c0b2751b110e59f3b2421436266 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sat, 21 May 2016 17:33:21 -0700 Subject: [PATCH 35/45] Application controller methods to extract music and food objects from suggestions --- app/controllers/application_controller.rb | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 85d94ce..38c34da 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -17,6 +17,32 @@ def require_login end end + def extract_suggestions(suggestions) + if suggestions.suggestions[0].class == String + suggestions = pull_from_id(suggestions.suggestions) + suggestions.each do |suggestion_hash| + food = suggestion_hash.suggestion["food_id"] + suggestion_hash.suggestion["food_suggestion"]= Food.retrieve(food) + suggestion_hash.suggestion["music_suggestion"] = Music.retrieve(suggestion_hash.suggestion["music_id"], suggestion_hash.suggestion["music_type"]) + end + return suggestions + end + suggestions.each do |suggestion_hash| + food = suggestion_hash["food_id"] + suggestion_hash["food_suggestion"]= Food.retrieve(food) + suggestion_hash["music_suggestion"] = Music.retrieve(suggestion_hash["music_id"], suggestion_hash["music_type"]) + end + return suggestions + end + + def pull_from_id(suggestions) + hashes = [] + suggestions.each do |id| + hashes << TunesTakeoutWrapper.specific_suggestion(id) + end + return hashes + end + def get_food(suggestions) if suggestions.count == 1 food = Food.find(id) From c573fdb4d64772273dc56a01b5a7bdfa64c08645 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sat, 21 May 2016 17:33:58 -0700 Subject: [PATCH 36/45] music model, added functionality to deal with differences in suggestions/suggestion --- app/models/music.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app/models/music.rb b/app/models/music.rb index 28a0936..2d342c4 100644 --- a/app/models/music.rb +++ b/app/models/music.rb @@ -1,13 +1,11 @@ class Music -attr_reader :name, :artist, :url, :album, :music +attr_reader :name, :artist, :url, :album, :cover def initialize(music, type) @name = music.name @artist = music.artists unless type == "artist" @url = music.artists[0].external_urls["spotify"] unless type =="artist" # @tracks = music.tracks_cache - @album = music.album unless type == "album" - # @cover = cover_art(music) - @music = music + @cover = cover_art(music, type) end def self.retrieve(id, type) @@ -24,8 +22,16 @@ def self.retrieve(id, type) Music.new(music, type) end - def cover_art(music) - cover_art = music.images[0]["url"] - # cover_art = music.images[1]["url"] + def cover_art(music, type) + cover_art_array = [] + if type == "album" || type == "artist" + cover_arts = music.images + else + cover_arts = music.album.images + end + cover_arts.each do |cover| + cover_art_array << cover["url"] + end + return cover_art_array end end From 7e7b83fb0e5d88a1fd8618cbc8d79a9667bf6e39 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sat, 21 May 2016 17:34:42 -0700 Subject: [PATCH 37/45] suggestion views for index and results refactored to use partial _suggestiob, + _suggestion --- app/views/suggestions/_suggestion.html.erb | 17 +++++++++++++++++ app/views/suggestions/index.html.erb | 14 ++------------ app/views/suggestions/results.html.erb | 11 +++-------- 3 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 app/views/suggestions/_suggestion.html.erb diff --git a/app/views/suggestions/_suggestion.html.erb b/app/views/suggestions/_suggestion.html.erb new file mode 100644 index 0000000..6da77eb --- /dev/null +++ b/app/views/suggestions/_suggestion.html.erb @@ -0,0 +1,17 @@ +<% @suggestions.each do |hash|%> +
+ <% if hash.class == TunesTakeoutWrapper %> + <% hash = hash.suggestion %> + <%end%> + Eat at + <% food = hash["food_suggestion"]%> + <%= link_to food.name, "https://yelp.com/biz/#{hash["food_id"]}" %><%= image_tag "#{food.rating}" %> +
<%= image_tag "#{food.image}" %><%= food.address.address[0]%>
+
+ While listening to the <%= hash["music_type"]%> + <% sound = hash["music_suggestion"]%> + <%= link_to sound.name, sound.url %> +
<%= image_tag "#{sound.cover[0]}" %> <%= food.address.address[0]%>
+
+
+<%end%> diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index 4ff0983..71bdfeb 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -1,12 +1,2 @@ -

This is the suggestions index page

-<%= params%> -<%= @top%> - -
- <%= form_tag search_path(:query), method: 'get', class: 'navbar-form', role: 'search' do %> -
- <%= text_field_tag :query, params[:query], size: 150, placeholder: "Search", class: "form-control"%> -
- <%= submit_tag 'Search', class: 'btn btn-success' %> - <% end %> -
+

Top rated suggestions:

+<%=render partial: "suggestion"%> diff --git a/app/views/suggestions/results.html.erb b/app/views/suggestions/results.html.erb index 7d96d97..8a35ad0 100644 --- a/app/views/suggestions/results.html.erb +++ b/app/views/suggestions/results.html.erb @@ -1,9 +1,4 @@ -<% @suggestions.each do |hash|%> -
- <% food = hash["food_id"]%> - <%= food.food.name %> - <% sound = hash["music_id"]%> - <%= sound.music.name %> +

Search results for "<%= params["query"]%>"

+<%=params%> -
-<%end%> +<%=render partial: "suggestion"%> From bd63de3870dd73123069901085309c82ba6a27ae Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sat, 21 May 2016 17:35:05 -0700 Subject: [PATCH 38/45] Controller refactored to remove spaghetti --- app/controllers/suggestions_controller.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 9c10654..c26c595 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -2,7 +2,8 @@ class SuggestionsController < ApplicationController skip_before_action :require_login, only: :index def index - @top = TunesTakeoutWrapper.top(4) + @suggestions = TunesTakeoutWrapper.top(4) + @suggestions = extract_suggestions(@suggestions) #shows top 20 suggestions, ranked by total number of favorites @user = current_user # if current_user @@ -27,12 +28,8 @@ def unfavorite def search @user = current_user - @suggestions = TunesTakeoutWrapper.search(params[:query]).suggestions - @suggestions.each do |suggestion_hash| - food = suggestion_hash["food_id"] - suggestion_hash["food_id"]= Food.retrieve(food) - suggestion_hash["music_id"] = Music.retrieve(suggestion_hash["music_id"], suggestion_hash["music_type"]) - end + @suggestions = TunesTakeoutWrapper.limited_search(params["query"], 4).suggestions + @suggestions = extract_suggestions(@suggestions) render :results end From 4bbddf8c41c68e0ddf7ef16956eaa09032665573 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sat, 21 May 2016 17:35:49 -0700 Subject: [PATCH 39/45] Food model updated to include image urls --- app/models/food.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/models/food.rb b/app/models/food.rb index cf34148..a0ff356 100644 --- a/app/models/food.rb +++ b/app/models/food.rb @@ -1,8 +1,12 @@ class Food -attr_reader :food +attr_reader :food, :name, :address, :url, :rating, :image def initialize(food) - @food = food + @name = food.name + @address = food.location + @url = food.mobile_url + @rating = food.rating_img_url + @image = food.image_url end def self.retrieve(id) From a9fba31e20671f40022f2da57fb76d8c3c4e08bb Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sat, 21 May 2016 17:36:15 -0700 Subject: [PATCH 40/45] added search bar to be a part of the application nav --- app/views/layouts/application.html.erb | 13 +++++++++++-- config/initializers/spotify.rb | 3 +++ config/initializers/yelp.rb | 8 ++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 config/initializers/spotify.rb create mode 100644 config/initializers/yelp.rb diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 7fad871..17cfb1f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,10 +11,19 @@ diff --git a/config/initializers/spotify.rb b/config/initializers/spotify.rb new file mode 100644 index 0000000..ac8eee1 --- /dev/null +++ b/config/initializers/spotify.rb @@ -0,0 +1,3 @@ +require 'rspotify' + +RSpotify::authenticate(ENV["SPOTIFY_CLIENT_ID"], ENV["SPOTIFY_CLIENT_SECRET"]) diff --git a/config/initializers/yelp.rb b/config/initializers/yelp.rb new file mode 100644 index 0000000..b51eebb --- /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 From 9dcc9ec351c1516279acf3f2a56d554ddbb01092 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sun, 22 May 2016 00:19:30 -0700 Subject: [PATCH 41/45] Everything works, don't you touch a fucking thing. --- app/controllers/application_controller.rb | 2 +- app/controllers/suggestions_controller.rb | 9 ++++-- app/models/music.rb | 2 +- app/views/layouts/application.html.erb | 3 ++ app/views/suggestions/_suggestion.html.erb | 33 ++++++++++++---------- app/views/suggestions/favorites.html.erb | 2 ++ lib/tunes_takeout_wrapper.rb | 12 ++++++-- 7 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 app/views/suggestions/favorites.html.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 38c34da..d759f2f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,7 +18,7 @@ def require_login end def extract_suggestions(suggestions) - if suggestions.suggestions[0].class == String + if suggestions.class != Array suggestions = pull_from_id(suggestions.suggestions) suggestions.each do |suggestion_hash| food = suggestion_hash.suggestion["food_id"] diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index c26c595..399a910 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -1,9 +1,9 @@ # require '/lib/tunes_takeout_wrapper' class SuggestionsController < ApplicationController skip_before_action :require_login, only: :index + def index - @suggestions = TunesTakeoutWrapper.top(4) - @suggestions = extract_suggestions(@suggestions) + @suggestions = extract_suggestions(TunesTakeoutWrapper.top(4)) #shows top 20 suggestions, ranked by total number of favorites @user = current_user # if current_user @@ -13,7 +13,10 @@ def index def favorites #display user favorites @user = current_user - @favorites = TunesTakeoutWrapper.user_favorites(@user.user_id) #returns array of pairing id's, FIX CALL + @suggestions = TunesTakeoutWrapper.user_favorites(@user.uid) + unless @suggestions.suggestions ==[] + @suggestions = extract_suggestions(@suggestions) #returns array of pairing id's, FIX CALL + end end def favorite diff --git a/app/models/music.rb b/app/models/music.rb index 2d342c4..2a830dd 100644 --- a/app/models/music.rb +++ b/app/models/music.rb @@ -25,7 +25,7 @@ def self.retrieve(id, type) def cover_art(music, type) cover_art_array = [] if type == "album" || type == "artist" - cover_arts = music.images + return music.images else cover_arts = music.album.images end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 17cfb1f..3c85315 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -23,6 +23,9 @@
  • <%= link_to "Log In", "/auth/spotify" unless current_user %>
  • +
  • + <%= link_to "My favorites", favorites_path(current_user.id) if current_user%> +
  • <%= link_to "Log Out", logout_path, method: :delete if current_user%>
  • diff --git a/app/views/suggestions/_suggestion.html.erb b/app/views/suggestions/_suggestion.html.erb index 6da77eb..07901a2 100644 --- a/app/views/suggestions/_suggestion.html.erb +++ b/app/views/suggestions/_suggestion.html.erb @@ -1,17 +1,20 @@ -<% @suggestions.each do |hash|%> -
    - <% if hash.class == TunesTakeoutWrapper %> - <% hash = hash.suggestion %> - <%end%> - Eat at - <% food = hash["food_suggestion"]%> - <%= link_to food.name, "https://yelp.com/biz/#{hash["food_id"]}" %><%= image_tag "#{food.rating}" %> -
    <%= image_tag "#{food.image}" %><%= food.address.address[0]%>
    +<% if @suggestions.class == Array %> + <% @suggestions.each do |hash|%> + +
    + <% if hash.class == TunesTakeoutWrapper %> + <% hash = hash.suggestion %> + <%end%> + Eat at + <% food = hash["food_suggestion"]%> + <%= link_to food.name, "https://yelp.com/biz/#{hash["food_id"]}" %><%= image_tag "#{food.rating}" %> +
    <%= image_tag "#{food.image}" %><%= food.address.address[0]%>
    +
    + While listening to the <%= hash["music_type"]%> + <% sound = hash["music_suggestion"]%> + <%= link_to sound.name, sound.url %> +
    <%= image_tag "#{sound.cover[0]}" %> <%= food.address.address[0]%>
    +

    - While listening to the <%= hash["music_type"]%> - <% sound = hash["music_suggestion"]%> - <%= link_to sound.name, sound.url %> -
    <%= image_tag "#{sound.cover[0]}" %> <%= food.address.address[0]%>
    -
    -
    + <%end%> <%end%> diff --git a/app/views/suggestions/favorites.html.erb b/app/views/suggestions/favorites.html.erb new file mode 100644 index 0000000..f4d385a --- /dev/null +++ b/app/views/suggestions/favorites.html.erb @@ -0,0 +1,2 @@ +

    Your favorite pairings

    +<%=render partial: "suggestion"%> diff --git a/lib/tunes_takeout_wrapper.rb b/lib/tunes_takeout_wrapper.rb index 3646ed4..12a5b7e 100644 --- a/lib/tunes_takeout_wrapper.rb +++ b/lib/tunes_takeout_wrapper.rb @@ -6,9 +6,17 @@ class TunesTakeoutWrapper def initialize(data) if data["suggestions"] - @suggestions = data["suggestions"] + if data["suggestions"].class == Array + @suggestions = data["suggestions"] + else + @suggestions = data["suggestions"].suggestions + end elsif data["suggestion"] - @suggestion = data["suggestion"] + if data["suggestion"].class == Array|| data["suggestion"].class == Hash + @suggestion = data["suggestion"] + else + @suggestion = data["suggestion"].suggestion + end else @errors = "No data returned" end From b29a369b1bef40a5c7042572024011b1d25a6b45 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sun, 22 May 2016 19:57:56 -0700 Subject: [PATCH 42/45] favoriting / unfavoriting functions --- app/controllers/suggestions_controller.rb | 12 +++++++++--- app/views/suggestions/favorites.html.erb | 1 + app/views/suggestions/results.html.erb | 3 ++- lib/tunes_takeout_wrapper.rb | 10 ++++++++-- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index 399a910..c92f8ad 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -21,17 +21,23 @@ def favorites #display user favorites def favorite @user = current_user - @suggestion = TunesTakeoutWrapper.favorite(@user.user_id, suggestion_id) + @path = params[:path] + @suggestion_id = params[:format] + @suggestion = TunesTakeoutWrapper.favorite(@user.uid, @suggestion_id) + redirect_to favorites_path(current_user.uid)#path.to_sym end def unfavorite @user = current_user - @suggestion = TunesTakeoutWrapper.unfavorite(@user.user_id, suggestion_id) + @suggestion_id = params[:format] + @suggestion = TunesTakeoutWrapper.unfavorite(@user.uid, @suggestion_id) + @path = params[:path] + redirect_to favorites_path(current_user.uid)# path.to_sym end def search @user = current_user - @suggestions = TunesTakeoutWrapper.limited_search(params["query"], 4).suggestions + @suggestions = TunesTakeoutWrapper.limited_search(params[:query], 4).suggestions @suggestions = extract_suggestions(@suggestions) render :results end diff --git a/app/views/suggestions/favorites.html.erb b/app/views/suggestions/favorites.html.erb index f4d385a..14c1254 100644 --- a/app/views/suggestions/favorites.html.erb +++ b/app/views/suggestions/favorites.html.erb @@ -1,2 +1,3 @@ +<% @path = favorites_path(current_user.id) %>

    Your favorite pairings

    <%=render partial: "suggestion"%> diff --git a/app/views/suggestions/results.html.erb b/app/views/suggestions/results.html.erb index 8a35ad0..d4fa924 100644 --- a/app/views/suggestions/results.html.erb +++ b/app/views/suggestions/results.html.erb @@ -1,4 +1,5 @@ -

    Search results for "<%= params["query"]%>"

    +<% @path = search_path(params[:query]) %> +

    Search results for "<%= params[:query]%>"

    <%=params%> <%=render partial: "suggestion"%> diff --git a/lib/tunes_takeout_wrapper.rb b/lib/tunes_takeout_wrapper.rb index 12a5b7e..7034d98 100644 --- a/lib/tunes_takeout_wrapper.rb +++ b/lib/tunes_takeout_wrapper.rb @@ -52,11 +52,17 @@ def self.user_favorites(user_id) #returns "suggestions":["VzoxXvLQUmT7dPJ5", "Vz end def self.favorite(user_id, suggestion_id) - HTTParty.post(BASE_URL + "/v1/users/#{user_id}/favorites", { body: {"suggestion": suggestion_id}}.to_json) + fave_request = HTTParty.post(BASE_URL + "/v1/users/#{user_id}/favorites", + body: {"suggestion"=> "#{suggestion_id}"}.to_json, + headers: {"Content-Type" => "application/json" }) + return fave_request.code end def self.unfavorite(user_id, suggestion_id) - HTTParty.delete(BASE_URL + "/v1/users/#{user_id}/favorites", { body: {"suggestion": suggestion_id}}.to_json) + unfave_request = HTTParty.delete(BASE_URL + "/v1/users/#{user_id}/favorites", + body: {"suggestion"=> "#{suggestion_id}"}.to_json, + headers: {"Content-Type" => "application/json" }) + return unfave_request.code end def self.top(limit = 20) From c7b924917c91fca9fa04b962db3263f69a9264c8 Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sun, 22 May 2016 19:59:51 -0700 Subject: [PATCH 43/45] displaying album art correctly - finally covered all edge cases as far as I know --- app/models/music.rb | 19 +++++++++++-------- app/views/suggestions/_suggestion.html.erb | 14 +++++++++++++- app/views/suggestions/results.html.erb | 1 - 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/models/music.rb b/app/models/music.rb index 2a830dd..b4a6a07 100644 --- a/app/models/music.rb +++ b/app/models/music.rb @@ -23,15 +23,18 @@ def self.retrieve(id, type) end def cover_art(music, type) - cover_art_array = [] - if type == "album" || type == "artist" - return music.images + if type == "album" + return music.images[0] + elsif type == "artist" + if music.images == [] + return {"url" => "http://localhost:3000/assets/artist_image.jpg"} + else + return music.images + end + elsif type == "track" + return music.album.images[0] else - cover_arts = music.album.images + return "WHAT DO?!" end - cover_arts.each do |cover| - cover_art_array << cover["url"] - end - return cover_art_array end end diff --git a/app/views/suggestions/_suggestion.html.erb b/app/views/suggestions/_suggestion.html.erb index 07901a2..b668379 100644 --- a/app/views/suggestions/_suggestion.html.erb +++ b/app/views/suggestions/_suggestion.html.erb @@ -13,7 +13,19 @@ While listening to the <%= hash["music_type"]%> <% sound = hash["music_suggestion"]%> <%= link_to sound.name, sound.url %> -
    <%= image_tag "#{sound.cover[0]}" %> <%= food.address.address[0]%>
    +
    <%= (sound.cover.class == Array) ? (image_tag "#{sound.cover[0]["url"]}") : (image_tag "#{sound.cover["url"]}") %> <%= food.address.address[0]%>
    +
    + <% user_faves = TunesTakeoutWrapper.user_favorites(@user.uid)%> + <% unless user_faves ==[]%> + <% user_faves = user_faves.suggestions%> #returns array of pairing id's %> + <%end%> + <% if user_faves.include? hash["id"] %> + <%= link_to "Unfavorite this pairing", unfave_path(hash["id"], @path), method: :delete, class: "btn btn-danger" %> + <% else%> + <%= link_to "Favorite this pairing", add_fave_path(hash["id"], @path), method: :post, class: "btn btn-primary" %> + <%end%> +
    +
    <%end%> diff --git a/app/views/suggestions/results.html.erb b/app/views/suggestions/results.html.erb index d4fa924..5a0d849 100644 --- a/app/views/suggestions/results.html.erb +++ b/app/views/suggestions/results.html.erb @@ -1,5 +1,4 @@ <% @path = search_path(params[:query]) %>

    Search results for "<%= params[:query]%>"

    -<%=params%> <%=render partial: "suggestion"%> From a37dd56aa04098db95dfac43d7d143d28986128b Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sun, 22 May 2016 20:00:20 -0700 Subject: [PATCH 44/45] attemted to pass path variable to re-render page pairing was favorited from, but not functional --- app/views/suggestions/index.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/suggestions/index.html.erb b/app/views/suggestions/index.html.erb index 71bdfeb..7cdabc7 100644 --- a/app/views/suggestions/index.html.erb +++ b/app/views/suggestions/index.html.erb @@ -1,2 +1,3 @@ +<% @path = root_path %>

    Top rated suggestions:

    <%=render partial: "suggestion"%> From b9704f3a3a674581420a6165119beb0bbdb3543d Mon Sep 17 00:00:00 2001 From: Nicole Iwasaki Date: Sun, 22 May 2016 20:29:06 -0700 Subject: [PATCH 45/45] Added user photo or guest photo to nav in layout --- app/assets/stylesheets/application.css | 5 +++++ app/controllers/suggestions_controller.rb | 6 +++++- app/views/layouts/application.html.erb | 6 ++++++ app/views/suggestions/_suggestion.html.erb | 18 ++++++++++-------- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index be6c843..264f23e 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -26,3 +26,8 @@ clear: both; float: left: } + +.user-photo img { + height: 70px; + width: 70px; +} diff --git a/app/controllers/suggestions_controller.rb b/app/controllers/suggestions_controller.rb index c92f8ad..d97b130 100644 --- a/app/controllers/suggestions_controller.rb +++ b/app/controllers/suggestions_controller.rb @@ -5,7 +5,11 @@ class SuggestionsController < ApplicationController def index @suggestions = extract_suggestions(TunesTakeoutWrapper.top(4)) #shows top 20 suggestions, ranked by total number of favorites - @user = current_user + if current_user + @spotify_user = RSpotify::User.find(@current_user.uid) + @user = current_user + @image = @spotify_user.images[0]["url"] + end # if current_user # @favorites = TunesTakeoutWrapper.user_favorites(@user.user_id) # end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 3c85315..640da3c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -26,6 +26,12 @@
  • <%= link_to "My favorites", favorites_path(current_user.id) if current_user%>
  • +
  • <% if @user && @image %> + <%= image_tag "#{@image}" %> + <%else%> + <%= image_tag "http://localhost:3000/assets/artist_image.jpg"%> + <%end%> +
  • <%= link_to "Log Out", logout_path, method: :delete if current_user%>
  • diff --git a/app/views/suggestions/_suggestion.html.erb b/app/views/suggestions/_suggestion.html.erb index b668379..5f396e0 100644 --- a/app/views/suggestions/_suggestion.html.erb +++ b/app/views/suggestions/_suggestion.html.erb @@ -15,14 +15,16 @@ <%= link_to sound.name, sound.url %>
    <%= (sound.cover.class == Array) ? (image_tag "#{sound.cover[0]["url"]}") : (image_tag "#{sound.cover["url"]}") %> <%= food.address.address[0]%>
    - <% user_faves = TunesTakeoutWrapper.user_favorites(@user.uid)%> - <% unless user_faves ==[]%> - <% user_faves = user_faves.suggestions%> #returns array of pairing id's %> - <%end%> - <% if user_faves.include? hash["id"] %> - <%= link_to "Unfavorite this pairing", unfave_path(hash["id"], @path), method: :delete, class: "btn btn-danger" %> - <% else%> - <%= link_to "Favorite this pairing", add_fave_path(hash["id"], @path), method: :post, class: "btn btn-primary" %> + <% if current_user %> + <% user_faves = TunesTakeoutWrapper.user_favorites(current_user.uid)%> + <% unless user_faves ==[]%> + <% user_faves = user_faves.suggestions%> #returns array of pairing id's %> + <%end%> + <% if user_faves.include? hash["id"] %> + <%= link_to "Unfavorite this pairing", unfave_path(hash["id"], @path), method: :delete, class: "btn btn-danger" %> + <% else%> + <%= link_to "Favorite this pairing", add_fave_path(hash["id"], @path), method: :post, class: "btn btn-primary" %> + <%end%> <%end%>