Class: BetterSeo::StructuredData::FAQPage

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

Returns a new instance of FAQPage.



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

def initialize(**properties)
  super("FAQPage", **properties)
  @questions = []
end

Instance Attribute Details

#questionsObject (readonly)

Returns the value of attribute questions.



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

def questions
  @questions
end

Instance Method Details

#add_question(question:, answer:) ⇒ Object

Add a single question



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

def add_question(question:, answer:)
  @questions << { question: question, answer: answer }
  self
end

#add_questions(questions_array) ⇒ Object

Add multiple questions from an array



20
21
22
23
24
25
# File 'lib/better_seo/structured_data/faq_page.rb', line 20

def add_questions(questions_array)
  questions_array.each do |q|
    add_question(question: q[:question], answer: q[:answer])
  end
  self
end

#clearObject

Clear all questions



28
29
30
31
32
# File 'lib/better_seo/structured_data/faq_page.rb', line 28

def clear
  @questions = []
  @properties.delete(:mainEntity)
  self
end

#to_hObject

Override to_h to include mainEntity



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/better_seo/structured_data/faq_page.rb', line 35

def to_h
  hash = super

  if @questions.any?
    hash["mainEntity"] = @questions.map do |q|
      {
        "@type" => "Question",
        "name" => q[:question],
        "acceptedAnswer" => {
          "@type" => "Answer",
          "text" => q[:answer]
        }
      }
    end
  end

  hash
end