Class: Xanthus::Dataverse

Inherits:
Repository show all
Defined in:
lib/xanthus/dataverse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Repository

#prepare_readme_file, #prepare_xanthus_file

Constructor Details

#initializeDataverse

Returns a new instance of Dataverse.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xanthus/dataverse.rb', line 17

def initialize
  super
  @server = 'default_server'
  @repo = 'default_repo'
  @token = 'default_token'
  @dataset_name = 'default_name'
  @author = 'default_author'
  @affiliation = 'default_affiliation'
  @email = 'default_email'
  @description = 'default_description'
  @subject = 'default_subject'
  @doi = 'not_set'
end

Instance Attribute Details

#affiliationObject

Returns the value of attribute affiliation.



11
12
13
# File 'lib/xanthus/dataverse.rb', line 11

def affiliation
  @affiliation
end

#authorObject

Returns the value of attribute author.



10
11
12
# File 'lib/xanthus/dataverse.rb', line 10

def author
  @author
end

#dataset_nameObject

Returns the value of attribute dataset_name.



9
10
11
# File 'lib/xanthus/dataverse.rb', line 9

def dataset_name
  @dataset_name
end

#descriptionObject

Returns the value of attribute description.



13
14
15
# File 'lib/xanthus/dataverse.rb', line 13

def description
  @description
end

#doiObject

Returns the value of attribute doi.



15
16
17
# File 'lib/xanthus/dataverse.rb', line 15

def doi
  @doi
end

#emailObject

Returns the value of attribute email.



12
13
14
# File 'lib/xanthus/dataverse.rb', line 12

def email
  @email
end

#repoObject

Returns the value of attribute repo.



7
8
9
# File 'lib/xanthus/dataverse.rb', line 7

def repo
  @repo
end

#serverObject

Returns the value of attribute server.



6
7
8
# File 'lib/xanthus/dataverse.rb', line 6

def server
  @server
end

#subjectObject

Returns the value of attribute subject.



14
15
16
# File 'lib/xanthus/dataverse.rb', line 14

def subject
  @subject
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/xanthus/dataverse.rb', line 8

def token
  @token
end

Instance Method Details

#add(content) ⇒ Object



174
175
176
# File 'lib/xanthus/dataverse.rb', line 174

def add content
  self.add_file_to_dataverse content, "#{content} is a Xanthus generated file (check metadata for description)", 'data'
end

#add_file_to_dataverse(name, description, folder) ⇒ Object



148
149
150
151
# File 'lib/xanthus/dataverse.rb', line 148

def add_file_to_dataverse name, description, folder
  output = `curl --speed-time 15 --speed-limit 1000 --retry 50 --retry-max-time 0 -H X-Dataverse-key:#{@token} -X POST -F "file=@#{name}" -F 'jsonData={"description":"#{description}","directoryLabel":"#{folder}","categories":["Data"], "restrict":"false"}' "#{@server}/api/datasets/:persistentId/add?persistentId=#{@doi}"`
  puts output
end

#create_datasetObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/xanthus/dataverse.rb', line 114

def create_dataset
  Dir.chdir 'dataverse_dataset' do
    File.open('dataset.json', 'w+') do |f|
      f.write(self.dataset_json)
    end
    puts "Creating dataverse #{@dataset_name} in #{@repo} at #{@server}..."
    output = `curl --speed-time 15 --speed-limit 1000 --retry 50 --retry-max-time 0 -H X-Dataverse-key:#{@token} -X POST #{@server}/api/dataverses/#{@repo}/datasets --upload-file dataset.json`
    puts output # needed to escape curl output
    parsed = JSON.parse(output)
    @doi = parsed['data']['persistentId']
    puts "Dataverse #{@doi} created."
  end
end

#dataset_jsonObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/xanthus/dataverse.rb', line 31

def dataset_json
json = %Q{
  {
"datasetVersion": {
  "metadataBlocks": {
    "citation": {
      "fields": [
        {
          "value": "#{@dataset_name}",
          "typeClass": "primitive",
          "multiple": false,
          "typeName": "title"
        },
        {
          "value": [
            {
              "authorName": {
                "value": "#{@author}",
                "typeClass": "primitive",
                "multiple": false,
                "typeName": "authorName"
              },
              "authorAffiliation": {
                "value": "#{@affiliation}",
                "typeClass": "primitive",
                "multiple": false,
                "typeName": "authorAffiliation"
              }
            }
          ],
          "typeClass": "compound",
          "multiple": true,
          "typeName": "author"
        },
        {
          "value": [
              { "datasetContactEmail" : {
                  "typeClass": "primitive",
                  "multiple": false,
                  "typeName": "datasetContactEmail",
                  "value" : "#{@email}"
              },
              "datasetContactName" : {
                  "typeClass": "primitive",
                  "multiple": false,
                  "typeName": "datasetContactName",
                  "value": "#{@author}"
              }
          }],
          "typeClass": "compound",
          "multiple": true,
          "typeName": "datasetContact"
        },
        {
          "value": [ {
             "dsDescriptionValue":{
              "value": "#{@description.gsub(/\r/," ").gsub(/\n/," ")}",
              "multiple":false,
             "typeClass": "primitive",
             "typeName": "dsDescriptionValue"
          }}],
          "typeClass": "compound",
          "multiple": true,
          "typeName": "dsDescription"
        },
        {
          "value": [
            "#{@subject}"
          ],
          "typeClass": "controlledVocabulary",
          "multiple": true,
          "typeName": "subject"
        }
      ],
      "displayName": "Citation Metadata"
    }
  }
}
  }
}
  return json
end

#init(config) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/xanthus/dataverse.rb', line 128

def init config
  # initialize with config information
  @author = config.authors
  @affiliation = config.affiliation
  @email = config.email
  @description = config.description
  @dataset_name = config.name+'-'+Time.now.strftime("%Y-%m-%d_%H-%M")

  FileUtils.mkdir_p 'dataverse_dataset'
  self.create_dataset
  Dir.chdir 'dataverse_dataset' do
    FileUtils.mkdir_p 'repo'
    Dir.chdir 'repo' do
      self.xanthus_file
      self.readme_file config
      self.inputs_file config
    end
  end
end

#inputs_file(config) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/xanthus/dataverse.rb', line 163

def inputs_file config
  config.jobs.each do |name,job|
    job.inputs.each do |k, files|
      files.each do |file|
        system('cp', '-f', "../../#{file}", "#{file}")
        self.add_file_to_dataverse file, 'Job input file.', 'metadata'
      end
    end
  end
end

#readme_file(config) ⇒ Object



158
159
160
161
# File 'lib/xanthus/dataverse.rb', line 158

def readme_file config
  self.prepare_readme_file config
  self.add_file_to_dataverse 'README.md', 'readme describing the dataset.', 'metadata'
end

#xanthus_fileObject



153
154
155
156
# File 'lib/xanthus/dataverse.rb', line 153

def xanthus_file
  self.prepare_xanthus_file
  self.add_file_to_dataverse '.xanthus', 'xanthus file used to generate the data.', 'metadata'
end