Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

![Tests](https://github.com/ResearchObject/ro-crate-ruby/actions/workflows/tests.yml/badge.svg)

This is a WIP gem for creating, manipulating and reading RO-Crates (conforming to version 1.1 of the specification).
This is a WIP gem for creating, manipulating and reading RO-Crates (conforming to version 1.2 of the specification). RO-Crates produced by older versions (1.0, 1.1) of the spec can still be read.

* RO-Crate - https://researchobject.github.io/ro-crate/
* RO-Crate spec (1.1) - https://researchobject.github.io/ro-crate/1.1/
* RO-Crate - https://www.researchobject.org/ro-crate/
* RO-Crate spec (1.2) - https://www.researchobject.org/ro-crate/specification/1.2/

## Installation

Expand Down
13 changes: 11 additions & 2 deletions lib/ro_crate/model/crate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ def self.format_local_id(id)

##
# Initialize an empty RO-Crate.
def initialize(id = IDENTIFIER, properties = {})
#
# @param id [String] The crate's identifier.
# @param properties [Hash] Initial properties for the root data entity.
# @param version [String] RO-Crate spec version to declare (default: ROCrate::Metadata::DEFAULT_VERSION).
# Must be one of ROCrate::Metadata::SUPPORTED_VERSIONS.
def initialize(id = IDENTIFIER, properties = {}, version: ROCrate::Metadata::DEFAULT_VERSION)
unless ROCrate::Metadata::SUPPORTED_VERSIONS.include?(version)
raise ArgumentError, "Unsupported RO-Crate version: #{version.inspect}. Supported: #{ROCrate::Metadata::SUPPORTED_VERSIONS.join(', ')}"
Comment thread
cllde8 marked this conversation as resolved.
Outdated
end
@data_entities = Set.new
@contextual_entities = Set.new
@metadata_version = version
super(self, nil, id, properties)
end

Expand Down Expand Up @@ -168,7 +177,7 @@ def add_data_entity(entity)
#
# @return [Metadata]
def metadata
@metadata ||= ROCrate::Metadata.new(self)
@metadata ||= ROCrate::Metadata.new(self, {}, version: @metadata_version || ROCrate::Metadata::DEFAULT_VERSION)
end

##
Expand Down
28 changes: 23 additions & 5 deletions lib/ro_crate/model/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,31 @@ class Metadata < File
IDENTIFIER = 'ro-crate-metadata.json'.freeze
IDENTIFIER_1_0 = 'ro-crate-metadata.jsonld'.freeze # 1.0 spec identifier
RO_CRATE_BASE = 'https://w3id.org/ro/crate/'
CONTEXT = "#{RO_CRATE_BASE}1.1/context".freeze
SPEC = "#{RO_CRATE_BASE}1.1".freeze

def initialize(crate, properties = {})
SUPPORTED_VERSIONS = %w[1.0 1.0-DRAFT 1.1 1.1-DRAFT 1.2 1.2-DRAFT].freeze
DEFAULT_VERSION = '1.2'.freeze

CONTEXT = "#{RO_CRATE_BASE}#{DEFAULT_VERSION}/context".freeze
SPEC = "#{RO_CRATE_BASE}#{DEFAULT_VERSION}".freeze

attr_reader :version

def initialize(crate, properties = {}, version: DEFAULT_VERSION)
unless SUPPORTED_VERSIONS.include?(version)
raise ArgumentError, "Unsupported RO-Crate version: #{version.inspect}. Supported: #{SUPPORTED_VERSIONS.join(', ')}"
Comment thread
cllde8 marked this conversation as resolved.
Outdated
end
@version = version
super(crate, nil, IDENTIFIER, properties)
end

def context_url
"#{RO_CRATE_BASE}#{@version}/context"
end

def spec_url
"#{RO_CRATE_BASE}#{@version}"
end

##
# Generate the crate's `ro-crate-metadata.jsonld`.
# @return [String] The rendered JSON-LD as a "prettified" string.
Expand All @@ -21,7 +39,7 @@ def generate
end

def context
@context || CONTEXT
@context || context_url
end

def context= c
Expand All @@ -39,7 +57,7 @@ def default_properties
'@id' => IDENTIFIER,
'@type' => 'CreativeWork',
'about' => { '@id' => crate.id },
'conformsTo' => { '@id' => SPEC }
'conformsTo' => { '@id' => spec_url }
}
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ro_crate/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def self.create_data_entity(crate, entity_class, source, entity_props)

##
# Extract the metadata entity from the entity hash, according to the rules defined here:
# https://www.researchobject.org/ro-crate/1.1/root-data-entity.html#finding-the-root-data-entity
# https://www.researchobject.org/ro-crate/specification/1.2/root-data-entity.html#finding-the-root-data-entity
# @return [nil, Hash{String => Hash}] A Hash containing (hopefully) one value, the metadata entity's properties
# mapped by its @id, or nil if nothing is found.
def self.extract_metadata_entity(entities)
Expand Down Expand Up @@ -294,7 +294,7 @@ def self.extract_preview_entity(entities)

##
# Extract the root entity from the entity hash, according to the rules defined here:
# https://www.researchobject.org/ro-crate/1.1/root-data-entity.html#finding-the-root-data-entity
# https://www.researchobject.org/ro-crate/specification/1.2/root-data-entity.html#finding-the-root-data-entity
# @return [Hash{String => Hash}] A Hash containing (hopefully) one value, the root entity's properties,
# mapped by its @id.
def self.extract_root_entity(entities)
Expand Down
20 changes: 20 additions & 0 deletions test/crate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,24 @@ class CrateTest < Test::Unit::TestCase
assert_nil crate.get('#joe')
assert crate.get('#joehouse')
end

test 'defaults to RO-Crate spec 1.2' do
crate = ROCrate::Crate.new
assert_equal '1.2', crate.metadata.version
assert_equal 'https://w3id.org/ro/crate/1.2/context', crate.metadata.context
assert_equal 'https://w3id.org/ro/crate/1.2', crate.metadata.spec_url
assert_equal({ '@id' => 'https://w3id.org/ro/crate/1.2' }, crate.metadata.properties['conformsTo'])
end
Comment thread
cllde8 marked this conversation as resolved.

test 'can write older spec version' do
crate = ROCrate::Crate.new(ROCrate::Crate::IDENTIFIER, {}, version: '1.1')
assert_equal '1.1', crate.metadata.version
assert_equal 'https://w3id.org/ro/crate/1.1/context', crate.metadata.context
assert_equal({ '@id' => 'https://w3id.org/ro/crate/1.1' }, crate.metadata.properties['conformsTo'])
end

test 'rejects unsupported spec version' do
assert_raise(ArgumentError) { ROCrate::Crate.new(ROCrate::Crate::IDENTIFIER, {}, version: '1.5') }
assert_raise(ArgumentError) { ROCrate::Crate.new(ROCrate::Crate::IDENTIFIER, {}, version: 'v1.2') }
end
end