lib/bolognese/utils.rb in bolognese-1.0.2 vs lib/bolognese/utils.rb in bolognese-1.0.3

- old
+ new

@@ -360,32 +360,32 @@ "datacite" elsif options[:ext] == ".json" && Maremma.from_json(string).to_h.dig("@context").to_s.start_with?("http://schema.org", "https://schema.org") "schema_org" elsif options[:ext] == ".json" && Maremma.from_json(string).to_h.dig("@context") == ("https://raw.githubusercontent.com/codemeta/codemeta/master/codemeta.jsonld") "codemeta" - elsif options[:ext] == ".json" && Maremma.from_json(string).to_h.dig("ris_type") - "crosscite" elsif options[:ext] == ".json" && Maremma.from_json(string).to_h.dig("schema-version").to_s.start_with?("http://datacite.org/schema/kernel") "datacite_json" + elsif options[:ext] == ".json" && Maremma.from_json(string).to_h.dig("types") + "crosscite" elsif options[:ext] == ".json" && Maremma.from_json(string).to_h.dig("issued", "date-parts").present? "citeproc" end end def find_from_format_by_string(string) if Maremma.from_xml(string).to_h.dig("doi_records", "doi_record", "crossref").present? "crossref" elsif Nokogiri::XML(string, nil, 'UTF-8', &:noblanks).collect_namespaces.find { |k, v| v.start_with?("http://datacite.org/schema/kernel") } "datacite" - elsif Maremma.from_json(string).to_h.dig("ris_type").present? - "crosscite" elsif Maremma.from_json(string).to_h.dig("@context").to_s.start_with?("http://schema.org", "https://schema.org") "schema_org" elsif Maremma.from_json(string).to_h.dig("@context") == ("https://raw.githubusercontent.com/codemeta/codemeta/master/codemeta.jsonld") "codemeta" - elsif Maremma.from_json(string).to_h.dig("schemaVersion").to_s.start_with?("http://datacite.org/schema/kernel") + elsif Maremma.from_json(string).to_h.dig("schema-version").to_s.start_with?("http://datacite.org/schema/kernel") "datacite_json" + elsif Maremma.from_json(string).to_h.dig("types").present? + "crosscite" elsif Maremma.from_json(string).to_h.dig("issued", "date-parts").present? "citeproc" elsif string.start_with?("TY - ") "ris" elsif BibTeX.parse(string).first @@ -522,10 +522,22 @@ uri.to_s rescue URI::InvalidURIError nil end + def to_datacite_json(element) + Array.wrap(element).each do |e| + e.inject({}) {|h, (k,v)| h[k.dasherize] = v; h } + end.unwrap + end + + def from_datacite_json(element) + Array.wrap(element).each do |e| + e.inject({}) {|h, (k,v)| h[k.underscore] = v; h } + end.unwrap + end + def to_schema_org(element) mapping = { "type" => "@type", "id" => "@id", "title" => "name" } map_hash_keys(element: element, mapping: mapping) end @@ -795,9 +807,14 @@ # return nil if invalid iso8601 timestamp def get_datetime_from_iso8601(iso8601_time) ISO8601::DateTime.new(iso8601_time).to_time.utc rescue nil + end + + def get_date(dates, date_type) + dd = dates.find { |d| d["date_type"] == date_type } || {} + dd.fetch("date", nil) end def jsonlint(json) return ["No JSON provided"] unless json.present?