From a804da0c990ab804eda1d17e163d2e32aa2d16c8 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Mon, 31 Oct 2016 16:07:45 -0700 Subject: [PATCH 01/45] Added a homepages controller and some lib folder files --- .env | 0 app/assets/javascripts/homepages.coffee | 3 +++ app/assets/stylesheets/homepages.scss | 3 +++ app/controllers/homepages_controller.rb | 2 ++ app/helpers/homepages_helper.rb | 2 ++ lib/edemam_api_wrapper.rb | 5 +++++ lib/recipe.rb | 3 +++ test/controllers/homepages_controller_test.rb | 7 +++++++ 8 files changed, 25 insertions(+) delete mode 100644 .env create mode 100644 app/assets/javascripts/homepages.coffee create mode 100644 app/assets/stylesheets/homepages.scss create mode 100644 app/controllers/homepages_controller.rb create mode 100644 app/helpers/homepages_helper.rb create mode 100644 lib/edemam_api_wrapper.rb create mode 100644 lib/recipe.rb create mode 100644 test/controllers/homepages_controller_test.rb diff --git a/.env b/.env deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/assets/javascripts/homepages.coffee b/app/assets/javascripts/homepages.coffee new file mode 100644 index 000000000..24f83d18b --- /dev/null +++ b/app/assets/javascripts/homepages.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/homepages.scss b/app/assets/stylesheets/homepages.scss new file mode 100644 index 000000000..e42583ae8 --- /dev/null +++ b/app/assets/stylesheets/homepages.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the homepages 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/homepages_controller.rb b/app/controllers/homepages_controller.rb new file mode 100644 index 000000000..8663bd848 --- /dev/null +++ b/app/controllers/homepages_controller.rb @@ -0,0 +1,2 @@ +class HomepagesController < ApplicationController +end diff --git a/app/helpers/homepages_helper.rb b/app/helpers/homepages_helper.rb new file mode 100644 index 000000000..4bd8098f3 --- /dev/null +++ b/app/helpers/homepages_helper.rb @@ -0,0 +1,2 @@ +module HomepagesHelper +end diff --git a/lib/edemam_api_wrapper.rb b/lib/edemam_api_wrapper.rb new file mode 100644 index 000000000..7f56b825c --- /dev/null +++ b/lib/edemam_api_wrapper.rb @@ -0,0 +1,5 @@ +class EdemamApiWrapper + BASE_URL = "https://api.edamam.com/search" + KEY = ENV["APP_KEY"] + ID - ENV["APP_ID"] +end diff --git a/lib/recipe.rb b/lib/recipe.rb new file mode 100644 index 000000000..de83aa172 --- /dev/null +++ b/lib/recipe.rb @@ -0,0 +1,3 @@ +class Recipe + +end diff --git a/test/controllers/homepages_controller_test.rb b/test/controllers/homepages_controller_test.rb new file mode 100644 index 000000000..d73f39b9c --- /dev/null +++ b/test/controllers/homepages_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class HomepagesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end From 62e77ef99ce5fd499e1ece8a3cd5c70716311f65 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Mon, 31 Oct 2016 16:54:16 -0700 Subject: [PATCH 02/45] Forgot to add httparty gem --- Gemfile | 1 + Gemfile.lock | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/Gemfile b/Gemfile index aa61d21fd..afb82bc49 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,7 @@ group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri gem 'better_errors' + gem 'httparty' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 84e4e9889..ebb4f1147 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,6 +60,8 @@ GEM ffi (1.9.14) globalid (0.3.7) activesupport (>= 4.1.0) + httparty (0.14.0) + multi_xml (>= 0.5.2) i18n (0.7.0) jbuilder (2.6.0) activesupport (>= 3.0.0, < 5.1) @@ -82,6 +84,7 @@ GEM mini_portile2 (2.1.0) minitest (5.9.1) multi_json (1.12.1) + multi_xml (0.5.5) nio4r (1.2.1) nokogiri (1.6.8.1) mini_portile2 (~> 2.1.0) @@ -162,6 +165,7 @@ DEPENDENCIES better_errors byebug coffee-rails (~> 4.2) + httparty jbuilder (~> 2.5) jquery-rails listen (~> 3.0.5) From 6b9cee0b7ba279b135b1d60a3cc49caad02ea29c Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Tue, 1 Nov 2016 11:22:21 -0700 Subject: [PATCH 03/45] Added index and show views --- app/views/homepages/index.html.erb | 1 + app/views/homepages/show.html.erb | 0 2 files changed, 1 insertion(+) create mode 100644 app/views/homepages/index.html.erb create mode 100644 app/views/homepages/show.html.erb diff --git a/app/views/homepages/index.html.erb b/app/views/homepages/index.html.erb new file mode 100644 index 000000000..00c07af2a --- /dev/null +++ b/app/views/homepages/index.html.erb @@ -0,0 +1 @@ +

hey.

diff --git a/app/views/homepages/show.html.erb b/app/views/homepages/show.html.erb new file mode 100644 index 000000000..e69de29bb From 8ef9e2e453a534fe50b8acb58b01a19478fc1147 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Tue, 1 Nov 2016 11:44:02 -0700 Subject: [PATCH 04/45] Added dotenv gem along with minitest and vcr gems --- Gemfile | 3 +++ Gemfile.lock | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Gemfile b/Gemfile index afb82bc49..53afb5141 100644 --- a/Gemfile +++ b/Gemfile @@ -35,6 +35,9 @@ group :development, :test do gem 'byebug', platform: :mri gem 'better_errors' gem 'httparty' + gem 'dotenv-rails' + gem 'minitest-vcr' + gem 'webmock' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index ebb4f1147..804d67b55 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -38,6 +38,7 @@ GEM i18n (~> 0.7) minitest (~> 5.1) tzinfo (~> 1.1) + addressable (2.4.0) arel (7.1.4) better_errors (2.1.1) coderay (>= 1.0.0) @@ -54,12 +55,19 @@ 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.7.0) ffi (1.9.14) globalid (0.3.7) activesupport (>= 4.1.0) + hashdiff (0.3.0) httparty (0.14.0) multi_xml (>= 0.5.2) i18n (0.7.0) @@ -82,7 +90,13 @@ GEM mime-types-data (~> 3.2015) mime-types-data (3.2016.0521) mini_portile2 (2.1.0) + minispec-metadata (2.0.0) + minitest minitest (5.9.1) + minitest-vcr (1.4.0) + minispec-metadata (~> 2.0) + minitest (>= 4.7.5) + vcr (>= 2.9) multi_json (1.12.1) multi_xml (0.5.5) nio4r (1.2.1) @@ -119,6 +133,7 @@ GEM rb-fsevent (0.9.8) rb-inotify (0.9.7) ffi (>= 0.5.0) + safe_yaml (1.0.4) sass (3.4.22) sass-rails (5.0.6) railties (>= 4.0.0, < 6) @@ -149,11 +164,16 @@ GEM thread_safe (~> 0.1) uglifier (3.0.3) execjs (>= 0.3.0, < 3) + vcr (3.0.3) web-console (3.4.0) actionview (>= 5.0) activemodel (>= 5.0) debug_inspector railties (>= 5.0) + webmock (2.1.0) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff websocket-driver (0.6.4) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.2) @@ -165,10 +185,12 @@ DEPENDENCIES better_errors byebug coffee-rails (~> 4.2) + dotenv-rails httparty jbuilder (~> 2.5) jquery-rails listen (~> 3.0.5) + minitest-vcr puma (~> 3.0) rails (~> 5.0.0, >= 5.0.0.1) sass-rails (~> 5.0) @@ -179,6 +201,7 @@ DEPENDENCIES tzinfo-data uglifier (>= 1.3.0) web-console + webmock BUNDLED WITH 1.13.5 From ca0041eb36e058e2f3f1da27d1485aa65b8a6c7f Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Tue, 1 Nov 2016 12:13:53 -0700 Subject: [PATCH 05/45] Added empty test files for my lib files --- test/lib/edamam_api_wrapper_test.rb | 0 test/lib/recipe_test.rb | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/lib/edamam_api_wrapper_test.rb create mode 100644 test/lib/recipe_test.rb diff --git a/test/lib/edamam_api_wrapper_test.rb b/test/lib/edamam_api_wrapper_test.rb new file mode 100644 index 000000000..e69de29bb diff --git a/test/lib/recipe_test.rb b/test/lib/recipe_test.rb new file mode 100644 index 000000000..e69de29bb From 09eef9f656c128735aaf5b3c2447d33032f25686 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Tue, 1 Nov 2016 12:14:23 -0700 Subject: [PATCH 06/45] added requires for my lib files in the application controller --- app/controllers/application_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1c07694e9..4482f78c0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,6 @@ +require "edemam_api_wrapper" +require "recipe" + class ApplicationController < ActionController::Base protect_from_forgery with: :exception end From cd77c0969f01858aa7e2fc551707f42c94b8193f Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Tue, 1 Nov 2016 12:19:34 -0700 Subject: [PATCH 07/45] I spelled edamam wrong in my api wrapper --- lib/edamam_api_wrapper.rb | 30 ++++++++++++++++++++++++++++++ lib/edemam_api_wrapper.rb | 5 ----- lib/recipe.rb | 14 ++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 lib/edamam_api_wrapper.rb delete mode 100644 lib/edemam_api_wrapper.rb diff --git a/lib/edamam_api_wrapper.rb b/lib/edamam_api_wrapper.rb new file mode 100644 index 000000000..f4917219d --- /dev/null +++ b/lib/edamam_api_wrapper.rb @@ -0,0 +1,30 @@ +require 'httparty' + +class EdamamApiWrapper + BASE_URL = "https://api.edamam.com/search?q=" + KEY = ENV["APP_KEY"] + ID = ENV["APP_ID"] + + + def self.listrecipes(search_word) + url = "#{BASE_URL}" + "#{search_word}" + "&app_id=$#{ID}" + "&app_key=$#{KEY}" + data = HTTParty.get(url) + + # puts "#{url}" + # return data + recipes_list = [] + + if data["hits"] + data["hits"].each do |entry| + entry["recipe"].each do |recipe| + wrapper = Recipe.new(recipe["label"]) #{ image: entry["image"], source: entry["source"], ingredients: entry["ingredientLines"], url: entry[:url] } ) + + recipes_list << wrapper + end + end + end + + return recipes_list + end + +end diff --git a/lib/edemam_api_wrapper.rb b/lib/edemam_api_wrapper.rb deleted file mode 100644 index 7f56b825c..000000000 --- a/lib/edemam_api_wrapper.rb +++ /dev/null @@ -1,5 +0,0 @@ -class EdemamApiWrapper - BASE_URL = "https://api.edamam.com/search" - KEY = ENV["APP_KEY"] - ID - ENV["APP_ID"] -end diff --git a/lib/recipe.rb b/lib/recipe.rb index de83aa172..0eda2f6ab 100644 --- a/lib/recipe.rb +++ b/lib/recipe.rb @@ -1,3 +1,17 @@ +require 'httparty' + class Recipe + def initialize(name, options = { }) + raise ArgumentError if name == nil || name == "" || id == nil || id == "" + + @name = name + @id = id + + @image = options[:image] + @source = options[:source] + @ingredients = options[:ingredientLines] + @url = options[:url] + end + end From fc4f5eb794194a4d89e43fdb00c16a1387e4de9c Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Tue, 1 Nov 2016 19:28:14 -0700 Subject: [PATCH 08/45] Modifed the views a little so I'd know which page was which --- app/views/homepages/index.html.erb | 2 +- app/views/homepages/search.html.erb | 1 + app/views/homepages/show.html.erb | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 app/views/homepages/search.html.erb diff --git a/app/views/homepages/index.html.erb b/app/views/homepages/index.html.erb index 00c07af2a..4d1252504 100644 --- a/app/views/homepages/index.html.erb +++ b/app/views/homepages/index.html.erb @@ -1 +1 @@ -

hey.

+

index - search results.

diff --git a/app/views/homepages/search.html.erb b/app/views/homepages/search.html.erb new file mode 100644 index 000000000..053ca7180 --- /dev/null +++ b/app/views/homepages/search.html.erb @@ -0,0 +1 @@ +

welcome, please search here

diff --git a/app/views/homepages/show.html.erb b/app/views/homepages/show.html.erb index e69de29bb..802c41cd7 100644 --- a/app/views/homepages/show.html.erb +++ b/app/views/homepages/show.html.erb @@ -0,0 +1 @@ +

detailed recipe stuff here

From 481f07c35b946c994de7e607f45ccf5cf0eeb8b4 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Wed, 2 Nov 2016 15:12:03 -0700 Subject: [PATCH 09/45] Added binding of caller of gem --- Gemfile | 1 + Gemfile.lock | 3 +++ 2 files changed, 4 insertions(+) diff --git a/Gemfile b/Gemfile index 53afb5141..3fa7995b3 100644 --- a/Gemfile +++ b/Gemfile @@ -38,6 +38,7 @@ group :development, :test do gem 'dotenv-rails' gem 'minitest-vcr' gem 'webmock' + gem 'binding_of_caller' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 804d67b55..f7f453531 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -44,6 +44,8 @@ GEM 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 (9.0.6) coderay (1.1.1) @@ -183,6 +185,7 @@ PLATFORMS DEPENDENCIES better_errors + binding_of_caller byebug coffee-rails (~> 4.2) dotenv-rails From cc5c8659d26e24910c13e568e24a14df97b31692 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Wed, 2 Nov 2016 15:12:45 -0700 Subject: [PATCH 10/45] Changed wrapper a bit and inactivated .all method in recipe.rb --- lib/edamam_api_wrapper.rb | 14 ++++++-------- lib/recipe.rb | 11 ++++++++--- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/edamam_api_wrapper.rb b/lib/edamam_api_wrapper.rb index f4917219d..f6a0d6f41 100644 --- a/lib/edamam_api_wrapper.rb +++ b/lib/edamam_api_wrapper.rb @@ -5,22 +5,20 @@ class EdamamApiWrapper KEY = ENV["APP_KEY"] ID = ENV["APP_ID"] + def initialize(search_word) + @search_word = search_word + end def self.listrecipes(search_word) - url = "#{BASE_URL}" + "#{search_word}" + "&app_id=$#{ID}" + "&app_key=$#{KEY}" + url = "#{BASE_URL}" + "#{@search_word}" + "&app_id=#{ID}" + "&app_key=#{KEY}" data = HTTParty.get(url) - # puts "#{url}" - # return data recipes_list = [] if data["hits"] data["hits"].each do |entry| - entry["recipe"].each do |recipe| - wrapper = Recipe.new(recipe["label"]) #{ image: entry["image"], source: entry["source"], ingredients: entry["ingredientLines"], url: entry[:url] } ) - - recipes_list << wrapper - end + thing = Recipe.new(entry["recipe"]["label"], { image: entry["recipe"]["image"], source: entry["recipe"]["source"], ingredients: entry["recipe"]["ingredientLines"], url: entry["recipe"]["url"] } ) + recipes_list << thing end end diff --git a/lib/recipe.rb b/lib/recipe.rb index 0eda2f6ab..497f328ab 100644 --- a/lib/recipe.rb +++ b/lib/recipe.rb @@ -1,17 +1,22 @@ require 'httparty' +require 'edamam_api_wrapper.rb' class Recipe + attr_reader :name, :image, :source, :ingredients, :url def initialize(name, options = { }) - raise ArgumentError if name == nil || name == "" || id == nil || id == "" + raise ArgumentError if name == nil || name == "" @name = name - @id = id @image = options[:image] @source = options[:source] - @ingredients = options[:ingredientLines] + @ingredients = options[:ingredients] @url = options[:url] end + # def self.all(search_word) + # EdamamApiWrapper.listrecipes(search_word) + # end + end From ddc7277f150d06e047ce7d4151ec1b0a0cd33657 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Wed, 2 Nov 2016 15:14:01 -0700 Subject: [PATCH 11/45] Added some tests and their files, nothing working yet --- test/controllers/searches_controller_test.rb | 7 +++ test/lib/edamam_api_wrapper_test.rb | 37 ++++++++++++++++ test/lib/recipe_test.rb | 46 ++++++++++++++++++++ test/test_helper.rb | 13 ++++++ 4 files changed, 103 insertions(+) create mode 100644 test/controllers/searches_controller_test.rb diff --git a/test/controllers/searches_controller_test.rb b/test/controllers/searches_controller_test.rb new file mode 100644 index 000000000..194ba9790 --- /dev/null +++ b/test/controllers/searches_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SearchesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/lib/edamam_api_wrapper_test.rb b/test/lib/edamam_api_wrapper_test.rb index e69de29bb..3d643e8cb 100644 --- a/test/lib/edamam_api_wrapper_test.rb +++ b/test/lib/edamam_api_wrapper_test.rb @@ -0,0 +1,37 @@ +require 'test_helper' + +class EdamamApiWrapperTest < ActiveSupport::TestCase + test "whether the tests are running" do + assert true + end + + test "Can send valid message to real channel" do + VCR.use_cassette("channels") do + message = "test message, yo" + response = SlackApiWrapper.sendmsg("test-api-brackets", message) + assert response["ok"] + assert_equal response["message"]["text"], message + end + end + + test "Can't send message to fake channel" do + VCR.use_cassette("channels") do + response = SlackApiWrapper.sendmsg("this-channel-does-not-exist", "test message") + assert_not response["ok"] + assert_not_nil response["error"] + end + end + + test "listchannels returns an array of Channel objects" do + VCR.use_cassette("channels") do + channels = SlackApiWrapper.listchannels + + assert_kind_of Array, channels + assert_not channels.empty? + + channels.each do |channel| + assert_kind_of Channel, channel + end + end + end +end diff --git a/test/lib/recipe_test.rb b/test/lib/recipe_test.rb index e69de29bb..c6805c1cf 100644 --- a/test/lib/recipe_test.rb +++ b/test/lib/recipe_test.rb @@ -0,0 +1,46 @@ +require 'test_helper' + +class RecipeTest < ActiveSupport::TestCase + test "whether the tests are running" do + assert true + end + + test "initialize will create a Recipe object" do + name = "name" + test_id = "test_id" + recipe = Recipe.new(name,test_id) + + assert_equal name, recipe.name + assert_equal test_id, recipe.id + assert_kind_of Recipe, recipe + end + + test "Recipe.all returns an array of Recipe objects" do + VCR.use_cassette("recipes") do + recipes = Recipe.all + + assert_kind_of Array, recipes + assert_not recipes.empty? + + recipes.each do |recipe| + assert_kind_of Recipe, recipe + end + end + end + + test "Recipe.by_name should return nil if no match" do + VCR.use_cassette("recipes") do + recipe = Recipe.by_name("non-existant_recipe") + assert_nil recipe + end + end + + test "Recipe.by_name should return matching recipe" do + VCR.use_cassette("recipes") do + recipe = Recipe.by_name("test-api-brackets") + name = "test-api-brackets" + assert_kind_of Recipe, recipe + assert_equal name, recipe.name + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 92e39b2d7..26dc41e45 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,10 +1,23 @@ ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' +require 'minitest/reporters' +require 'vcr' +require 'webmock/minitest' class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all # Add more helper methods to be used by all tests here... + Minitest::Reporters.use! +end + +VCR.configure do |config| + config.cassette_library_dir = 'test/cassettes' # folder where casettes will be located + config.hook_into :webmock # tie into this other tool called webmock + config.default_cassette_options = { + :record => :new_episodes, # record new data when we don't have it yet + :match_requests_on => [:method, :uri, :body] # The http method, URI and body of a request all need to match + } end From e7ae58da3e1b0b0f276cb901072edab24872367c Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Wed, 2 Nov 2016 15:15:04 -0700 Subject: [PATCH 12/45] Added a search controller, made more sense to me I guess --- app/assets/javascripts/searches.coffee | 3 +++ app/assets/stylesheets/searches.scss | 3 +++ app/controllers/searches_controller.rb | 27 ++++++++++++++++++++++++++ app/helpers/searches_helper.rb | 2 ++ app/views/homepages/search.html.erb | 1 - app/views/searches/index.html.erb | 5 +++++ app/views/searches/new.html.erb | 7 +++++++ app/views/searches/show.html.erb | 14 +++++++++++++ 8 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/searches.coffee create mode 100644 app/assets/stylesheets/searches.scss create mode 100644 app/controllers/searches_controller.rb create mode 100644 app/helpers/searches_helper.rb delete mode 100644 app/views/homepages/search.html.erb create mode 100644 app/views/searches/index.html.erb create mode 100644 app/views/searches/new.html.erb create mode 100644 app/views/searches/show.html.erb diff --git a/app/assets/javascripts/searches.coffee b/app/assets/javascripts/searches.coffee new file mode 100644 index 000000000..24f83d18b --- /dev/null +++ b/app/assets/javascripts/searches.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/searches.scss b/app/assets/stylesheets/searches.scss new file mode 100644 index 000000000..886c1d09f --- /dev/null +++ b/app/assets/stylesheets/searches.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the searches 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/searches_controller.rb b/app/controllers/searches_controller.rb new file mode 100644 index 000000000..7b0914bbc --- /dev/null +++ b/app/controllers/searches_controller.rb @@ -0,0 +1,27 @@ +class SearchesController < ApplicationController + def index + @results + end + + def show + raise + selection = @results.select do |recipe| + if recipe.name == params[:recipe_name] + end + end + + return selection[0] + end + + def new; end + + + def create + @results ||= EdamamApiWrapper.listrecipes(params[:search_word]) + class << self + attr_writer :results + end + render :index + end + +end diff --git a/app/helpers/searches_helper.rb b/app/helpers/searches_helper.rb new file mode 100644 index 000000000..382355326 --- /dev/null +++ b/app/helpers/searches_helper.rb @@ -0,0 +1,2 @@ +module SearchesHelper +end diff --git a/app/views/homepages/search.html.erb b/app/views/homepages/search.html.erb deleted file mode 100644 index 053ca7180..000000000 --- a/app/views/homepages/search.html.erb +++ /dev/null @@ -1 +0,0 @@ -

welcome, please search here

diff --git a/app/views/searches/index.html.erb b/app/views/searches/index.html.erb new file mode 100644 index 000000000..5476e0507 --- /dev/null +++ b/app/views/searches/index.html.erb @@ -0,0 +1,5 @@ +

Ten Search results

+ +<% @results.each do |result| %> +

<%= link_to result.name, search_path("result.name") %>

+<% end %> diff --git a/app/views/searches/new.html.erb b/app/views/searches/new.html.erb new file mode 100644 index 000000000..b504e0428 --- /dev/null +++ b/app/views/searches/new.html.erb @@ -0,0 +1,7 @@ +

Please enter a food search word here:

+ +<%= form_tag searches_path do %> + + <%= text_field_tag :search_word %> + <%= submit_tag "Submit" %> +<% end %> diff --git a/app/views/searches/show.html.erb b/app/views/searches/show.html.erb new file mode 100644 index 000000000..bd09997d8 --- /dev/null +++ b/app/views/searches/show.html.erb @@ -0,0 +1,14 @@ +

<%= @recipe.name %>

+<%= image_tag url(@recipe.image) %> +

Source:

+

<%= @recipe %>

+ +

Ingredients:

+
    + <% @recipe.ingredients.each do |ingredient| %> +
  • <%= ingredient %>
  • + <% end %> +
+ +

Instructions:

+

<%= @recipe.url %>

From 0e8f2ef1f0c933a8c285831252cd46afeca9a902 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Wed, 2 Nov 2016 15:16:00 -0700 Subject: [PATCH 13/45] Added some routes for searches controller --- config/application.rb | 6 ++++++ config/routes.rb | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/config/application.rb b/config/application.rb index 38f15dbec..37c7d1ed6 100644 --- a/config/application.rb +++ b/config/application.rb @@ -11,5 +11,11 @@ class Application < Rails::Application # 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. + + # Do not swallow errors in after_commit/after_rollback callbacks. + config.active_record.raise_in_transactional_callbacks = true + + # Automatically load all code from /lib + config.autoload_paths << Rails.root.join('lib') end end diff --git a/config/routes.rb b/config/routes.rb index 787824f88..62d3fba0c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,9 @@ Rails.application.routes.draw do - # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + resources :homepages, only: [:index] + + get '/searches/', to: 'searches#index', as: 'searches' + post '/searches/', to: 'searches#create', as: 'create_search' + get '/searches/new/', to: 'searches#new', as: 'new_search' + get '/searches/:recipe/', to: 'searches#show', as: 'search' + end From 8415b84ce7ac93bc6288860f51ce4ab1811552f0 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Wed, 2 Nov 2016 15:16:32 -0700 Subject: [PATCH 14/45] Required lib directory files in application controller --- app/controllers/application_controller.rb | 4 ++-- app/controllers/homepages_controller.rb | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4482f78c0..f23d4c8d7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,5 @@ -require "edemam_api_wrapper" -require "recipe" +require "edamam_api_wrapper.rb" +require "recipe.rb" class ApplicationController < ActionController::Base protect_from_forgery with: :exception diff --git a/app/controllers/homepages_controller.rb b/app/controllers/homepages_controller.rb index 8663bd848..f200c8ac2 100644 --- a/app/controllers/homepages_controller.rb +++ b/app/controllers/homepages_controller.rb @@ -1,2 +1,4 @@ class HomepagesController < ApplicationController + def index + end end From 5dcdba8475198c2c85fdf06d118b69e72edcc744 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Wed, 2 Nov 2016 15:16:59 -0700 Subject: [PATCH 15/45] Only index view for homepages controller now --- app/views/homepages/index.html.erb | 2 +- app/views/homepages/show.html.erb | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 app/views/homepages/show.html.erb diff --git a/app/views/homepages/index.html.erb b/app/views/homepages/index.html.erb index 4d1252504..4c52a201a 100644 --- a/app/views/homepages/index.html.erb +++ b/app/views/homepages/index.html.erb @@ -1 +1 @@ -

index - search results.

+

Please search here

diff --git a/app/views/homepages/show.html.erb b/app/views/homepages/show.html.erb deleted file mode 100644 index 802c41cd7..000000000 --- a/app/views/homepages/show.html.erb +++ /dev/null @@ -1 +0,0 @@ -

detailed recipe stuff here

From 65c631f055314847553f3ff10a2fd61cef07f6a6 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Wed, 2 Nov 2016 16:34:56 -0700 Subject: [PATCH 16/45] Changed the show route a little --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 62d3fba0c..48ba26eda 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,6 @@ get '/searches/', to: 'searches#index', as: 'searches' post '/searches/', to: 'searches#create', as: 'create_search' get '/searches/new/', to: 'searches#new', as: 'new_search' - get '/searches/:recipe/', to: 'searches#show', as: 'search' + get '/searches/:id/', to: 'searches#show', as: 'search' end From 4a9651a5d734f48cb093af62d5f241a0cf7bb882 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Wed, 2 Nov 2016 16:35:47 -0700 Subject: [PATCH 17/45] Finally got show to work by dividing up the uri and putting it back together in the api wrapper --- app/controllers/searches_controller.rb | 14 ++++---------- app/views/searches/index.html.erb | 2 +- app/views/searches/show.html.erb | 8 +++++--- lib/edamam_api_wrapper.rb | 22 +++++++++++++++------- lib/recipe.rb | 7 ++++--- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/app/controllers/searches_controller.rb b/app/controllers/searches_controller.rb index 7b0914bbc..6aa77dc3a 100644 --- a/app/controllers/searches_controller.rb +++ b/app/controllers/searches_controller.rb @@ -4,23 +4,17 @@ def index end def show - raise - selection = @results.select do |recipe| - if recipe.name == params[:recipe_name] - end - end - - return selection[0] + @recipe ||= EdamamApiWrapper.listrecipe(params[:id]) end def new; end + # class << self + # attr_writer :results + # end def create @results ||= EdamamApiWrapper.listrecipes(params[:search_word]) - class << self - attr_writer :results - end render :index end diff --git a/app/views/searches/index.html.erb b/app/views/searches/index.html.erb index 5476e0507..45b779f70 100644 --- a/app/views/searches/index.html.erb +++ b/app/views/searches/index.html.erb @@ -1,5 +1,5 @@

Ten Search results

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

<%= link_to result.name, search_path("result.name") %>

+

<%= link_to result.name, search_path(id: result.id) %>

<% end %> diff --git a/app/views/searches/show.html.erb b/app/views/searches/show.html.erb index bd09997d8..011e4b608 100644 --- a/app/views/searches/show.html.erb +++ b/app/views/searches/show.html.erb @@ -1,7 +1,9 @@

<%= @recipe.name %>

-<%= image_tag url(@recipe.image) %> +<%= image_tag @recipe.image %>

Source:

-

<%= @recipe %>

+
    +
  • <%= @recipe.source %>
  • +

Ingredients:

    @@ -11,4 +13,4 @@

Instructions:

-

<%= @recipe.url %>

+<%= @recipe.url %>

diff --git a/lib/edamam_api_wrapper.rb b/lib/edamam_api_wrapper.rb index f6a0d6f41..53664a58c 100644 --- a/lib/edamam_api_wrapper.rb +++ b/lib/edamam_api_wrapper.rb @@ -1,23 +1,20 @@ require 'httparty' class EdamamApiWrapper - BASE_URL = "https://api.edamam.com/search?q=" + BASE_URL = "https://api.edamam.com/search" + PREFIX = "http://www.edamam.com/ontologies/edamam.owl%23recipe_" KEY = ENV["APP_KEY"] ID = ENV["APP_ID"] - def initialize(search_word) - @search_word = search_word - end - def self.listrecipes(search_word) - url = "#{BASE_URL}" + "#{@search_word}" + "&app_id=#{ID}" + "&app_key=#{KEY}" + url = "#{BASE_URL}" + "?q=" + "#{search_word}" + "&app_id=#{ID}" + "&app_key=#{KEY}" data = HTTParty.get(url) recipes_list = [] if data["hits"] data["hits"].each do |entry| - thing = Recipe.new(entry["recipe"]["label"], { image: entry["recipe"]["image"], source: entry["recipe"]["source"], ingredients: entry["recipe"]["ingredientLines"], url: entry["recipe"]["url"] } ) + thing = Recipe.new(entry["recipe"]["label"], entry["recipe"]["uri"], { image: entry["recipe"]["image"], source: entry["recipe"]["source"], ingredients: entry["recipe"]["ingredientLines"], url: entry["recipe"]["url"] } ) recipes_list << thing end end @@ -25,4 +22,15 @@ def self.listrecipes(search_word) return recipes_list end + def self.listrecipe(id) + url = "#{BASE_URL}" + "?r=" + "#{PREFIX}" + "#{id}" + "&app_id=#{ID}" + "&app_key=#{KEY}" + data = HTTParty.get(url) + + if !data.nil? + Recipe.new(data[0]["label"], data[0]["uri"], { image: data[0]["image"], source: data[0]["source"], ingredients: data[0]["ingredientLines"], url: data[0]["url"] } ) + # else + # render some sort of search not found error page + end + end + end diff --git a/lib/recipe.rb b/lib/recipe.rb index 497f328ab..fb281dac4 100644 --- a/lib/recipe.rb +++ b/lib/recipe.rb @@ -2,12 +2,13 @@ require 'edamam_api_wrapper.rb' class Recipe - attr_reader :name, :image, :source, :ingredients, :url + attr_reader :name, :id, :image, :source, :ingredients, :url - def initialize(name, options = { }) - raise ArgumentError if name == nil || name == "" + def initialize(name, id, options = { }) + raise ArgumentError if name == nil || name == "" || id == nil || id == "" @name = name + @id = id.split("_")[1] @image = options[:image] @source = options[:source] From e73743e24ec48309377376330ab09054682e9c2c Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Wed, 2 Nov 2016 16:38:23 -0700 Subject: [PATCH 18/45] Made the root path the searches#new route --- app/controllers/searches_controller.rb | 4 ---- config/routes.rb | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/app/controllers/searches_controller.rb b/app/controllers/searches_controller.rb index 6aa77dc3a..49b0b19fb 100644 --- a/app/controllers/searches_controller.rb +++ b/app/controllers/searches_controller.rb @@ -9,10 +9,6 @@ def show def new; end - # class << self - # attr_writer :results - # end - def create @results ||= EdamamApiWrapper.listrecipes(params[:search_word]) render :index diff --git a/config/routes.rb b/config/routes.rb index 48ba26eda..e17be1c7b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,9 @@ Rails.application.routes.draw do resources :homepages, only: [:index] + get '/', to: 'searches#new', as: 'root' get '/searches/', to: 'searches#index', as: 'searches' post '/searches/', to: 'searches#create', as: 'create_search' - get '/searches/new/', to: 'searches#new', as: 'new_search' get '/searches/:id/', to: 'searches#show', as: 'search' end From 67b28c21f5cb01475a501115d7a728275bc836cf Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Thu, 3 Nov 2016 15:48:00 -0700 Subject: [PATCH 19/45] Cleaning up the file a bit --- lib/recipe.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/recipe.rb b/lib/recipe.rb index fb281dac4..02506e2bd 100644 --- a/lib/recipe.rb +++ b/lib/recipe.rb @@ -16,8 +16,4 @@ def initialize(name, id, options = { }) @url = options[:url] end - # def self.all(search_word) - # EdamamApiWrapper.listrecipes(search_word) - # end - end From 5890af1c6d8f7e4d5068c14c5c73969ecc0dafb0 Mon Sep 17 00:00:00 2001 From: Nicole Url Date: Thu, 3 Nov 2016 15:49:07 -0700 Subject: [PATCH 20/45] Added forward pagination powers --- app/controllers/searches_controller.rb | 15 +++++++++++++++ app/views/searches/index.html.erb | 8 ++++++-- config/routes.rb | 3 ++- lib/edamam_api_wrapper.rb | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/controllers/searches_controller.rb b/app/controllers/searches_controller.rb index 49b0b19fb..08e96f20c 100644 --- a/app/controllers/searches_controller.rb +++ b/app/controllers/searches_controller.rb @@ -1,6 +1,8 @@ class SearchesController < ApplicationController def index @results + @search_word + @search_index end def show @@ -11,6 +13,19 @@ def new; end def create @results ||= EdamamApiWrapper.listrecipes(params[:search_word]) + @search_word = params[:search_word] + @search_index = 0 + render :index + end + + def next + params[:search_index] = params[:search_index].to_i + 10 + + @search_index = params[:search_index] + @search_word = params[:search_word] + + @results ||= EdamamApiWrapper.nextpage(@search_index, @search_word) + render :index end diff --git a/app/views/searches/index.html.erb b/app/views/searches/index.html.erb index 45b779f70..6ba74416b 100644 --- a/app/views/searches/index.html.erb +++ b/app/views/searches/index.html.erb @@ -1,5 +1,9 @@ -

Ten Search results

+

Search results from <%= @search_index %> to <%= @search_index.to_i + 10 %> for <%= @search_word %>

+<%= link_to "next page", next_path(search_index: @search_index, search_word: @search_word) %> + +