ro-crate 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa3bf8a8cdaa985143e1e2988d4fc24e14aad40bfd55763b4be17af23c8fa36a
4
- data.tar.gz: 190326475ec90f7328e86252dd4f0c1150750ba8298fef7ba8337291ae988e21
3
+ metadata.gz: 28049e56f761e9b8684970817cc4eace0a60c78aee05b6a16688e589a2088357
4
+ data.tar.gz: 3e731b4c6039aa74ea34ab7d672fa665b99d4d030598a16ac593877cb0bf306f
5
5
  SHA512:
6
- metadata.gz: 7cf568838eba39df6ff3770456a8f2133efe8797d1b916b0acf4523f1830623120e14e3a3e8d3e5bbbf09d7960fe5afc585389010ab674394cc80b3c944363d7
7
- data.tar.gz: 151807e2411c7d7d09ef2930f75b1eca9f1274fce14ca28d084c7b609268defc70803832697d79b68b9efe282ed63b0af1b179fd1cb0de2dba641798b331afe9
6
+ metadata.gz: ac8be2df8a04041f7cafc51e04c2ee67d0e25599b1c7a16716aa0b61a4db132b5fb1e55a1bef48b7c6c83aea6bfaba1826a1ac294f58e3092a35438e1938433c
7
+ data.tar.gz: 13a3780ee9ff9c85ac6829b0f93e393df3917e9286468c54cd127d1fdba704d14ced069fafb7bcb76378c6b120c6598e431e905059983f1dcec704efefc038cf
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ro-crate (0.5.0)
4
+ ro-crate (0.5.1)
5
5
  addressable (>= 2.7, < 2.9)
6
6
  rubyzip (~> 2.0.0)
7
7
 
@@ -27,12 +27,25 @@ module ROCrate
27
27
  # PLEASE NOTE, the new data entity will not be added to the crate. To do this, call Crate#add_data_entity.
28
28
  #
29
29
  # @param crate [Crate] The RO-Crate that owns this directory.
30
- # @param source [nil] Ignored. For compatibility with the File and Directory constructor signatures.
31
- # @param id [String, nil] An ID to identify this DataEntity, or nil to auto-generate an appropriate one,
32
- # (or determine via the properties param)
30
+ # @param source [String, Pathname, ::File, URI, nil, #read] The source on the disk (or on the internet if a URI)
31
+ # where the content of this DataEntity can be found.
32
+ # @param crate_path [String, nil] The relative path within the RO-Crate where this data entity should be written.
33
+ # Also used as the ID of the DataEntity. Will be taken from `properties` or generated if `crate_path` is nil.
33
34
  # @param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this DataEntity.
34
- def initialize(crate, source = nil, id = nil, properties = {})
35
- super(crate, id, properties)
35
+ def initialize(crate, source = nil, crate_path = nil, properties = {})
36
+ if crate_path.is_a?(Hash) && properties.empty?
37
+ properties = crate_path
38
+ crate_path = nil
39
+ end
40
+
41
+ @source = normalize_source(source)
42
+
43
+ if crate_path.nil?
44
+ crate_path = @source.basename.to_s if @source.respond_to?(:basename)
45
+ crate_path = @source.to_s if @source.is_a?(URI) && @source.absolute?
46
+ end
47
+
48
+ super(crate, crate_path, properties)
36
49
  end
37
50
 
38
51
  ##
@@ -51,5 +64,28 @@ module ROCrate
51
64
  def filepath
52
65
  Addressable::URI.unescape(id.sub(/\A\//, '')).to_s # Remove initial / and decode %20 etc.
53
66
  end
67
+
68
+ private
69
+
70
+ ##
71
+ # Do some normalization of the given source. Coverts `::File` and `String` (if relative path) to `Pathname`
72
+ # and ensures they are expanded.
73
+ #
74
+ # @param source [String, Pathname, ::File, URI, nil, #read] The source on the disk (or on the internet if a URI).
75
+ # @return [Pathname, URI, nil, #read] An absolute Pathname or URI for the source.
76
+ def normalize_source(source)
77
+ if source.is_a?(String)
78
+ uri = URI(source) rescue nil
79
+ if uri&.absolute?
80
+ source = uri
81
+ else
82
+ source = Pathname.new(source)
83
+ end
84
+ elsif source.is_a?(::File)
85
+ source = Pathname.new(source)
86
+ end
87
+
88
+ source.is_a?(Pathname) ? source.expand_path : source
89
+ end
54
90
  end
55
91
  end
@@ -11,20 +11,21 @@ module ROCrate
11
11
  # Crate#add_data_entity, or just use Crate#add_directory.
12
12
  #
13
13
  # @param crate [Crate] The RO-Crate that owns this directory.
14
- # @param source_directory [String, Pathname, ::File, nil] The source directory that will be included in the crate.
15
- # @param crate_path [String] The relative path within the RO-Crate where this directory will be written.
14
+ # @param source [String, Pathname, ::File, nil] The source directory that will be included in the crate.
15
+ # @param crate_path [String, nil] The relative path within the RO-Crate where this directory will be written.
16
16
  # @param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this directory.
17
- def initialize(crate, source_directory = nil, crate_path = nil, properties = {})
18
- @directory_entries = {}
17
+ def initialize(crate, source = nil, crate_path = nil, properties = {})
18
+ super(crate, source, crate_path, properties)
19
19
 
20
- if source_directory
21
- source_directory = Pathname.new(::File.expand_path(source_directory))
22
- @entry = Entry.new(source_directory)
23
- populate_entries(source_directory)
24
- crate_path = source_directory.basename.to_s if crate_path.nil?
20
+ @directory_entries = {}
21
+ if @source
22
+ if @source.is_a?(URI) && @source.absolute?
23
+ @entry = RemoteEntry.new(@source, directory: true)
24
+ else
25
+ @entry = Entry.new(@source)
26
+ populate_entries(@source)
27
+ end
25
28
  end
26
-
27
- super(crate, nil, crate_path, properties)
28
29
  end
29
30
 
30
31
  ##
@@ -34,7 +35,7 @@ module ROCrate
34
35
  # @return [Hash{String => Entry}>]
35
36
  def payload
36
37
  entries = {}
37
- entries[filepath.chomp('/')] = @entry if @entry
38
+ entries[filepath.chomp('/')] = @entry if @entry && !remote?
38
39
 
39
40
  @directory_entries.each do |rel_path, entry|
40
41
  entries[full_entry_path(rel_path)] = entry
@@ -43,6 +44,10 @@ module ROCrate
43
44
  entries
44
45
  end
45
46
 
47
+ def remote?
48
+ @entry.remote?
49
+ end
50
+
46
51
  private
47
52
 
48
53
  ##
@@ -15,33 +15,12 @@ module ROCrate
15
15
  # @param crate_path [String] The relative path within the RO-Crate where this file will be written.
16
16
  # @param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this file.
17
17
  def initialize(crate, source, crate_path = nil, properties = {})
18
- if crate_path.is_a?(Hash) && properties.empty?
19
- properties = crate_path
20
- crate_path = nil
21
- end
22
-
23
- if source.is_a?(String)
24
- uri = URI(source) rescue nil
25
- if uri.absolute?
26
- source = uri
27
- else
28
- source = Pathname.new(source).expand_path
29
- end
30
- elsif source.is_a?(::File)
31
- source = Pathname.new(source).expand_path
32
- end
33
-
34
- if crate_path.nil?
35
- crate_path = source.basename.to_s if source.respond_to?(:basename)
36
- crate_path = source.to_s if source.is_a?(URI) && source.absolute?
37
- end
38
-
39
- super(crate, nil, crate_path, properties)
18
+ super(crate, source, crate_path, properties)
40
19
 
41
- if source.is_a?(URI) && source.absolute?
42
- @entry = RemoteEntry.new(source)
20
+ if @source.is_a?(URI) && @source.absolute?
21
+ @entry = RemoteEntry.new(@source)
43
22
  else
44
- @entry = Entry.new(source)
23
+ @entry = Entry.new(@source)
45
24
  end
46
25
  end
47
26
 
@@ -9,8 +9,9 @@ module ROCrate
9
9
  # Create a new RemoteEntry.
10
10
  #
11
11
  # @param uri [URI] An absolute URI.
12
- def initialize(uri)
12
+ def initialize(uri, directory: false)
13
13
  @uri = uri
14
+ @directory = directory
14
15
  end
15
16
 
16
17
  ##
@@ -23,7 +24,7 @@ module ROCrate
23
24
  ##
24
25
  # Does this RemoteEntry point to a directory
25
26
  def directory?
26
- false
27
+ @directory
27
28
  end
28
29
 
29
30
  ##
data/ro_crate.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'ro-crate'
3
- s.version = '0.5.0'
3
+ s.version = '0.5.1'
4
4
  s.summary = 'Create, manipulate, read RO-Crates.'
5
5
  s.authors = ['Finn Bacall']
6
6
  s.email = 'finn.bacall@manchester.ac.uk'
@@ -0,0 +1,2 @@
1
+ # Redacted!
2
+ # Thanks to Veit Schwämmle and Laura Rodriguez Navas for this example