Class: Datacite::Mds

Inherits:
Object
  • Object
show all
Defined in:
lib/datacite_mds.rb

Overview

Wraps up Mds functionality wihin an object

Author:

  • Fred Heath

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Mds

Note:

If :authorize is not passed as an option , then the method will look for the usrname and password in environment variables DATACITE_USR and DATACITE_PWD.

creates a new Mds object, passing an options hash

Parameters:

  • options (Hash)

    the options to create an Mds objects with.

Options Hash (**options):

  • :authorize (Hash)

    Authorization includes two keys :usr [String], :pwd [String]

  • :test_mode (String)

    If true, all API calls to Datacite will occur in test mode



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/datacite_mds.rb', line 38

def initialize(**options)
  if options[:authorize]
    @username = options[:authorize][:usr]
    @passwd = options[:authorize][:pwd]
  else
    @username = ENV['DATACITE_USR']
    @passwd = ENV['DATACITE_PWD']
  end
  @test_mode = options[:testing] ? '?testMode=true' : ''
  @uri = URI.parse(ENDPOINT)

end

Instance Attribute Details

#httpNet::HTTP

the object wrapping up the http connection to Datacite Endpoint

Returns:

  • (Net::HTTP)

    the current value of http



23
24
25
# File 'lib/datacite_mds.rb', line 23

def http
  @http
end

#passwdString

the Datacentre’s authorised password

Returns:

  • (String)

    the current value of passwd



23
24
25
# File 'lib/datacite_mds.rb', line 23

def passwd
  @passwd
end

#test_modeBoolean

indicates whether to make APi calls in test mode

Returns:

  • (Boolean)

    the current value of test_mode



23
24
25
# File 'lib/datacite_mds.rb', line 23

def test_mode
  @test_mode
end

#uriURI

the object wrapping up the Datacite Endpoint

Returns:

  • (URI)

    the current value of uri



23
24
25
# File 'lib/datacite_mds.rb', line 23

def uri
  @uri
end

#usernameString

the Datacentre’s authorised username

Returns:

  • (String)

    the current value of username



23
24
25
# File 'lib/datacite_mds.rb', line 23

def username
  @username
end

Class Method Details

.metadata_valid?(xml_doc) ⇒ Boolean

Validates the passed XML against the Datacite XML schema v3.1. Method will first check the well-formedness of the XML string and then validate it against the Schema errors

Parameters:

  • xml_doc (String)

    an XML string, representing dataset metadata

Returns:

  • (Boolean)

    Boolean True if XML is valid. If XML is invalid then the



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/datacite_mds.rb', line 149

def self.(xml_doc)

  begin
    doc = Nokogiri::XML(xml_doc) { |config| config.strict }
  rescue Nokogiri::XML::SyntaxError => e
    raise  ArgumentError, "Badly-formed meta-data XML: #{e.message}"
  end

  @errors = DATACITE_SCHEMA.validate(doc)
  @errors.empty?
end

.validation_errorsObject

Returns the XML parser errors, as set by metadata_valid? class method



163
164
165
# File 'lib/datacite_mds.rb', line 163

def self.validation_errors
  @errors
end

Instance Method Details

#delete_metadata(doi) ⇒ Net::HTTPResponse

Deletes the metadata associated with the DOI.

Parameters:

  • doi (String)

    a Data Object identifier

Returns:

  • (Net::HTTPResponse)

    Succesful operation will return HTTPOK



133
134
135
136
137
138
# File 'lib/datacite_mds.rb', line 133

def (doi)
  @uri.path = RESOURCES[:metadata] + '/' + doi
  @http = Net::HTTP.new(@uri.host, @uri.port)
  request = Net::HTTP::Delete.new(@uri.request_uri)
  call_datacite(request)
end

#get_all_doisNet::HTTPResponse

Note:

There is no guaranteed order in the list of DOIs

Returns a list of all DOIs for the requesting datacentre

Returns:

  • (Net::HTTPResponse)

    Succesful operation will return HTTPOK and the response body will contain a list (String) of all relevant DOIs.



68
69
70
71
72
73
# File 'lib/datacite_mds.rb', line 68

def get_all_dois
  @uri.path = RESOURCES[:doi]
  @http = Net::HTTP.new(@uri.host, @uri.port)
  request = Net::HTTP::Get.new(@uri.request_uri)
  call_datacite(request)
end

#get_metadata(doi) ⇒ Net::HTTPResponse

Returns the most recent version of metadata associated with the DOI.

Parameters:

  • doi (String)

    a Data Object identifier

Returns:

  • (Net::HTTPResponse)

    Succesful operation will return HTTPOK and the response body will consist of XML representing a dataset’s metadata.



121
122
123
124
125
126
# File 'lib/datacite_mds.rb', line 121

def (doi)
  @uri.path = RESOURCES[:metadata] + '/' + doi
  @http = Net::HTTP.new(@uri.host, @uri.port)
  request = Net::HTTP::Get.new(@uri.request_uri)
  call_datacite(request)
end

#mint(doi, url) ⇒ Net::HTTPResponse

Will mint new DOI if specified DOI doesn’t exist. This method will attempt to update URL if you specify existing DOI. Standard domains and quota restrictions check will be performed by Datacite.

Parameters:

  • doi (String)

    a Data Object Identifier

  • url (String)

    the dataset’s location

Returns:

  • (Net::HTTPResponse)

    Succesful operation will return HTTPCreated and the response body will provide a short explanation of the status code.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/datacite_mds.rb', line 84

def mint(doi, url)
  @uri.path = RESOURCES[:doi]
  @http = Net::HTTP.new(@uri.host, @uri.port)

  request = Net::HTTP::Post.new(@uri.request_uri)
  @uri.query = @test_mode unless @test_mode.empty?

  request.content_type = 'text/plain'
  request.set_form_data({doi: doi, url: url})

  call_datacite(request)
end

#resolve(doi) ⇒ Net::HTTPResponse

Returns a url associated with a given DOI

Parameters:

  • doi (String)

    a Data Object identifier

Returns:

  • (Net::HTTPResponse)

    Succesful operation will return HTTPOK and the response body will contain the URL (String) representing the dataset.



56
57
58
59
60
61
# File 'lib/datacite_mds.rb', line 56

def resolve(doi)
  @uri.path = RESOURCES[:doi] + '/' + doi
  @http = Net::HTTP.new(@uri.host, @uri.port)
  request = Net::HTTP::Get.new(@uri.request_uri)
  call_datacite(request)
end

#upload_metadata(xml_string) ⇒ Net::HTTPResponse

Stores new version of metadata.

Parameters:

Returns:

  • (Net::HTTPResponse)

    Succesful operation will return HTTPCreated and the response body will provide a short explanation of the status code.



103
104
105
106
107
108
109
110
111
112
# File 'lib/datacite_mds.rb', line 103

def (xml_string)
  @uri.path = RESOURCES[:metadata]
  @http = Net::HTTP.new(@uri.host, @uri.port)

  @uri.query = @test_mode unless @test_mode.empty?
  request = Net::HTTP::Post.new(@uri.request_uri)
  request.content_type = 'application/xml'
  request.body = xml_string
  call_datacite(request)
end