class SchemaDotOrg::SchemaType

Base class for schema types. Refactors out common code.

Constants

ROOT_ATTR
UNQUALIFIED_CLASS_NAME_REGEX

Public Instance Methods

_to_json_struct() click to toggle source
# File lib/schema_dot_org.rb, line 46
def _to_json_struct
  raise 'For subclasses to implement'
end
to_json(pretty: false, as_root: false) click to toggle source
# File lib/schema_dot_org.rb, line 30
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) click to toggle source
# File lib/schema_dot_org.rb, line 26
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() click to toggle source

Use the class name to create the “@type” attribute. @return a hash structure representing json.

# File lib/schema_dot_org.rb, line 42
def to_json_struct
  { '@type' => un_namespaced_classname }.merge(_to_json_struct.compact)
end
to_s() click to toggle source
# File lib/schema_dot_org.rb, line 15
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
un_namespaced_classname() click to toggle source

@return the classname without the module namespace.

# File lib/schema_dot_org.rb, line 51
def un_namespaced_classname
  self.class.name =~ UNQUALIFIED_CLASS_NAME_REGEX
  Regexp.last_match(1)
end

Private Instance Methods

rails_production?() click to toggle source
# File lib/schema_dot_org.rb, line 58
def rails_production?
  defined?(Rails) && Rails.env.production?
end