Class: BetterSeo::StructuredData::Base
- Inherits:
-
Object
- Object
- BetterSeo::StructuredData::Base
show all
- Defined in:
- lib/better_seo/structured_data/base.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(type, **properties) ⇒ Base
Returns a new instance of Base.
10
11
12
13
14
|
# File 'lib/better_seo/structured_data/base.rb', line 10
def initialize(type, **properties)
@type = type
@properties = {}
properties.each { |key, value| set(key, value) }
end
|
Instance Attribute Details
#properties ⇒ Object
Returns the value of attribute properties.
8
9
10
|
# File 'lib/better_seo/structured_data/base.rb', line 8
def properties
@properties
end
|
#type ⇒ Object
Returns the value of attribute type.
8
9
10
|
# File 'lib/better_seo/structured_data/base.rb', line 8
def type
@type
end
|
Instance Method Details
#get(key) ⇒ Object
21
22
23
|
# File 'lib/better_seo/structured_data/base.rb', line 21
def get(key)
@properties[key]
end
|
#set(key, value) ⇒ Object
16
17
18
19
|
# File 'lib/better_seo/structured_data/base.rb', line 16
def set(key, value)
@properties[key] = value unless value.nil?
self
end
|
#to_h ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/better_seo/structured_data/base.rb', line 25
def to_h
hash = {
"@context" => "https://schema.org",
"@type" => @type
}
@properties.each do |key, value|
hash[key.to_s] = convert_value(value)
end
hash
end
|
#to_json(*_args) ⇒ Object
38
39
40
|
# File 'lib/better_seo/structured_data/base.rb', line 38
def to_json(*_args)
JSON.pretty_generate(to_h)
end
|
#to_script_tag ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/better_seo/structured_data/base.rb', line 42
def to_script_tag
<<~HTML.strip
<script type="application/ld+json">
#{to_json}
</script>
HTML
end
|
#valid? ⇒ Boolean
50
51
52
|
# File 'lib/better_seo/structured_data/base.rb', line 50
def valid?
!@type.nil? && !@type.to_s.empty?
end
|
#validate! ⇒ Object
54
55
56
57
58
|
# File 'lib/better_seo/structured_data/base.rb', line 54
def validate!
raise ValidationError, "@type is required for structured data" unless valid?
true
end
|