Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipes - Sairagul - Muncher #48

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Show page added
sairagula committed Nov 3, 2017
commit 1af348ba3dfa83a6e2a78e6714c853f523fff9d4
25 changes: 21 additions & 4 deletions app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
class RecipesController < ApplicationController

def index
def index # Main page that we search for food
end

def results
@recipes = EdamamApiMuncher.search(params[:q], params[:from], params[:to])
def results # Gives all results for the search
if params[:q] == ""
flash[:status] = :failure
flash[:message] = "Please enter a search word"
redirect_back(fallback_location: root_path)
else
@recipes = EdamamApiMuncher.search(params[:q], params[:from], params[:to])
flash[:status] = :success
flash[:message] = "Search is successful"
end
end

def show
Recipe r1 = Recipe.new("http://google.com/1", "First Fake data for " + params[:id])
# Recipe r1 = Recipe.new("http://google.com/1", "First Fake data for " + params[:id])
@recipe = EdamamApiMuncher.find(params[:id])
if @recipe != []
flash[:status] = :success
flash[:message] = "Success, recipe has been found"
else
flash[:status] = :failure
flash[:message] = "Please enter a valid uri id"
redirect_back(fallback_location: root_path)
end
end

end
8 changes: 4 additions & 4 deletions app/views/recipes/results.html.erb
Original file line number Diff line number Diff line change
@@ -3,15 +3,15 @@
<ul>
<% @recipes.each do |recipe| %>
<li>
<%= image_tag recipe.image %>,
<p><%= link_to recipe.title, show_path(recipe.uri) %></p>
<%= image_tag(recipe.image) %>,
<p><%= link_to recipe.title, show_path(recipe.id) %></p>
</li>
<% end %>
</ul>


<% if params[:from] != "0" %>
<%= link_to "Prev", results_path(:q => params[:q], :from => params[:from].to_i() -10, :to => params[:from]) %>
<%end%>
<% end %>
<%= link_to "Next", results_path(:q => params[:q], :from => params[:to], :to => params[:to].to_i() + 10) %>
</section>
</section>
6 changes: 5 additions & 1 deletion app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<h2> Details of the recipe</h2>
<h2> <%= @recipe.title %> </h2>
<%= image_tag @recipe.image %>
<p> Ingredients:<%= @recipe.ingredient_lines %></p>
<%= @recipe.url %>
<p> Dietary Labels:<%= @recipe.diet_label %></p>
36 changes: 28 additions & 8 deletions lib/edamam_api_muncher.rb
Original file line number Diff line number Diff line change
@@ -2,19 +2,34 @@
# require "pry"
#
class EdamamApiMuncher
BASE_URL = "https://api.edamam.com/search?q="
BASE_URL = "https://api.edamam.com/search"
RECIPE_URI_PREFIX = "http://www.edamam.com/ontologies/edamam.owl%23recipe_"
APP_ID = ENV["APP_ID"]
API_KEY = ENV["API_KEY"]

def self.search(query, from, to)
url = BASE_URL + query + "&app_id=#{APP_ID}&app_key=#{API_KEY}&from=#{from}&to=#{to}"
url = BASE_URL + "?q=#{query}" + "&app_id=#{APP_ID}&app_key=#{API_KEY}&from=#{from}&to=#{to}"
data = HTTParty.get(url)
binding.pry
recipes = []
if data["hits"]
data["hits"].each do |recipe|
recipes << create_recipe(recipe["recipe"])
end
end
return recipes
end

recipe_list = []
data["hits"].each do |recipe|
recipe_list << create_recipe(recipe["recipe"])
def self.find(id)
url = BASE_URL + "?r=" + RECIPE_URI_PREFIX + "#{id}" + "&app_id=#{APP_ID}&app_key=#{API_KEY}"
puts "Requesting recipe #{id}"
puts "url is #{url}"
data = HTTParty.get(url)
unless data.empty?
return create_recipe(data[0])
else
return nil
end
return recipe_list
end

private
@@ -23,7 +38,12 @@ def self.create_recipe(recipe)
return Recipe.new(
recipe["uri"],
recipe["label"],
recipe
recipe["image"],
options = {
diet_label: recipe["dietLabels"],
ingredient_lines: recipe["ingredientLines"],
url: recipe["url"]
}
)
end
end
end
15 changes: 10 additions & 5 deletions lib/recipe.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
class Recipe
attr_reader :uri, :title, :image, :diet_label, :url, :ingredient_lines

def initialize(uri, title, options = {} )
def initialize(uri, title, image, options = {} )
raise ArgumentError if title == nil || title == "" || uri == nil || uri == ""


@title = title
@uri = uri
@image = image

@image = options["image"]
@diet_label = options["dietLabels"]
@url = options["url"]
@ingredient_lines = options["ingredientLines"]
@diet_label = options[:dietLabels]
@url = options[:url]
@ingredient_lines = options[:ingredientLines]
end

def id
return self.uri[/(?<=_)[a-zA-Z0-9]+/]
end

end