Class: BetterSeo::StructuredData::HowTo

Inherits:
Base
  • Object
show all
Defined in:
lib/better_seo/structured_data/how_to.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) ⇒ HowTo

Returns a new instance of HowTo.



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

def initialize(**properties)
  super("HowTo", **properties)
  @steps = []
end

Instance Attribute Details

#stepsObject (readonly)

Returns the value of attribute steps.



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

def steps
  @steps
end

Instance Method Details

#add_step(name:, text:, image: nil, url: nil, position: nil) ⇒ Object

Add a single step



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/better_seo/structured_data/how_to.rb', line 39

def add_step(name:, text:, image: nil, url: nil, position: nil)
  position ||= @steps.size + 1
  @steps << {
    name: name,
    text: text,
    image: image,
    url: url,
    position: position
  }
  self
end

#add_steps(steps_array) ⇒ Object

Add multiple steps from an array



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/better_seo/structured_data/how_to.rb', line 52

def add_steps(steps_array)
  steps_array.each do |step|
    add_step(
      name: step[:name],
      text: step[:text],
      image: step[:image],
      url: step[:url],
      position: step[:position]
    )
  end
  self
end

#clearObject

Clear all steps



66
67
68
69
70
# File 'lib/better_seo/structured_data/how_to.rb', line 66

def clear
  @steps = []
  @properties.delete(:step)
  self
end

#description(value) ⇒ Object



18
19
20
# File 'lib/better_seo/structured_data/how_to.rb', line 18

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

#image(value) ⇒ Object



22
23
24
# File 'lib/better_seo/structured_data/how_to.rb', line 22

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

#name(value) ⇒ Object

Basic properties



14
15
16
# File 'lib/better_seo/structured_data/how_to.rb', line 14

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

#supply(value) ⇒ Object



30
31
32
# File 'lib/better_seo/structured_data/how_to.rb', line 30

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

#to_hObject

Override to_h to include steps



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/better_seo/structured_data/how_to.rb', line 73

def to_h
  hash = super

  if @steps.any?
    hash["step"] = @steps.map do |step|
      step_hash = {
        "@type" => "HowToStep",
        "name" => step[:name],
        "text" => step[:text],
        "position" => step[:position]
      }

      step_hash["image"] = step[:image] if step[:image]
      step_hash["url"] = step[:url] if step[:url]

      step_hash
    end
  end

  hash
end

#tool(value) ⇒ Object



34
35
36
# File 'lib/better_seo/structured_data/how_to.rb', line 34

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

#total_time(value) ⇒ Object



26
27
28
# File 'lib/better_seo/structured_data/how_to.rb', line 26

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