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, #get_dois_by_prefix

Methods included from Api

#delete_metadata, #get_doi, #get_dois, #get_media, #get_metadata, #post_media, #post_metadata, #put_doi, #put_metadata

Instance Method Details

#accession_numberObject



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

def accession_number
  puts generate_accession_number(options)
end

#check(doi) ⇒ Object



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

def check(doi)
  if decode_doi(doi) > 0
    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)

  if number > 0
    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