Class: SchemaDotOrg::SchemaType

Inherits:
ValidatedObject::Base
  • Object
show all
Defined in:
lib/schema_dot_org.rb

Constant Summary collapse

ROOT_ATTR =
{ "@context" => "https://schema.org" }.freeze
UNQUALIFIED_CLASS_NAME_REGEX =
/([^:]+)$/

Instance Method Summary collapse

Instance Method Details

#_to_json_structObject



49
50
51
# File 'lib/schema_dot_org.rb', line 49

def _to_json_struct
  attrs_and_values
end

#attrsObject



100
101
102
# File 'lib/schema_dot_org.rb', line 100

def attrs
  instance_variables.reject{ |v| [:@validation_context, :@errors].include?(v) }
end

#attrs_and_valuesObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/schema_dot_org.rb', line 67

def attrs_and_values
  attrs.map do |attr|
    # Clean up and andle the `query-input` attribute, which
    # doesn't follow the normal camelCase convention.
    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

Returns:

  • (Boolean)


105
106
107
# File 'lib/schema_dot_org.rb', line 105

def is_schema_type?(object)
  object.class.module_parent == SchemaDotOrg
end

#object_to_json_struct(object) ⇒ Object



61
62
63
64
# File 'lib/schema_dot_org.rb', line 61

def object_to_json_struct(object)
  return nil unless object
  object.to_json_struct
end

#rails_production?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/schema_dot_org.rb', line 110

def rails_production?
  defined?(Rails) && Rails.env.production?
end

#resolve_value(value) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/schema_dot_org.rb', line 79

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



95
96
97
# File 'lib/schema_dot_org.rb', line 95

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



31
32
33
34
35
36
37
38
39
# File 'lib/schema_dot_org.rb', line 31

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



26
27
28
# 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_structObject

Use the class name to create the “@type” attribute.

Returns:

  • a hash structure representing json.



44
45
46
# File 'lib/schema_dot_org.rb', line 44

def to_json_struct
  { "@type" => un_namespaced_classname }.merge(_to_json_struct.reject { |_, v| v.blank? })
end

#to_sObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/schema_dot_org.rb', line 14

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_classnameObject

Returns the classname without the module namespace.

Returns:

  • the classname without the module namespace.



55
56
57
58
# File 'lib/schema_dot_org.rb', line 55

def un_namespaced_classname
  self.class.name =~ UNQUALIFIED_CLASS_NAME_REGEX
  Regexp.last_match(1)
end