Class: Food

Inherits:
Object
  • Object
show all
Defined in:
lib/food.rb

Overview

Represents single recipe from Yummly

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(food_json) ⇒ Food

Creates food with the parameters from JSON

Parameters:

  • food_json (JSON)

    recipe encapsulated in json from Yummly



9
10
11
12
13
14
15
16
# File 'lib/food.rb', line 9

def initialize(food_json)
  @name = food_json['name']
  @id = food_json['id']
  @aprox_time = food_json['totalTime']
  @image_url = image_url food_json
  @ingredients = food_json['ingredientLines']
  @guide_url = food_json['source']['sourceRecipeUrl']
end

Instance Attribute Details

#idObject (readonly)

the id of the recipe on Yummly



4
5
6
# File 'lib/food.rb', line 4

def id
  @id
end

Instance Method Details

#image_url(food_json) ⇒ String

Extract image from json

Parameters:

  • food_json (JSON)

    recipe encapsulated in json from Yummly

Returns:

  • (String)

    URL of the image



22
23
24
25
26
# File 'lib/food.rb', line 22

def image_url(food_json)
  url = food_json['images'][0]['hostedLargeUrl']
  url = 'Sorry, I can not provide you picture of this food' if url.nil?
  url
end

#to_strString

Provides stats of the user in human-readble form

Returns:

  • (String)

    stats of the user



31
32
33
34
35
36
37
38
39
40
# File 'lib/food.rb', line 31

def to_str
  result = @name
  result += "\nID: #{@id}"
  result += "\n\nApproximate time: #{@aprox_time}"
  result += "\nImage URL: #{@image_url}\n"
  result += "\nIngredients:\n"
  result += @ingredients.join("\n")
  result += "\n\nStep-by-step:  #{@guide_url}\n"
  result
end