Class: Cirneco::Doi

Inherits:
Thor
  • Object
show all
Includes:
Bolognese::DoiUtils, Bolognese::Utils, Api, Base, Utils
Defined in:
lib/cirneco/doi.rb

Constant Summary

Constants included from Utils

Utils::UPPER_LIMIT

Instance Method Summary collapse

Methods included from Utils

#decode_doi, #encode_doi, #generate_accession_number, #generate_state_change_template, #generate_transfer_template, #get_dois_by_prefix

Methods included from Api

#delete_metadata, #get_doi, #get_dois, #get_media, #get_metadata, #get_rest_doi, #post_media, #post_metadata, #put_doi, #put_metadata, #transfer_doi, #update_rest_doi

Instance Method Details

#accession_numberObject



92
93
94
# File 'lib/cirneco/doi.rb', line 92

def accession_number
  puts generate_accession_number(options)
end

#bulk_state_change(file) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cirneco/doi.rb', line 148

def bulk_state_change(file)
  count = 0
  File.foreach(file) do |line|
    doi = line.rstrip
    next unless doi.present?

    previous_state, operation = case options[:target_state]
    when "findable"
      ["registered", "publish"]
    when "registered"
      ["draft", "register"]
    end

    meta = generate_state_change_template({operation: operation})

     = get_rest_doi(doi, options)

    unless [200].include?(.status)
      puts "#{doi} was not found #{.status}"
      next
    end
    
     = JSON.parse(.body.fetch("data", []))
    if .dig("data","attributes","state") == previous_state
      response = update_rest_doi(doi, options.merge(data: meta.to_json))
    else
      puts "#{doi} is in the wrong state for change. #{doi} was not changed"
      next
    end

    if [200, 201].include?(response.status)
      puts "#{doi} Updated to #{options[:target_state]}."
      count += 1
    else
      puts "Error: #{doi} " + response.body["errors"].first.fetch("title", "An error occured")
    end
  end

  puts "#{count} DOIs updated."
end

#check(doi) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/cirneco/doi.rb', line 108

def check(doi)
  unless decode_doi(doi).nil?
    puts "Checksum for #{doi} is valid"
  else
    puts "Checksum for #{doi} is not valid"
  end
end

#decode(doi) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/cirneco/doi.rb', line 97

def decode(doi)
  number = decode_doi(doi)

  unless number.nil?
    puts "DOI #{doi} was encoded with #{number}"
  else
    puts "DOI #{doi} could not be decoded"
  end
end

#generateObject



80
81
82
83
84
85
86
# File 'lib/cirneco/doi.rb', line 80

def generate
  if options[:prefix]
    puts encode_doi(options[:prefix], number: options[:number])
  else
    puts "No PREFIX provided. Use --prefix option or PREFIX ENV variable"
  end
end

#get(doi) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cirneco/doi.rb', line 21

def get(doi)
  if doi == "all"
    response = get_dois(options)
  else
    response = get_doi(doi, options)
  end

  if response.body["errors"]
    puts "Error: " + response.body["errors"].first.fetch("title", "An error occured")
  elsif doi == "all"
    puts response.body["data"][0...options[:limit]]
  else
    puts response.body["data"]
  end
end

#post(file) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cirneco/doi.rb', line 56

def post(file)
  data = JSON.parse(IO.read(file))
  count = 0
  data.each do |json|
    doi = doi_from_url(json["@id"])
    url = json["url"]
    next unless doi.present? && url.present?

    response = put_doi(doi, options.merge(url: url))

    if [200, 201].include?(response.status)
      puts "#{doi} registered/updated."
      count += 1
    else
      puts "Error: " + response.body["errors"].first.fetch("title", "An error occured")
    end
  end

  puts "#{count} DOIs registered/updated."
end

#put(doi) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/cirneco/doi.rb', line 42

def put(doi)
  response = put_doi(doi, options)

  if response.body["errors"]
    puts "Error: " + response.body["errors"].first.fetch("title", "An error occured")
  else
    puts response.body["data"]
  end
end

#transfer(file) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cirneco/doi.rb', line 122

def transfer(file)
  count = 0
  File.foreach(file) do |line|
    doi = line.rstrip
    next unless doi.present?
    meta = generate_transfer_template(options)

    response = transfer_doi(doi, options.merge(data: meta.to_json))

    if [200, 201].include?(response.status)
      puts "#{doi} Transfered to #{options[:target]}."
      count += 1
    else
      puts "Error: " + response.body["errors"].first.fetch("title", "An error occured")
    end
  end

  puts "#{count} DOIs transfered."
end