Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
/log/*
!/log/.keep
/tmp

.env
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ gem 'sdoc', '~> 0.4.0', group: :doc
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

gem 'httparty'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'dotenv-rails'
gem 'better_errors'
gem 'minitest-vcr'
gem 'webmock'
end

group :development do
Expand Down
27 changes: 27 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
addressable (2.4.0)
arel (6.0.3)
better_errors (2.1.1)
coderay (>= 1.0.0)
Expand All @@ -54,11 +55,20 @@ 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)
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)
jbuilder (2.6.0)
activesupport (>= 3.0.0, < 5.1)
Expand All @@ -76,8 +86,15 @@ 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)
nokogiri (1.6.8.1)
mini_portile2 (~> 2.1.0)
rack (1.6.4)
Expand Down Expand Up @@ -110,6 +127,7 @@ GEM
rake (11.3.0)
rdoc (4.2.2)
json (~> 1.4)
safe_yaml (1.0.4)
sass (3.4.22)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
Expand Down Expand Up @@ -140,11 +158,16 @@ GEM
thread_safe (~> 0.1)
uglifier (3.0.3)
execjs (>= 0.3.0, < 3)
vcr (3.0.3)
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.1.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff

PLATFORMS
ruby
Expand All @@ -153,8 +176,11 @@ DEPENDENCIES
better_errors
byebug
coffee-rails (~> 4.1.0)
dotenv-rails
httparty
jbuilder (~> 2.0)
jquery-rails
minitest-vcr
rails (= 4.2.7)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
Expand All @@ -163,6 +189,7 @@ DEPENDENCIES
turbolinks
uglifier (>= 1.3.0)
web-console (~> 2.0)
webmock

BUNDLED WITH
1.13.2
3 changes: 3 additions & 0 deletions app/assets/javascripts/homepages.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/homepages.scss
Original file line number Diff line number Diff line change
@@ -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/
18 changes: 18 additions & 0 deletions app/controllers/homepages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class HomepagesController < ApplicationController
def index
end

def recipe
# @recipes = Recipe.all
if params[:search]
@recipes = EdamamApiWrapper.list_recipes(params[:search])
# @q = params[:search]
# else
# @recipes = Recipe.all.order("created_at DESC")
end
end

def show
@recipe = EdamamApiWrapper.find_recipe(params[:id])
end
end
2 changes: 2 additions & 0 deletions app/helpers/homepages_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module HomepagesHelper
end
1 change: 1 addition & 0 deletions app/views/homepages/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

7 changes: 7 additions & 0 deletions app/views/homepages/recipe.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% if @recipes.blank? %>
<h4>There are no recipes containing the term <%= params[:search] %>.</h4>
<% end %>

<% @recipes.each do |recipe| %>
<p><img src="<%= recipe.image %>" alt="food picture"> <%= link_to recipe.name, show_path(recipe.id) %></p>
<% end %>
15 changes: 15 additions & 0 deletions app/views/homepages/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h2>Recipe for <%= @recipe.name %></h2>

<h3><%= link_to( "Original recipe", @recipe.url ) %></h3>
<h3>Ingredients</h3>
<% @recipe.ingredients.each do |ingredient| %>
<p><%= ingredient %></p>
<% end %>
<h3>Diet information</h3>
<% @recipe.diet_labels.each do |diet| %>
<p><%= diet %></p>
<% end %>
<h3>Health information</h3>
<% @recipe.health_labels.each do |health| %>
<p><%= health %></p>
<% end %>
5 changes: 5 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<%= csrf_meta_tags %>
</head>
<body>
<h2>Search Recipes Here!</h2>
<%= form_tag(recipes_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Recipes" %>
<%= submit_tag "Search", :name => nil %>
<% end %>

<%= yield %>

Expand Down
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ 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 << Rails.root.join('lib')
end
end
10 changes: 10 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
Rails.application.routes.draw do

root to: 'homepages#index'

get 'homepages/index'

get 'homepages/recipe' => 'homepages#recipe', as: 'recipes'

get 'homepages/show/:id' => 'homepages#show', as: 'show'


# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
52 changes: 52 additions & 0 deletions lib/edamam_api_wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'httparty'

class EdamamApiWrapper
BASE_URL = "https://api.edamam.com/search"
APP_ID = ENV["APP_ID"]
APP_KEY = ENV["APP_KEY"]

attr_reader :q, :name

def initialize(q)
@q = q
end

def self.list_recipes(q)

url = BASE_URL + "?app_id=#{APP_ID}" + "&app_key=#{APP_KEY}" + "&q=#{q}"
data = HTTParty.get(url)
recipes = []
if data["hits"]
data["hits"].each do |hit|
wrapper = Recipe.new hit["recipe"]["label"], hit["recipe"]["uri"], hit["recipe"]["image"], hit["recipe"]["label"], hit["recipe"]["ingredientLines"], hit["recipe"]["dietLabels"], hit["recipe"]["healthLabels"]
recipes << wrapper
end
return recipes
end
end

def self.find_recipe(id)
url = BASE_URL + "?app_id=#{APP_ID}" + "&app_key=#{APP_KEY}" + "&q=#{id}"
data = HTTParty.get(url)
data["hits"].each do |hit|
if hit["recipe"]["label"] == id
@recipe = Recipe.new hit["recipe"]["label"], hit["recipe"]["url"], hit["recipe"]["image"], hit["recipe"]["label"], hit["recipe"]["ingredientLines"], hit["recipe"]["dietLabels"], hit["recipe"]["healthLabels"]
end
end
return @recipe
end

def self.get_recipes(q, app_id = nil, app_key = nil)
app_id = APP_ID if app_id == nil
app_key = APP_KEY if app_key == nil

url = BASE_URL + "?app_id=#{app_id}" + "&app_key=#{app_key}"
puts url
data = HTTParty.post(url,
body: {
"q" => "#{q}"
},
:headers => { 'Content-Type' => 'application/x-www-form-urlencoded' })
end

end
21 changes: 21 additions & 0 deletions lib/recipe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Recipe
attr_reader :name, :url, :image, :id, :ingredients, :diet_labels, :health_labels

def initialize(name, url, image, id, ingredients, diet_labels, health_labels)
@name = name
@url = url
@image = image
@id = id
@ingredients = ingredients
@diet_labels = diet_labels
@health_labels = health_labels


# if q = nil || q = ""
# raise ArgumentError
# end

end


end
14 changes: 14 additions & 0 deletions test/controllers/homepages_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'test_helper'

class HomepagesControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end

test "should get show" do
get :show
assert_response :success
end

end
16 changes: 16 additions & 0 deletions test/lib/recipe_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'test_helper'
require 'recipe'

class RecipeTest < ActionController::TestCase

#just to verify that Rake can pick up the test
test "the truth" do
assert true
end

test "must provide a query for Recipe" do
assert_raises ArgumentError do
Recipe.new nil
end
end
end