Class: BetterSeo::StructuredData::Recipe

Inherits:
Base
  • Object
show all
Defined in:
lib/better_seo/structured_data/recipe.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#properties, #type

Instance Method Summary collapse

Methods inherited from Base

#get, #set, #to_json, #to_script_tag, #valid?, #validate!

Constructor Details

#initialize(**properties) ⇒ Recipe

Returns a new instance of Recipe.



8
9
10
11
12
# File 'lib/better_seo/structured_data/recipe.rb', line 8

def initialize(**properties)
  super("Recipe", **properties)
  @ingredients = []
  @instructions = []
end

Instance Attribute Details

#ingredientsObject (readonly)

Returns the value of attribute ingredients.



6
7
8
# File 'lib/better_seo/structured_data/recipe.rb', line 6

def ingredients
  @ingredients
end

#instructionsObject (readonly)

Returns the value of attribute instructions.



6
7
8
# File 'lib/better_seo/structured_data/recipe.rb', line 6

def instructions
  @instructions
end

Instance Method Details

#add_ingredient(ingredient) ⇒ Object

Ingredients



60
61
62
63
# File 'lib/better_seo/structured_data/recipe.rb', line 60

def add_ingredient(ingredient)
  @ingredients << ingredient
  self
end

#add_ingredients(ingredients_array) ⇒ Object



65
66
67
68
# File 'lib/better_seo/structured_data/recipe.rb', line 65

def add_ingredients(ingredients_array)
  @ingredients.concat(ingredients_array)
  self
end

#add_instruction(instruction) ⇒ Object

Instructions



71
72
73
74
# File 'lib/better_seo/structured_data/recipe.rb', line 71

def add_instruction(instruction)
  @instructions << instruction
  self
end

#add_instructions(instructions_array) ⇒ Object



76
77
78
79
# File 'lib/better_seo/structured_data/recipe.rb', line 76

def add_instructions(instructions_array)
  @instructions.concat(instructions_array)
  self
end

#aggregate_rating(rating_value:, review_count:, best_rating: 5, worst_rating: 1) ⇒ Object

Aggregate rating



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/better_seo/structured_data/recipe.rb', line 102

def aggregate_rating(rating_value:, review_count:, best_rating: 5, worst_rating: 1)
  rating = {
    "@type" => "AggregateRating",
    "ratingValue" => rating_value,
    "reviewCount" => review_count,
    "bestRating" => best_rating,
    "worstRating" => worst_rating
  }

  set(:aggregateRating, rating)
end

#author(value) ⇒ Object



27
28
29
# File 'lib/better_seo/structured_data/recipe.rb', line 27

def author(value)
  set(:author, value)
end

#cook_time(value) ⇒ Object



35
36
37
# File 'lib/better_seo/structured_data/recipe.rb', line 35

def cook_time(value)
  set(:cookTime, value)
end

#description(value) ⇒ Object



19
20
21
# File 'lib/better_seo/structured_data/recipe.rb', line 19

def description(value)
  set(:description, value)
end

#image(value) ⇒ Object



23
24
25
# File 'lib/better_seo/structured_data/recipe.rb', line 23

def image(value)
  set(:image, value)
end

#keywords(value) ⇒ Object



55
56
57
# File 'lib/better_seo/structured_data/recipe.rb', line 55

def keywords(value)
  set(:keywords, value)
end

#name(value) ⇒ Object

Basic properties



15
16
17
# File 'lib/better_seo/structured_data/recipe.rb', line 15

def name(value)
  set(:name, value)
end

#nutrition(calories: nil, fat_content: nil, carbohydrate_content: nil, protein_content: nil, sugar_content: nil, **other) ⇒ Object

Nutrition information



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/better_seo/structured_data/recipe.rb', line 82

def nutrition(calories: nil, fat_content: nil, carbohydrate_content: nil, protein_content: nil,
              sugar_content: nil, **other)
  nutrition_hash = {
    "@type" => "NutritionInformation"
  }

  nutrition_hash["calories"] = calories if calories
  nutrition_hash["fatContent"] = fat_content if fat_content
  nutrition_hash["carbohydrateContent"] = carbohydrate_content if carbohydrate_content
  nutrition_hash["proteinContent"] = protein_content if protein_content
  nutrition_hash["sugarContent"] = sugar_content if sugar_content

  other.each do |key, value|
    nutrition_hash[key.to_s.camelize(:lower)] = value if value
  end

  set(:nutrition, nutrition_hash)
end

#prep_time(value) ⇒ Object



31
32
33
# File 'lib/better_seo/structured_data/recipe.rb', line 31

def prep_time(value)
  set(:prepTime, value)
end

#recipe_category(value) ⇒ Object



47
48
49
# File 'lib/better_seo/structured_data/recipe.rb', line 47

def recipe_category(value)
  set(:recipeCategory, value)
end

#recipe_cuisine(value) ⇒ Object



51
52
53
# File 'lib/better_seo/structured_data/recipe.rb', line 51

def recipe_cuisine(value)
  set(:recipeCuisine, value)
end

#recipe_yield(value) ⇒ Object



43
44
45
# File 'lib/better_seo/structured_data/recipe.rb', line 43

def recipe_yield(value)
  set(:recipeYield, value)
end

#to_hObject

Override to_h to include ingredients and instructions



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/better_seo/structured_data/recipe.rb', line 115

def to_h
  hash = super

  hash["recipeIngredient"] = @ingredients if @ingredients.any?

  if @instructions.any?
    hash["recipeInstructions"] = @instructions.map.with_index(1) do |instruction, index|
      {
        "@type" => "HowToStep",
        "position" => index,
        "text" => instruction
      }
    end
  end

  hash
end

#total_time(value) ⇒ Object



39
40
41
# File 'lib/better_seo/structured_data/recipe.rb', line 39

def total_time(value)
  set(:totalTime, value)
end