Class: Datacite::Mds
- Inherits:
-
Object
- Object
- Datacite::Mds
- Defined in:
- lib/datacite_mds.rb
Overview
Wraps up Mds functionality wihin an object
Instance Attribute Summary collapse
-
#http ⇒ Net::HTTP
the object wrapping up the http connection to Datacite Endpoint.
-
#passwd ⇒ String
the Datacentre’s authorised password.
-
#test_mode ⇒ Boolean
indicates whether to make APi calls in test mode.
-
#uri ⇒ URI
the object wrapping up the Datacite Endpoint.
-
#username ⇒ String
the Datacentre’s authorised username.
Class Method Summary collapse
-
.metadata_valid?(xml_doc) ⇒ Boolean
Validates the passed XML against the Datacite XML schema v3.1.
-
.validation_errors ⇒ Object
Returns the XML parser errors, as set by metadata_valid? class method.
Instance Method Summary collapse
-
#delete_metadata(doi) ⇒ Net::HTTPResponse
Deletes the metadata associated with the DOI.
-
#get_all_dois ⇒ Net::HTTPResponse
Returns a list of all DOIs for the requesting datacentre.
-
#get_metadata(doi) ⇒ Net::HTTPResponse
Returns the most recent version of metadata associated with the DOI.
-
#initialize(**options) ⇒ Mds
constructor
creates a new Mds object, passing an options hash.
-
#mint(doi, url) ⇒ Net::HTTPResponse
Will mint new DOI if specified DOI doesn’t exist.
-
#resolve(doi) ⇒ Net::HTTPResponse
Returns a url associated with a given DOI.
-
#upload_metadata(xml_string) ⇒ Net::HTTPResponse
Stores new version of metadata.
Constructor Details
#initialize(**options) ⇒ Mds
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
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/datacite_mds.rb', line 38 def initialize(**) if [:authorize] @username = [:authorize][:usr] @passwd = [:authorize][:pwd] else @username = ENV['DATACITE_USR'] @passwd = ENV['DATACITE_PWD'] end @test_mode = [:testing] ? '?testMode=true' : '' @uri = URI.parse(ENDPOINT) end |
Instance Attribute Details
#http ⇒ Net::HTTP
the object wrapping up the http connection to Datacite Endpoint
23 24 25 |
# File 'lib/datacite_mds.rb', line 23 def http @http end |
#passwd ⇒ String
the Datacentre’s authorised password
23 24 25 |
# File 'lib/datacite_mds.rb', line 23 def passwd @passwd end |
#test_mode ⇒ Boolean
indicates whether to make APi calls in test mode
23 24 25 |
# File 'lib/datacite_mds.rb', line 23 def test_mode @test_mode end |
#uri ⇒ URI
the object wrapping up the Datacite Endpoint
23 24 25 |
# File 'lib/datacite_mds.rb', line 23 def uri @uri end |
#username ⇒ String
the Datacentre’s authorised 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
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.}" end @errors = DATACITE_SCHEMA.validate(doc) @errors.empty? end |
.validation_errors ⇒ Object
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.
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_dois ⇒ Net::HTTPResponse
There is no guaranteed order in the list of DOIs
Returns a list of all DOIs for the requesting datacentre
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.
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.
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
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.
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 |