Class: Hyrax::DOI::DataCiteClient

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/doi/datacite_client.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

TEST_BASE_URL =
"https://api.test.datacite.org/"
TEST_MDS_BASE_URL =
"https://mds.test.datacite.org/"
PRODUCTION_BASE_URL =
"https://api.datacite.org"
PRODUCTION_MDS_BASE_URL =
"https://mds.datacite.org/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username:, password:, prefix:, mode: :production) ⇒ DataCiteClient

Returns a new instance of DataCiteClient.



12
13
14
15
16
17
# File 'app/services/hyrax/doi/datacite_client.rb', line 12

def initialize(username:, password:, prefix:, mode: :production)
  @username = username
  @password = password
  @prefix = prefix
  @mode = mode
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



5
6
7
# File 'app/services/hyrax/doi/datacite_client.rb', line 5

def mode
  @mode
end

#passwordObject (readonly)

Returns the value of attribute password.



5
6
7
# File 'app/services/hyrax/doi/datacite_client.rb', line 5

def password
  @password
end

#prefixObject (readonly)

Returns the value of attribute prefix.



5
6
7
# File 'app/services/hyrax/doi/datacite_client.rb', line 5

def prefix
  @prefix
end

#usernameObject (readonly)

Returns the value of attribute username.



5
6
7
# File 'app/services/hyrax/doi/datacite_client.rb', line 5

def username
  @username
end

Instance Method Details

#create_draft_doiObject

Mint a draft DOI without metadata or a url If you already have a DOI and want to register it as a draft then go through the normal process (put_metadata/register_url)

Raises:



21
22
23
24
25
26
27
# File 'app/services/hyrax/doi/datacite_client.rb', line 21

def create_draft_doi
  # Use regular api instead of mds for metadata-less url-less draft doi creation
  response = connection.post('dois', draft_doi_payload.to_json, "Content-Type" => "application/json")
  raise Error.new('Failed creating draft DOI', response) unless response.status == 201

  JSON.parse(response.body)['data']['id']
end

#delete_draft_doi(doi) ⇒ Object

Raises:



29
30
31
32
33
34
# File 'app/services/hyrax/doi/datacite_client.rb', line 29

def delete_draft_doi(doi)
  response = mds_connection.delete("doi/#{doi}")
  raise Error.new('Failed deleting draft DOI', response) unless response.status == 200

  doi
end

#delete_metadata(doi) ⇒ Object

Beware: This will make findable DOIs become registered (by setting is_active to false) Otherwise this has no effect on the DOI’s metadata (even when draft) Beware: Attempts to delete the metadata of an unknown DOI will actually create a blank draft DOI

Raises:



58
59
60
61
62
63
# File 'app/services/hyrax/doi/datacite_client.rb', line 58

def (doi)
  response = mds_connection.delete("metadata/#{doi}")
  raise Error.new('Failed deleting DOI metadata', response) unless response.status == 200

  doi
end

#get_metadata(doi) ⇒ Object

Raises:



36
37
38
39
40
41
# File 'app/services/hyrax/doi/datacite_client.rb', line 36

def (doi)
  response = mds_connection.get("metadata/#{doi}")
  raise Error.new('Failed getting DOI metadata', response) unless response.status == 200

  Nokogiri::XML(response.body).remove_namespaces!
end

#get_url(doi) ⇒ Object

Raises:



65
66
67
68
69
70
# File 'app/services/hyrax/doi/datacite_client.rb', line 65

def get_url(doi)
  response = mds_connection.get("doi/#{doi}")
  raise Error.new('Failed getting DOI url', response) unless response.status == 200

  response.body
end

#put_metadata(doi, metadata) ⇒ Object

This will mint a new draft DOI if the passed doi parameter is blank The passed datacite xml needs an identifier (just the prefix when minting new DOIs) Beware: This will convert registered DOIs into findable!

Raises:



46
47
48
49
50
51
52
53
# File 'app/services/hyrax/doi/datacite_client.rb', line 46

def (doi, )
  doi = prefix if doi.blank?
  response = mds_connection.put("metadata/#{doi}", , { 'Content-Type': 'application/xml;charset=UTF-8' })
  raise Error.new('Failed creating metadata for DOI', response) unless response.status == 201

  /^OK \((?<found_or_created_doi>.*)\)$/ =~ response.body
  found_or_created_doi
end

#register_url(doi, url) ⇒ Object

Beware: This will convert draft DOIs to findable! Metadata needs to be registered for a DOI before a url can be registered

Raises:



74
75
76
77
78
79
80
# File 'app/services/hyrax/doi/datacite_client.rb', line 74

def register_url(doi, url)
  payload = "doi=#{doi}\nurl=#{url}"
  response = mds_connection.put("doi/#{doi}", payload, { 'Content-Type': 'text/plain;charset=UTF-8' })
  raise Error.new('Failed registering url for DOI', response) unless response.status == 201

  url
end