diff --git a/Gemfile.lock b/Gemfile.lock index f16f58c..67e0d15 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - cirneco (0.9.25) + cirneco (0.9.26) activesupport (>= 4.2.5, < 6) base32-url (~> 0.5) bergamasco (~> 0.3) @@ -36,7 +36,7 @@ GEM safe_yaml (~> 1.0, >= 1.0.4) bibtex-ruby (4.4.7) latex-decode (~> 0.0) - bolognese (0.13.4) + bolognese (0.14.1) activesupport (>= 4.2.5, < 6) benchmark_methods (~> 0.7) bibtex-ruby (~> 4.1) @@ -66,13 +66,13 @@ GEM diff-lcs (1.3) docile (1.1.5) dotenv (2.5.0) - ebnf (1.1.2) - rdf (>= 2.2, < 4.0) + ebnf (1.1.3) + rdf (~> 3.0) sxp (~> 1.0) excon (0.62.0) faraday (0.15.2) multipart-post (>= 1.2, < 3) - faraday-encoding (0.0.4) + faraday-encoding (0.0.5) faraday faraday_middleware (0.12.2) faraday (>= 0.7.4, < 1.0) @@ -129,7 +129,7 @@ GEM rack-test (0.8.3) rack (>= 1.0, < 3) rake (12.3.1) - rdf (3.0.2) + rdf (3.0.3) hamster (~> 3.0) link_header (~> 0.0, >= 0.0.8) rdf-aggregate-repo (2.2.1) @@ -148,7 +148,7 @@ GEM rdf-turtle (2.2.2) ebnf (~> 1.1) rdf (>= 2.2, < 4.0) - rdf-xsd (3.0.0) + rdf-xsd (3.0.1) rdf (~> 3.0) rspec (3.8.0) rspec-core (~> 3.8.0) @@ -204,4 +204,4 @@ DEPENDENCIES webmock (~> 3.0, >= 3.0.1) BUNDLED WITH - 1.16.1 + 1.16.4 diff --git a/README.md b/README.md index b2509f0..dec349a 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,19 @@ Hide DOIs with metadata for all markdown files in a folder cirneco doi hide /source/posts ``` +## Bulk Operations + +Transfer a list of DOIs. A list of DOIs must be provided in a file + +``` +10.5438/5aep-2n86 +10.5438/cd2b-xj80 +``` + +```shell +cirneco doi transfer --target DATACITE.DATACITE --jwt {YOUR-JSON-WEB-TOKEN} ./doi_transfer.txt +``` + ## Development We use rspec for unit testing: diff --git a/lib/cirneco/api.rb b/lib/cirneco/api.rb index e07fa55..6a387d0 100644 --- a/lib/cirneco/api.rb +++ b/lib/cirneco/api.rb @@ -21,6 +21,19 @@ def put_metadata(doi, options={}) Maremma.put(url, content_type: 'application/xml;charset=UTF-8', data: options[:data], username: options[:username], password: options[:password]) end + def transfer_doi(doi, options={}) + return OpenStruct.new(body: { "errors" => [{ "title" => "JWT or Username or password missing" }] }) unless options[:jwt].present? || (options[:username].present? && options[:password].present?) + + api_url = options[:sandbox] ? 'https://api.test.datacite.org' : 'https://api.datacite.org' + + url = URI.encode("#{api_url}/dois/#{doi}") + if options[:jwt].present? + Maremma.patch(url, content_type: 'application/vnd.api+json;charset=UTF-8', data: options[:data], bearer: options[:jwt]) + else + Maremma.patch(url, content_type: 'application/vnd.api+json;charset=UTF-8', data: options[:data], username: options[:username], password: options[:password]) + end + end + def get_metadata(doi, options={}) return OpenStruct.new(body: { "errors" => [{ "title" => "Username or password missing" }] }) unless options[:username].present? && options[:password].present? diff --git a/lib/cirneco/doi.rb b/lib/cirneco/doi.rb index 2de0a53..6a3e4ab 100644 --- a/lib/cirneco/doi.rb +++ b/lib/cirneco/doi.rb @@ -112,5 +112,31 @@ def check(doi) puts "Checksum for #{doi} is not valid" end end + + desc "transfer DOIs", "transfer list of DOIs" + method_option :target, :type => :string + method_option :username, :default => ENV['MDS_USERNAME'] + method_option :password, :default => ENV['MDS_PASSWORD'] + method_option :sandbox, :type => :boolean, :force => false + method_option :jwt, :default => ENV['JWT'] + 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 end end diff --git a/lib/cirneco/utils.rb b/lib/cirneco/utils.rb index 9c4f8b8..fb5cce1 100644 --- a/lib/cirneco/utils.rb +++ b/lib/cirneco/utils.rb @@ -18,6 +18,26 @@ def get_dois_by_prefix(prefix, options={}) response end + def generate_transfer_template(options={}) + response = { + "data" => { + "type" => "dois", + "attributes" => { + "mode" => "transfer" + }, + "relationships"=> { + "client"=> { + "data"=> { + "type"=> "clients", + "id"=> options[:target].downcase + } + } + } + } + } + response + end + def decode_doi(doi) prefix, string = doi.split('/', 2) Base32::URL.decode(string, checksum: true).to_i diff --git a/lib/cirneco/version.rb b/lib/cirneco/version.rb index 27fc8c8..8d58682 100644 --- a/lib/cirneco/version.rb +++ b/lib/cirneco/version.rb @@ -1,3 +1,3 @@ module Cirneco - VERSION = "0.9.25" + VERSION = "0.9.26" end