Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 41 additions & 5 deletions lib/ro_crate/model/data_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,25 @@ def self.specialize(props)
# PLEASE NOTE, the new data entity will not be added to the crate. To do this, call Crate#add_data_entity.
#
# @param crate [Crate] The RO-Crate that owns this directory.
# @param source [nil] Ignored. For compatibility with the File and Directory constructor signatures.
# @param id [String, nil] An ID to identify this DataEntity, or nil to auto-generate an appropriate one,
# (or determine via the properties param)
# @param source [String, Pathname, ::File, URI, nil, #read] The source on the disk (or on the internet if a URI)
# where the content of this DataEntity can be found.
# @param crate_path [String, nil] The relative path within the RO-Crate where this data entity should be written.
# Also used as the ID of the DataEntity. Will be taken from `properties` or generated if `crate_path` is nil.
# @param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this DataEntity.
def initialize(crate, source = nil, id = nil, properties = {})
super(crate, id, properties)
def initialize(crate, source = nil, crate_path = nil, properties = {})
if crate_path.is_a?(Hash) && properties.empty?
properties = crate_path
crate_path = nil
end

@source = normalize_source(source)

if crate_path.nil?
crate_path = @source.basename.to_s if @source.respond_to?(:basename)
crate_path = @source.to_s if @source.is_a?(URI) && @source.absolute?
end

super(crate, crate_path, properties)
end

##
Expand All @@ -51,5 +64,28 @@ def payload
def filepath
Addressable::URI.unescape(id.sub(/\A\//, '')).to_s # Remove initial / and decode %20 etc.
end

private

##
# Do some normalization of the given source. Coverts `::File` and `String` (if relative path) to `Pathname`
# and ensures they are expanded.
#
# @param source [String, Pathname, ::File, URI, nil, #read] The source on the disk (or on the internet if a URI).
# @return [Pathname, URI, nil, #read] An absolute Pathname or URI for the source.
def normalize_source(source)
if source.is_a?(String)
uri = URI(source) rescue nil
if uri&.absolute?
source = uri
else
source = Pathname.new(source)
end
elsif source.is_a?(::File)
source = Pathname.new(source)
end

source.is_a?(Pathname) ? source.expand_path : source
end
end
end
29 changes: 17 additions & 12 deletions lib/ro_crate/model/directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ def self.format_local_id(id)
# Crate#add_data_entity, or just use Crate#add_directory.
#
# @param crate [Crate] The RO-Crate that owns this directory.
# @param source_directory [String, Pathname, ::File, nil] The source directory that will be included in the crate.
# @param crate_path [String] The relative path within the RO-Crate where this directory will be written.
# @param source [String, Pathname, ::File, nil] The source directory that will be included in the crate.
# @param crate_path [String, nil] The relative path within the RO-Crate where this directory will be written.
# @param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this directory.
def initialize(crate, source_directory = nil, crate_path = nil, properties = {})
@directory_entries = {}
def initialize(crate, source = nil, crate_path = nil, properties = {})
super(crate, source, crate_path, properties)

if source_directory
source_directory = Pathname.new(::File.expand_path(source_directory))
@entry = Entry.new(source_directory)
populate_entries(source_directory)
crate_path = source_directory.basename.to_s if crate_path.nil?
@directory_entries = {}
if @source
if @source.is_a?(URI) && @source.absolute?
@entry = RemoteEntry.new(@source, directory: true)
else
@entry = Entry.new(@source)
populate_entries(@source)
end
end

super(crate, nil, crate_path, properties)
end

##
Expand All @@ -34,7 +35,7 @@ def initialize(crate, source_directory = nil, crate_path = nil, properties = {})
# @return [Hash{String => Entry}>]
def payload
entries = {}
entries[filepath.chomp('/')] = @entry if @entry
entries[filepath.chomp('/')] = @entry if @entry && !remote?

@directory_entries.each do |rel_path, entry|
entries[full_entry_path(rel_path)] = entry
Expand All @@ -43,6 +44,10 @@ def payload
entries
end

def remote?
@entry.remote?
end

private

##
Expand Down
29 changes: 4 additions & 25 deletions lib/ro_crate/model/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,12 @@ def self.format_local_id(id)
# @param crate_path [String] The relative path within the RO-Crate where this file will be written.
# @param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this file.
def initialize(crate, source, crate_path = nil, properties = {})
if crate_path.is_a?(Hash) && properties.empty?
properties = crate_path
crate_path = nil
end

if source.is_a?(String)
uri = URI(source) rescue nil
if uri.absolute?
source = uri
else
source = Pathname.new(source).expand_path
end
elsif source.is_a?(::File)
source = Pathname.new(source).expand_path
end

if crate_path.nil?
crate_path = source.basename.to_s if source.respond_to?(:basename)
crate_path = source.to_s if source.is_a?(URI) && source.absolute?
end

super(crate, nil, crate_path, properties)
super(crate, source, crate_path, properties)

if source.is_a?(URI) && source.absolute?
@entry = RemoteEntry.new(source)
if @source.is_a?(URI) && @source.absolute?
@entry = RemoteEntry.new(@source)
else
@entry = Entry.new(source)
@entry = Entry.new(@source)
end
end

Expand Down
5 changes: 3 additions & 2 deletions lib/ro_crate/model/remote_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class RemoteEntry < Entry
# Create a new RemoteEntry.
#
# @param uri [URI] An absolute URI.
def initialize(uri)
def initialize(uri, directory: false)
@uri = uri
@directory = directory
end

##
Expand All @@ -23,7 +24,7 @@ def source
##
# Does this RemoteEntry point to a directory
def directory?
false
@directory
end

##
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/uri_heavy_crate/main.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Redacted!
# Thanks to Veit Schwämmle and Laura Rodriguez Navas for this example
Loading