Sha256: aae3e3e537a653ba778658e19399eca9d0ddebd6451894aca5c5a357acd8bfb6
Contents?: true
Size: 1.55 KB
Versions: 3
Compression:
Stored size: 1.55 KB
Contents
require 'json' require 'validated_object' module SchemaDotOrg class SchemaType < ValidatedObject::Base ROOT_ATTR = { "@context" => "http://schema.org" }.freeze UNQUALIFIED_CLASS_NAME_REGEX = /([^:]+)$/ def to_s json_string = to_json_ld(pretty: (!rails_production? && !ENV['SCHEMA_DOT_ORG_MINIFIED_JSON'])) # Mark as safe if we're in Rails if json_string.respond_to?(:html_safe) json_string.html_safe else json_string end end def to_json_ld(pretty: false) "<script type=\"application/ld+json\">\n" + to_json(pretty: pretty, as_root: true) + "\n</script>" end 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 # Use the class name to create the "@type" attribute. # @return a hash structure representing json. def to_json_struct { "@type" => un_namespaced_classname }.merge(_to_json_struct.reject { |_, v| v.blank? }) end def _to_json_struct raise "For subclasses to implement" end # @return the classname without the module namespace. def un_namespaced_classname self.class.name =~ UNQUALIFIED_CLASS_NAME_REGEX Regexp.last_match(1) end def object_to_json_struct(object) return nil unless object object.to_json_struct end private def rails_production? defined?(Rails) && Rails.env.production? end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
schema_dot_org-2.2.0 | lib/schema_dot_org/schema_type.rb |
schema_dot_org-2.1 | lib/schema_dot_org/schema_type.rb |
schema_dot_org-2.0 | lib/schema_dot_org/schema_type.rb |