Skip to content

Commit

Permalink
made basic show page but still in the middle of changing routes to fi…
Browse files Browse the repository at this point in the history
…x index
  • Loading branch information
add2point71dots committed May 3, 2017
1 parent 93b60ef commit fb550cc
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class RecipesController < ApplicationController
def index
@recipes = EdamamApiWrapper.list_recipes(params[:search])
end

def show
@recipe = EdamamApiWrapper.show_recipe(params[:uri])
end
end
5 changes: 4 additions & 1 deletion app/views/recipes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<ul>
<% @recipes.each do |recipe| %>
<li><%= recipe.label %></li>
<li>
<%= image_tag recipe.image, alt: recipe.label %>
<%= link_to recipe.label, recipe_path(recipe.uri) %>
</li>
<% end %>
</ul>
3 changes: 3 additions & 0 deletions app/views/recipes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h2><%= @recipe.label %></h2>

<%= image_tag @recipe.image, alt: @recipe.label %>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Rails.application.routes.draw do
root 'home#index'

get '/recipes/:search', to: 'recipes#index', as: 'recipes'
get '/recipes', to: 'recipes#index', as: 'recipes'
get '/recipes/:uri', to: 'recipes#show', as: 'recipe'
end
20 changes: 19 additions & 1 deletion lib/edamam_api_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,28 @@ def self.list_recipes(search)

if response["hits"]
return response["hits"].map do |recipe|
Recipe.new(recipe["recipe"]["label"], recipe["recipe"]["uri"].gsub("#", "%23"))
Recipe.new(recipe["recipe"]["label"], recipe["recipe"]["uri"].partition("recipe_").last, recipe["recipe"]["image"])
end
else
return []
end
end

def self.show_recipe(uri)
url = BASE_URL + "r=http://www.edamam.com/ontologies/edamam.owl%23recipe_#{uri}"
response = HTTParty.get(url)[0]

if response
options = {
recipe_link: response["url"],
ingredients: response["ingredientLines"],
diet_labels: response["dietLabels"],
health_labels: response["healthLabels"],
calories: response["calories"]
}
return Recipe.new(response["label"], response["uri"].partition("recipe_").last, response["image"], options )
else
return nil
end
end
end
5 changes: 3 additions & 2 deletions lib/recipe.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class Recipe
attr_reader :label, :uri
attr_reader :label, :uri, :image

def initialize(label, id, options= {})
def initialize(label, uri, image, options = {})
@label = label
@uri = uri
@image = image
end
end

0 comments on commit fb550cc

Please sign in to comment.