Class: Apps::Common::Schema::Base
- Inherits:
-
Object
- Object
- Apps::Common::Schema::Base
- Defined in:
- lib/apps/common/schema/base.rb
Constant Summary collapse
- DEFAULT_CONTEXT =
'http://schema.org'
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
the schema.org URL if not the default.
-
#force_context ⇒ Object
readonly
the schema.org URL if not the default.
Instance Method Summary collapse
- #as_json(force_context: false) ⇒ Object
-
#initialize(**attrs) ⇒ Base
constructor
A new instance of Base.
- #serialize ⇒ Object
- #serialize_hash(hash, key: 'name', value: 'value') ⇒ Object
- #serialized_context ⇒ Object
- #to_json(force_context: false, pretty: false) ⇒ Object
- #to_script(force_context: false, pretty: true) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(**attrs) ⇒ Base
12 13 14 |
# File 'lib/apps/common/schema/base.rb', line 12 def initialize(**attrs) prune(attrs).each { |name, value| instance_variable_set(:"@#{name}", value) } end |
Instance Attribute Details
#context ⇒ Object (readonly)
the schema.org URL if not the default
9 10 11 |
# File 'lib/apps/common/schema/base.rb', line 9 def context @context end |
#force_context ⇒ Object (readonly)
the schema.org URL if not the default
9 10 11 |
# File 'lib/apps/common/schema/base.rb', line 9 def force_context @force_context end |
Instance Method Details
#as_json(force_context: false) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/apps/common/schema/base.rb', line 44 def as_json(force_context: false) serialized = serialize if force_context == true serialized['@context'] ||= DEFAULT_CONTEXT elsif force_context.is_a?(String) serialized['@context'] = force_context end prune serialized end |
#serialize ⇒ Object
20 21 22 23 24 25 |
# File 'lib/apps/common/schema/base.rb', line 20 def serialize { "@context" => serialized_context, "@type" => type } end |
#serialize_hash(hash, key: 'name', value: 'value') ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/apps/common/schema/base.rb', line 27 def serialize_hash(hash, key: 'name', value: 'value') hash.map do |k, v| { key => k, value => v } end end |
#serialized_context ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/apps/common/schema/base.rb', line 36 def serialized_context if force_context context || DEFAULT_CONTEXT else context end end |
#to_json(force_context: false, pretty: false) ⇒ Object
56 57 58 59 60 |
# File 'lib/apps/common/schema/base.rb', line 56 def to_json(force_context: false, pretty: false) hash = as_json(force_context: force_context) pretty ? JSON.pretty_generate(hash) : JSON.dump(hash) end |
#to_script(force_context: false, pretty: true) ⇒ Object
62 63 64 65 66 |
# File 'lib/apps/common/schema/base.rb', line 62 def to_script(force_context: false, pretty: true) json = to_json(pretty: pretty, force_context: force_context) %`<script type="application/ld+json">\n#{json}\n</script>` end |
#type ⇒ Object
16 17 18 |
# File 'lib/apps/common/schema/base.rb', line 16 def type @type ||= self.class.name.split('::')[-1] end |