Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/datacite/cirneco
Browse files Browse the repository at this point in the history
  • Loading branch information
kjgarza committed Jan 9, 2019
2 parents f23fc26 + 62f7400 commit 46a57be
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 13 deletions.
38 changes: 26 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
cirneco (0.9.26)
cirneco (0.9.27)
activesupport (>= 4.2.5, < 6)
base32-url (~> 0.5)
bergamasco (~> 0.3)
Expand All @@ -15,7 +15,7 @@ PATH
GEM
remote: https://rubygems.org/
specs:
activesupport (5.2.1)
activesupport (5.2.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
Expand All @@ -36,41 +36,55 @@ GEM
safe_yaml (~> 1.0, >= 1.0.4)
bibtex-ruby (4.4.7)
latex-decode (~> 0.0)
bolognese (0.14.1)
bolognese (0.15.9)
activesupport (>= 4.2.5, < 6)
benchmark_methods (~> 0.7)
bibtex-ruby (~> 4.1)
builder (~> 3.2, >= 3.2.2)
citeproc-ruby (~> 1.1, >= 1.1.10)
colorize (~> 0.8.1)
csl-styles (~> 1.0, >= 1.0.1.8)
edtf (~> 3.0, >= 3.0.4)
gender_detector (~> 0.1.2)
iso8601 (~> 0.9.1)
json-ld (~> 2.1, >= 2.1.2)
jsonlint (~> 0.2.0)
loofah (~> 2.0, >= 2.0.3)
maremma (>= 3.5.7, < 5)
namae (~> 0.11.3)
namae (~> 1.0)
nokogiri (~> 1.8.1)
oj (>= 2.8.3)
postrank-uri (~> 1.0, >= 1.0.18)
rdf-rdfxml (~> 2.0)
rdf-turtle (~> 2.2)
thor (~> 0.19)
builder (3.2.3)
citeproc (1.0.9)
namae (~> 1.0)
citeproc-ruby (1.1.10)
citeproc (~> 1.0, >= 1.0.9)
csl (~> 1.5)
codeclimate-test-reporter (1.0.7)
simplecov
colorize (0.8.1)
concurrent-ruby (1.0.5)
concurrent-ruby (1.1.3)
crack (0.4.3)
safe_yaml (~> 1.0.0)
crass (1.0.4)
csl (1.5.0)
namae (~> 1.0)
csl-styles (1.0.1.9)
csl (~> 1.0)
diff-lcs (1.3)
docile (1.1.5)
dotenv (2.5.0)
ebnf (1.1.3)
rdf (~> 3.0)
sxp (~> 1.0)
edtf (3.0.4)
activesupport (>= 3.0, < 6.0)
excon (0.62.0)
faraday (0.15.2)
faraday (0.15.4)
multipart-post (>= 1.2, < 3)
faraday-encoding (0.0.5)
faraday
Expand All @@ -85,7 +99,7 @@ GEM
concurrent-ruby (~> 1.0)
hashdiff (0.3.7)
htmlentities (4.3.4)
i18n (1.1.0)
i18n (1.2.0)
concurrent-ruby (~> 1.0)
iso8601 (0.9.1)
json (2.1.0)
Expand All @@ -97,7 +111,7 @@ GEM
trollop (~> 2)
latex-decode (0.3.1)
link_header (0.0.8)
loofah (2.2.2)
loofah (2.2.3)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
maremma (4.1.1)
Expand All @@ -115,7 +129,7 @@ GEM
minitest (5.11.3)
multi_json (1.13.1)
multipart-post (2.0.0)
namae (0.11.3)
namae (1.0.1)
nokogiri (1.8.4)
mini_portile2 (~> 2.3.0)
oj (2.18.5)
Expand All @@ -129,7 +143,7 @@ GEM
rack-test (0.8.3)
rack (>= 1.0, < 3)
rake (12.3.1)
rdf (3.0.3)
rdf (3.0.7)
hamster (~> 3.0)
link_header (~> 0.0, >= 0.0.8)
rdf-aggregate-repo (2.2.1)
Expand Down Expand Up @@ -175,9 +189,9 @@ GEM
sxp (1.0.1)
rdf (>= 2.2, < 4.0)
temple (0.8.0)
thor (0.20.0)
thor (0.20.3)
thread_safe (0.3.6)
tilt (2.0.8)
tilt (2.0.9)
trollop (2.9.9)
tzinfo (1.2.5)
thread_safe (~> 0.1)
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ Transfer a list of DOIs. A list of DOIs must be provided in a file
cirneco doi transfer --target DATACITE.DATACITE --jwt {YOUR-JSON-WEB-TOKEN} ./doi_transfer.txt
```

Change the state of a list of DOIs. Note that all DOIS in the list must be in the ![previous state](https://support.datacite.org/docs/doi-states). A list of DOIs must be provided in a file

```
10.5438/5aep-2n86
10.5438/cd2b-xj80
```

In this example, we change alist of `registered` DOI to `findable` DOIS.

```shell
cirneco doi bulk_state_change --target_state findable ./test_doi_status_change.csv
```


## Development

We use rspec for unit testing:
Expand Down
22 changes: 22 additions & 0 deletions lib/cirneco/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,27 @@ def get_media(doi, options={})
end
response
end

def update_rest_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_rest_doi(doi, options={})
return OpenStruct.new(body: { "errors" => [{ "title" => "Username or password missing" }] }) unless options[:username].present? && options[:password].present?

api_url = options[:sandbox] ? 'https://api.test.datacite.org' : 'https://api.datacite.org'

url = "#{api_url}/dois/#{doi}"
Maremma.get(url, accept: 'application/vnd.api+json;charset=UTF-8', username: options[:username], password: options[:password], raw: true)
end
end
end
47 changes: 47 additions & 0 deletions lib/cirneco/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,52 @@ def transfer(file)

puts "#{count} DOIs transfered."
end

desc "change DOIs state", "modify states of list of DOIs"
method_option :target_state, :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 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})

metadata_reponse = get_rest_doi(doi, options)

unless [200].include?(metadata_reponse.status)
puts "#{doi} was not found #{metadata_reponse.status}"
next
end

metadata = JSON.parse(metadata_reponse.body.fetch("data", []))
if metadata.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
end
end
12 changes: 12 additions & 0 deletions lib/cirneco/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def generate_transfer_template(options={})
response
end

def generate_state_change_template(options={})
response = {
"data" => {
"type" => "dois",
"attributes" => {
"event" => options[:operation]
}
}
}
response
end

def decode_doi(doi)
prefix, string = doi.split('/', 2)
Base32::URL.decode(string, checksum: true)
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.26"
VERSION = "0.9.27"
end
8 changes: 8 additions & 0 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,13 @@
expect(media).to eq([{:mime_type=>"application/pdf", :url=>"http://www.datacite.org/cirneco-test.pdf"}])
end
end

describe "get_rest_doi" do
it 'should get doi' do
r = subject.get_rest_doi(subject.doi, options)
response = JSON.parse(r.body.fetch("data", []))
expect(response.dig("data","id")).to eq("10.5438/6brg-2m37")
end
end
end
end
Loading

0 comments on commit 46a57be

Please sign in to comment.