dataverse 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,157 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+ require_relative 'dataset'
5
+
6
+ module Dataverse
7
+ class Dataverse < Base
8
+
9
+ attr_reader :id
10
+
11
+ def self.id(id)
12
+ new(id)
13
+ rescue RestClient::NotFound
14
+ nil
15
+ end
16
+
17
+ def self.root
18
+ id(':root')
19
+ end
20
+
21
+ SAMPLE_DATA= {
22
+ name: 'new dataverse',
23
+ alias: 'new_dv',
24
+ dataverseContacts: [
25
+ { contactEmail: 'abc@def.org'}
26
+ ],
27
+ affiliation: 'My organization',
28
+ description: 'My new dataverse',
29
+ dataverseType: 'ORGANIZATIONS_INSTITUTIONS'
30
+ }
31
+
32
+ TYPES = %w'DEPARTMENT JOURNALS LABORATORY ORGANIZATIONS_INSTITUTIONS RESEARCHERS RESEARCH_GROUP RESEARCH_PROJECTS TEACHING_COURSES UNCATEGORIZED'
33
+
34
+ def create(data)
35
+ data = StringIO.new(data.to_json) if data.is_a?(Hash)
36
+ if data.is_a?(String)
37
+ begin
38
+ if File.exist?(data)
39
+ data = File.open(data, 'r')
40
+ elsif JSON::parse(data)
41
+ data = StringIO.new(data)
42
+ end
43
+ rescue JSON::ParserError, File
44
+ data = nil
45
+ end
46
+ end
47
+
48
+ unless data.is_a?(File) || data.is_a?(StringIO)
49
+ raise Error.new("Data could not be parsed. Should be a Hash, filename or JSON string.")
50
+ end
51
+
52
+ result = call('', method: :post, body: data)
53
+
54
+ dv = Dataverse.new(nil)
55
+ dv.init(result)
56
+ dv.instance_variable_set('@id', result['id'])
57
+ return dv
58
+
59
+ ensure
60
+ data.close if data.is_a?(File)
61
+
62
+ end
63
+
64
+ def publish
65
+ call("actions/:publish", method: :post)
66
+ return "Dataverse #{id} published"
67
+ end
68
+
69
+ def delete
70
+ call('', method: :delete)['message']
71
+ end
72
+
73
+ def call(url, **args)
74
+ api_call("dataverses/#{id}/#{url}", **args)
75
+ end
76
+
77
+ def children
78
+ @children ||= begin
79
+ result = []
80
+ data = call("contents")
81
+ data.each do |x|
82
+ case x['type']
83
+ when 'dataverse'
84
+ result << Dataverse.id(x['id'])
85
+ when 'dataset'
86
+ result << Dataset.id(x['id'])
87
+ else
88
+ raise Error.new("Unsupported type: #{x['type']} (#{x['name']})")
89
+ end
90
+ end
91
+ result
92
+ end
93
+ end
94
+
95
+ def each_dataverse(&block)
96
+ data = []
97
+ children.each do |child|
98
+ if child.is_a?(Dataverse)
99
+ data << (block_given? ? yield(child) : child)
100
+ data += child.each_dataverse(&block)
101
+ end
102
+ end
103
+ data
104
+ end
105
+
106
+ def each_dataset(&block)
107
+ data = []
108
+ children.each do |child|
109
+ if child.is_a?(Dataverse)
110
+ data += child.each_dataset(&block)
111
+ elsif child.is_a?(Dataset)
112
+ data << (block_given? ? yield(child) : child)
113
+ end
114
+ end
115
+ data
116
+ end
117
+
118
+ def size
119
+ @size ||= begin
120
+ data = api_call("dataverses/#{id}/storagesize")
121
+ data['message'][/[,\d]+/].delete(',').to_i
122
+ end
123
+ end
124
+
125
+ def rdm_data
126
+ api_data
127
+ end
128
+
129
+ def export_metadata(md_type)
130
+ format = case md_type.to_s
131
+ when 'rdm'
132
+ return rdm_data
133
+ else
134
+ raise Error.new("Unknown metadata format: '#{md_type}'")
135
+ end
136
+ end
137
+
138
+ protected
139
+
140
+ def initialize(id)
141
+ @id = id
142
+ init(get_data)
143
+ end
144
+
145
+ def init(data)
146
+ @size = nil
147
+ @children = nil
148
+ super(data)
149
+ end
150
+
151
+ def get_data
152
+ return unless id
153
+ api_call("dataverses/#{id}")
154
+ end
155
+
156
+ end
157
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dataverse
4
+
5
+ class Error < StandardError
6
+ def initialize(msg, backtrace: nil)
7
+ @backtrace = backtrace
8
+ super(msg)
9
+ end
10
+
11
+ def backtrace
12
+ return @backtrace if @backtrace
13
+ super
14
+ end
15
+
16
+ def cause
17
+ nil
18
+ end
19
+ end
20
+
21
+ class VersionError < Error
22
+ def initialize(version)
23
+ super "Version #{version} does not exist"
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dataverse
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dataverse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kris Dekeyser
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Dataverse.org API wrapper.
28
+ email:
29
+ - kris.dekeyser@libis.be
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".env.template"
35
+ - ".gitignore"
36
+ - ".rspec"
37
+ - ".travis.yml"
38
+ - CHANGELOG.md
39
+ - CODE_OF_CONDUCT.md
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - LICENSE.txt
43
+ - README.md
44
+ - Rakefile
45
+ - bin/console
46
+ - bin/setup
47
+ - dataverse.gemspec
48
+ - lib/dataverse.rb
49
+ - lib/dataverse/base.rb
50
+ - lib/dataverse/dataset.rb
51
+ - lib/dataverse/dataverse.rb
52
+ - lib/dataverse/errors.rb
53
+ - lib/dataverse/version.rb
54
+ homepage: https://rubygems.org/gems/dataverse
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ homepage_uri: https://rubygems.org/gems/dataverse
59
+ source_code_uri: https://github.com/libis/dataverse_api
60
+ changelog_uri: https://github.com/libis/dataverse_api/CHANGELOG.md
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 2.4.0
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.2.15
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Dataverse API.
80
+ test_files: []