Module: Bolognese::Readers::SchemaOrgReader

Included in:
Metadata
Defined in:
lib/bolognese/readers/schema_org_reader.rb

Constant Summary collapse

SO_TO_DC_RELATION_TYPES =
{
  "citation" => "References",
  "sameAs" => "IsIdenticalTo",
  "isPartOf" => "IsPartOf",
  "hasPart" => "HasPart",
  "isPredecessor" => "IsPreviousVersionOf",
  "isSuccessor" => "IsNewVersionOf"
}

Instance Method Summary collapse

Instance Method Details

#get_schema_org(id: nil, **options) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bolognese/readers/schema_org_reader.rb', line 62

def get_schema_org(id: nil, **options)
  return nil unless id.present?

  id = normalize_id(id)
  response = Maremma.get(id)
  doc = Nokogiri::XML(response.body.fetch("data", nil), nil, 'UTF-8')
  string = doc.at_xpath('//script[@type="application/ld+json"]')
  string = string.text if string.present?

  { "string" => string }
end

#read_schema_org(string: nil, **options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bolognese/readers/schema_org_reader.rb', line 13

def read_schema_org(string: nil, **options)
  errors = jsonlint(string)
  return { "errors" => errors } if errors.present?

  meta = string.present? ? Maremma.from_json(string) : {}

  id = normalize_id(meta.fetch("@id", nil))
  type = meta.fetch("@type", nil)
  resource_type_general = Bolognese::Utils::SO_TO_DC_TRANSLATIONS[type]
  author = get_authors(from_schema_org(Array.wrap(meta.fetch("author", nil))))
  editor = get_authors(from_schema_org(Array.wrap(meta.fetch("editor", nil))))
  publisher = if meta.dig("publisher").is_a?(Hash)
                meta.dig("publisher", "name")
              elsif publisher.is_a?(String)
                meta.dig("publisher")
              end
  date_published = meta.fetch("datePublished", nil)

  { "id" => id,
    "type" => type,
    "additional_type" => meta.fetch("additionalType", nil),
    "citeproc_type" => Bolognese::Utils::SO_TO_CP_TRANSLATIONS[type] || "article-journal",
    "bibtex_type" => Bolognese::Utils::SO_TO_BIB_TRANSLATIONS[type] || "misc",
    "ris_type" => Bolognese::Utils::SO_TO_RIS_TRANSLATIONS[resource_type_general.to_s.dasherize] || "GEN",
    "resource_type_general" => resource_type_general,
    "doi" => validate_doi(id),
    "url" => normalize_id(meta.fetch("url", nil)),
    "title" => meta.fetch("name", nil),
    "alternate_name" => meta.fetch("alternateName", nil),
    "author" => author,
    "publisher" => meta.dig("publisher", "name"),
    "provider" => meta.fetch("provider", nil),
    "is_identical_to" => schema_org_is_identical_to(meta),
    "is_part_of" => schema_org_is_part_of(meta),
    "has_part" => schema_org_has_part(meta),
    "references" => schema_org_references(meta),
    "is_referenced_by" => schema_org_is_referenced_by(meta),
    "is_supplement_to" => schema_org_is_supplement_to(meta),
    "is_supplemented_by" => schema_org_is_supplemented_by(meta),
    "date_created" => meta.fetch("dateCreated", nil),
    "date_published" => date_published,
    "date_modified" => meta.fetch("dateModified", nil),
    "description" => meta.fetch("description", nil).present? ? { "text" => sanitize(meta.fetch("description")) } : nil,
    "license" => { "id" => meta.fetch("license", nil) },
    "version" => meta.fetch("version", nil),
    "keywords" => meta.fetch("keywords", nil).to_s.split(", ")
  }
end

#schema_org_has_part(meta) ⇒ Object



90
91
92
# File 'lib/bolognese/readers/schema_org_reader.rb', line 90

def schema_org_has_part(meta)
  schema_org_related_identifier(meta, relation_type: "hasPart")
end

#schema_org_is_identical_to(meta) ⇒ Object



82
83
84
# File 'lib/bolognese/readers/schema_org_reader.rb', line 82

def schema_org_is_identical_to(meta)
  schema_org_related_identifier(meta, relation_type: "sameAs")
end

#schema_org_is_new_version_of(meta) ⇒ Object



98
99
100
# File 'lib/bolognese/readers/schema_org_reader.rb', line 98

def schema_org_is_new_version_of(meta)
  schema_org_related_identifier(meta, relation_type: "isSuccessor")
end

#schema_org_is_part_of(meta) ⇒ Object



86
87
88
# File 'lib/bolognese/readers/schema_org_reader.rb', line 86

def schema_org_is_part_of(meta)
  schema_org_related_identifier(meta, relation_type: "isPartOf")
end

#schema_org_is_previous_version_of(meta) ⇒ Object



94
95
96
# File 'lib/bolognese/readers/schema_org_reader.rb', line 94

def schema_org_is_previous_version_of(meta)
  schema_org_related_identifier(meta, relation_type: "isPredecessor")
end

#schema_org_is_referenced_by(meta) ⇒ Object



106
107
108
# File 'lib/bolognese/readers/schema_org_reader.rb', line 106

def schema_org_is_referenced_by(meta)
  schema_org_reverse_related_identifier(meta, relation_type: "citation")
end

#schema_org_is_supplement_to(meta) ⇒ Object



110
111
112
# File 'lib/bolognese/readers/schema_org_reader.rb', line 110

def schema_org_is_supplement_to(meta)
  schema_org_reverse_related_identifier(meta, relation_type: "isBasedOn")
end

#schema_org_is_supplemented_by(meta) ⇒ Object



114
115
116
# File 'lib/bolognese/readers/schema_org_reader.rb', line 114

def schema_org_is_supplemented_by(meta)
  schema_org_related_identifier(meta, relation_type: "isBasedOn")
end

#schema_org_references(meta) ⇒ Object



102
103
104
# File 'lib/bolognese/readers/schema_org_reader.rb', line 102

def schema_org_references(meta)
  schema_org_related_identifier(meta, relation_type: "citation")
end


74
75
76
# File 'lib/bolognese/readers/schema_org_reader.rb', line 74

def schema_org_related_identifier(meta, relation_type: nil)
  normalize_ids(ids: meta.fetch(relation_type, nil))
end


78
79
80
# File 'lib/bolognese/readers/schema_org_reader.rb', line 78

def schema_org_reverse_related_identifier(meta, relation_type: nil)
  normalize_ids(ids: meta.dig("@reverse", relation_type))
end