Class: BetterSeo::StructuredData::FAQPage
- Defined in:
- lib/better_seo/structured_data/faq_page.rb
Instance Attribute Summary collapse
-
#questions ⇒ Object
readonly
Returns the value of attribute questions.
Attributes inherited from Base
Instance Method Summary collapse
-
#add_question(question:, answer:) ⇒ Object
Add a single question.
-
#add_questions(questions_array) ⇒ Object
Add multiple questions from an array.
-
#clear ⇒ Object
Clear all questions.
-
#initialize(**properties) ⇒ FAQPage
constructor
A new instance of FAQPage.
-
#to_h ⇒ Object
Override to_h to include mainEntity.
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
#questions ⇒ Object (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 |
#clear ⇒ Object
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_h ⇒ Object
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 |