Module: Bolognese::Readers::SchemaOrgReader
- Included in:
- MetadataUtils
- 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
- #get_schema_org(id: nil, **options) ⇒ Object
- #read_schema_org(string: nil, **options) ⇒ Object
- #schema_org_has_part(meta) ⇒ Object
- #schema_org_is_identical_to(meta) ⇒ Object
- #schema_org_is_new_version_of(meta) ⇒ Object
- #schema_org_is_part_of(meta) ⇒ Object
- #schema_org_is_previous_version_of(meta) ⇒ Object
- #schema_org_is_referenced_by(meta) ⇒ Object
- #schema_org_is_supplement_to(meta) ⇒ Object
- #schema_org_is_supplemented_by(meta) ⇒ Object
- #schema_org_references(meta) ⇒ Object
- #schema_org_related_identifier(meta, relation_type: nil) ⇒ Object
- #schema_org_reverse_related_identifier(meta, relation_type: nil) ⇒ Object
Instance Method Details
#get_schema_org(id: nil, **options) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 15 def get_schema_org(id: nil, **) return { "string" => nil, "state" => "not_found" } unless id.present? id = normalize_id(id) response = Maremma.get(id) doc = Nokogiri::XML(response.body.fetch("data", nil), nil, 'UTF-8') # workaround for xhtml documents nodeset = doc.css("script") string = nodeset.find { |element| element["type"] == "application/ld+json" } string = string.text if string.present? { "string" => string } end |
#read_schema_org(string: nil, **options) ⇒ Object
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 30 def read_schema_org(string: nil, **) if string.present? errors = jsonlint(string) return { "errors" => errors } if errors.present? end = string.present? ? Maremma.from_json(string) : {} id = normalize_id(.fetch("@id", nil) || .fetch("identifier", nil)) type = .fetch("@type", nil) && .fetch("@type").camelcase resource_type_general = Bolognese::Utils::SO_TO_DC_TRANSLATIONS[type] = .fetch("author", nil) || .fetch("creator", nil) = (from_schema_org(Array.wrap())) editor = (from_schema_org(Array.wrap(.fetch("editor", nil)))) publisher = if .dig("publisher").is_a?(Hash) .dig("publisher", "name") elsif publisher.is_a?(String) .dig("publisher") end included_in_data_catalog = from_schema_org(Array.wrap(.fetch("includedInDataCatalog", nil))) included_in_data_catalog = Array.wrap(included_in_data_catalog).map { |dc| { "title" => dc["name"], "url" => dc["url"] } } is_part_of = schema_org_is_part_of() || included_in_data_catalog license = { "id" => parse_attributes(.fetch("license", nil), content: "id", first: true), "name" => parse_attributes(.fetch("license", nil), content: "name", first: true) } date_published = .fetch("datePublished", nil) state = .present? ? "findable" : "not_found" { "id" => id, "type" => type, "additional_type" => .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), "identifier" => id, "b_url" => normalize_id(.fetch("url", nil)), "title" => .fetch("name", nil), "alternate_name" => .fetch("alternateName", nil), "author" => , "editor" => editor, "publisher" => publisher, "service_provider" => .fetch("provider", nil), "is_identical_to" => schema_org_is_identical_to(), "is_part_of" => is_part_of, "has_part" => schema_org_has_part(), "references" => schema_org_references(), "is_referenced_by" => schema_org_is_referenced_by(), "is_supplement_to" => schema_org_is_supplement_to(), "is_supplemented_by" => schema_org_is_supplemented_by(), "date_created" => .fetch("dateCreated", nil), "date_published" => date_published, "date_modified" => .fetch("dateModified", nil), "description" => .fetch("description", nil).present? ? { "text" => sanitize(.fetch("description")) } : nil, "license" => license, "b_version" => .fetch("version", nil), "keywords" => .fetch("keywords", nil).to_s.split(", "), "state" => state } end |
#schema_org_has_part(meta) ⇒ Object
112 113 114 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 112 def schema_org_has_part() (, relation_type: "hasPart") end |
#schema_org_is_identical_to(meta) ⇒ Object
104 105 106 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 104 def schema_org_is_identical_to() (, relation_type: "sameAs") end |
#schema_org_is_new_version_of(meta) ⇒ Object
120 121 122 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 120 def schema_org_is_new_version_of() (, relation_type: "SuccessorOf") end |
#schema_org_is_part_of(meta) ⇒ Object
108 109 110 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 108 def schema_org_is_part_of() (, relation_type: "isPartOf") end |
#schema_org_is_previous_version_of(meta) ⇒ Object
116 117 118 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 116 def schema_org_is_previous_version_of() (, relation_type: "PredecessorOf") end |
#schema_org_is_referenced_by(meta) ⇒ Object
128 129 130 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 128 def schema_org_is_referenced_by() (, relation_type: "citation") end |
#schema_org_is_supplement_to(meta) ⇒ Object
132 133 134 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 132 def schema_org_is_supplement_to() (, relation_type: "isBasedOn") end |
#schema_org_is_supplemented_by(meta) ⇒ Object
136 137 138 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 136 def schema_org_is_supplemented_by() (, relation_type: "isBasedOn") end |
#schema_org_references(meta) ⇒ Object
124 125 126 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 124 def schema_org_references() (, relation_type: "citation") end |
#schema_org_related_identifier(meta, relation_type: nil) ⇒ Object
96 97 98 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 96 def (, relation_type: nil) normalize_ids(ids: .fetch(relation_type, nil)) end |
#schema_org_reverse_related_identifier(meta, relation_type: nil) ⇒ Object
100 101 102 |
# File 'lib/bolognese/readers/schema_org_reader.rb', line 100 def (, relation_type: nil) normalize_ids(ids: .dig("@reverse", relation_type)) end |