Class: WebAuthor::LdSchema

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/web_author/ld_schema.rb

Constant Summary collapse

AuthorBaseType =
T.type_alias { T.any(String, T::Hash[String, T.untyped]) }
ArrayofAuthors =
T.type_alias { T::Array[AuthorBaseType] }
AuthorType =
T.type_alias { T.any(AuthorBaseType, ArrayofAuthors) }
ATTRIBUTES =
T.let([
  '@context', '@type', '@id', 'name', 'description', 'url', 'image',
  'author', 'headline', 'publisher', 'blogPost'
].freeze, T::Array[String])

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_hash(hash) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/web_author/ld_schema.rb', line 39

def self.from_hash(hash)
  main_properties = hash.dup.select { |key, _| ATTRIBUTES.include?(key) }
  main_properties['blog_post'] = main_properties.delete('blogPost')
  main_properties.transform_keys! { |it| it.start_with?('@') ? it.sub('@', '').to_sym : it.to_sym }

  additional_properties = hash.dup
  ATTRIBUTES.each { |it| additional_properties.delete(it) }
  additional_properties.transform_keys!(&:to_s)

  new(**main_properties, additional_properties:)
end

.from_script_tag(script_html_tag) ⇒ Object



33
34
35
36
# File 'lib/web_author/ld_schema.rb', line 33

def self.from_script_tag(script_html_tag)
  hash = JSON.parse(script_html_tag)
  from_hash(hash)
end

Instance Method Details

#parsed_authorObject



52
53
54
55
56
# File 'lib/web_author/ld_schema.rb', line 52

def parsed_author
  return @_parsed_author if defined?(@_parsed_author)

  @_parsed_author = T.let(parse_author, T.nilable(T.any(LdAuthor, T::Array[LdAuthor])))
end