lib/bolognese/schema_org.rb in bolognese-0.4.3 vs lib/bolognese/schema_org.rb in bolognese-0.5
- old
+ new
@@ -1,48 +1,24 @@
module Bolognese
class SchemaOrg < Metadata
- DC_TO_SO_TRANSLATIONS = {
- "Audiovisual" => "VideoObject",
- "Collection" => "Collection",
- "Dataset" => "Dataset",
- "Event" => "Event",
- "Image" => "ImageObject",
- "InteractiveResource" => nil,
- "Model" => nil,
- "PhysicalObject" => nil,
- "Service" => "Service",
- "Software" => "SoftwareSourceCode",
- "Sound" => "AudioObject",
- "Text" => "ScholarlyArticle",
- "Workflow" => nil,
- "Other" => "CreativeWork"
- }
-
- attr_reader = :id, :raw, :metadata, :schema_org
-
def initialize(id: nil, string: nil)
- id = normalize_url(id) if id.present?
+ id = normalize_id(id) if id.present?
if string.present?
@raw = string
elsif id.present?
response = Maremma.get(id)
- @raw = response.body.fetch("data", nil)
+ doc = Nokogiri::XML(response.body.fetch("data", nil))
+ @raw = doc.at_xpath('//script[@type="application/ld+json"]')
end
end
+ alias_method :schema_org, :as_schema_org
+
def metadata
- @metadata ||= begin
- if raw.present?
- doc = Nokogiri::XML(raw)
- tag = doc.at_xpath('//script[@type="application/ld+json"]')
- Maremma.from_json(tag)
- else
- {}
- end
- end
+ @metadata ||= raw.present? ? Maremma.from_json(raw) : {}
end
def exists?
metadata.present?
end
@@ -50,15 +26,15 @@
def doi
doi_from_url(id)
end
def id
- normalize_url(metadata.fetch("@id", nil))
+ normalize_id(metadata.fetch("@id", nil))
end
def url
- normalize_url(metadata.fetch("url", nil))
+ normalize_id(metadata.fetch("url", nil))
end
def type
metadata.fetch("@type", nil)
end
@@ -74,15 +50,15 @@
def alternate_name
metadata.fetch("alternateName", nil)
end
def author
- Array(metadata.fetch("author", nil)).map { |a| a.except("name") }
+ Array(metadata.fetch("author", nil)).map { |a| a.except("name") }.presence
end
def editor
- Array(metadata.fetch("editor", nil)).map { |a| a.except("name") }
+ Array(metadata.fetch("editor", nil)).map { |a| a.except("name") }.presence
end
def description
metadata.fetch("description", nil)
end
@@ -118,15 +94,15 @@
def is_part_of
related_identifiers("isPartOf").first
end
def has_part
- related_identifiers("hasPart")
+ related_identifiers("hasPart").presence
end
def citation
- related_identifiers("citation")
+ related_identifiers("citation").presence
end
def publisher
metadata.fetch("publisher", nil)
end
@@ -139,32 +115,8 @@
end
end
def provider
metadata.fetch("provider", nil)
- end
-
- def as_schema_org
- { "@context" => "http://schema.org",
- "@type" => type,
- "@id" => id,
- "url" => url,
- "name" => name,
- "alternateName" => alternate_name,
- "author" => author,
- "editor" => editor,
- "description" => description,
- "license" => license,
- "version" => version,
- "keywords" => keywords,
- "dateCreated" => date_created,
- "datePublished" => date_published,
- "dateModified" => date_modified,
- "isPartOf" => is_part_of,
- "hasPart" => has_part,
- "citation" => citation,
- "publisher" => publisher,
- "provider" => provider
- }.compact
end
end
end