Class: SchemaDotOrg::SchemaType
- Inherits:
-
ValidatedObject::Base
- Object
- ValidatedObject::Base
- SchemaDotOrg::SchemaType
show all
- Defined in:
- lib/schema_dot_org.rb
Overview
Abstract base class for all the Schema.org types.
Direct Known Subclasses
AggregateOffer, BreadcrumbList, CollegeOrUniversity, Comment, ContactPoint, DiscussionForumPosting, InteractionCounter, ItemList, Language, ListItem, Offer, Organization, Person, Place, Product, SearchAction, WebSite
Constant Summary
collapse
- EXCLUDED_INSTANCE_VARIABLES =
%i[@context_for_validation @validation_context @errors].freeze
- ROOT_ATTR =
{ "@context" => "https://schema.org" }.freeze
- UNQUALIFIED_CLASS_NAME_REGEX =
/([^:]+)$/
Instance Method Summary
collapse
Instance Method Details
#_to_json_struct ⇒ Object
81
82
83
|
# File 'lib/schema_dot_org.rb', line 81
def _to_json_struct
attrs_and_values
end
|
#attrs ⇒ Object
132
133
134
|
# File 'lib/schema_dot_org.rb', line 132
def attrs
instance_variables.reject{ |v| EXCLUDED_INSTANCE_VARIABLES.include?(v) }
end
|
#attrs_and_values ⇒ Object
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/schema_dot_org.rb', line 99
def attrs_and_values
attrs.map do |attr|
attr_name = snake_case_to_lower_camel_case(attr.to_s.delete_prefix('@')).sub('queryInput', 'query-input')
attr_value = instance_variable_get(attr)
[attr_name, resolve_value(attr_value)]
end.to_h
end
|
#is_schema_type?(object) ⇒ Boolean
137
138
139
|
# File 'lib/schema_dot_org.rb', line 137
def is_schema_type?(object)
object.class.module_parent == SchemaDotOrg
end
|
#object_to_json_struct(object) ⇒ Object
93
94
95
96
|
# File 'lib/schema_dot_org.rb', line 93
def object_to_json_struct(object)
return nil unless object
object.to_json_struct
end
|
#rails_production? ⇒ Boolean
142
143
144
|
# File 'lib/schema_dot_org.rb', line 142
def rails_production?
defined?(Rails) && Rails.env.production?
end
|
#resolve_value(value) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/schema_dot_org.rb', line 111
def resolve_value(value)
if value.is_a?(Array)
value.map { |v| resolve_value(v) }
elsif value.is_a?(Date)
value.to_s
elsif is_schema_type?(value)
value.to_json_struct
else
value
end
end
|
#snake_case_to_lower_camel_case(snake_case) ⇒ Object
127
128
129
|
# File 'lib/schema_dot_org.rb', line 127
def snake_case_to_lower_camel_case(snake_case)
snake_case.to_s.split('_').map.with_index { |s, i| i.zero? ? s : s.capitalize }.join
end
|
#to_json(pretty: false, as_root: false) ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/schema_dot_org.rb', line 63
def to_json(pretty: false, as_root: false)
structure = as_root ? ROOT_ATTR.merge(to_json_struct) : to_json_struct
if pretty
JSON.pretty_generate(structure)
else
structure.to_json
end
end
|
#to_json_ld(pretty: false) ⇒ Object
58
59
60
|
# File 'lib/schema_dot_org.rb', line 58
def to_json_ld(pretty: false)
"<script type=\"application/ld+json\">\n" + to_json(pretty: pretty, as_root: true) + "\n</script>"
end
|
#to_json_struct ⇒ Object
Use the class name to create the “@type” attribute.
76
77
78
|
# File 'lib/schema_dot_org.rb', line 76
def to_json_struct
{ "@type" => un_namespaced_classname }.merge(_to_json_struct.reject { |_, v| v.blank? })
end
|
#to_s ⇒ Object
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/schema_dot_org.rb', line 46
def to_s
json_string = to_json_ld(pretty: (!rails_production? && !ENV['SCHEMA_DOT_ORG_MINIFIED_JSON']))
if json_string.respond_to?(:html_safe)
json_string.html_safe
else
json_string
end
end
|
#un_namespaced_classname ⇒ Object
Returns the classname without the module namespace.
87
88
89
90
|
# File 'lib/schema_dot_org.rb', line 87
def un_namespaced_classname
self.class.name =~ UNQUALIFIED_CLASS_NAME_REGEX
Regexp.last_match(1)
end
|