Skip to content

Commit

Permalink
Merge pull request #4 from datacite/feature_bulktrasnfer
Browse files Browse the repository at this point in the history
Feature bulk-transfer
  • Loading branch information
kjgarza authored Sep 20, 2018
2 parents 522587f + ba5ff0e commit 5e803da
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 9 deletions.
16 changes: 8 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -204,4 +204,4 @@ DEPENDENCIES
webmock (~> 3.0, >= 3.0.1)

BUNDLED WITH
1.16.1
1.16.4
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions lib/cirneco/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
26 changes: 26 additions & 0 deletions lib/cirneco/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions lib/cirneco/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/cirneco/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Cirneco
VERSION = "0.9.25"
VERSION = "0.9.26"
end

0 comments on commit 5e803da

Please sign in to comment.