Module: Bolognese::DoiUtils
Instance Method Summary collapse
- #doi_as_url(doi) ⇒ Object
- #doi_from_url(url) ⇒ Object
- #doi_resolver(doi, options = {}) ⇒ Object
- #doi_search(doi, options = {}) ⇒ Object
-
#get_doi_ra(doi) ⇒ Object
get DOI registration agency.
- #normalize_doi(doi, options = {}) ⇒ Object
- #validate_doi(doi) ⇒ Object
- #validate_prefix(doi) ⇒ Object
Instance Method Details
#doi_as_url(doi) ⇒ Object
38 39 40 |
# File 'lib/bolognese/doi_utils.rb', line 38 def doi_as_url(doi) "https://doi.org/#{doi}" if doi.present? end |
#doi_from_url(url) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/bolognese/doi_utils.rb', line 31 def doi_from_url(url) if /\A(?:(http|https):\/\/(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}\/.+)\z/.match(url) uri = Addressable::URI.parse(url) uri.path.gsub(/^\//, '').downcase end end |
#doi_resolver(doi, options = {}) ⇒ Object
13 14 15 16 |
# File 'lib/bolognese/doi_utils.rb', line 13 def doi_resolver(doi, ={}) sandbox = Array(/handle.test.datacite.org/.match(doi)).last sandbox.present? || [:sandbox] ? "https://handle.test.datacite.org/" : "https://doi.org/" end |
#doi_search(doi, options = {}) ⇒ Object
18 19 20 21 |
# File 'lib/bolognese/doi_utils.rb', line 18 def doi_search(doi, ={}) sandbox = Array(/handle.test.datacite.org/.match(doi)).last sandbox.present? || [:sandbox] ? "https://api.test.datacite.org" : "https://api.datacite.org" end |
#get_doi_ra(doi) ⇒ Object
get DOI registration agency
43 44 45 46 47 48 49 50 51 |
# File 'lib/bolognese/doi_utils.rb', line 43 def get_doi_ra(doi) prefix = validate_prefix(doi) return nil if prefix.blank? url = "https://api.datacite.org/prefixes/#{prefix}" result = Maremma.get(url) result.body.fetch("data", {}).fetch('attributes', {}).fetch('registration-agency', nil) end |
#normalize_doi(doi, options = {}) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/bolognese/doi_utils.rb', line 23 def normalize_doi(doi, ={}) doi_str = validate_doi(doi) return nil unless doi_str.present? # turn DOI into URL, escape unsafe characters doi_resolver(doi, ) + Addressable::URI.encode(doi_str) end |
#validate_doi(doi) ⇒ Object
3 4 5 6 7 |
# File 'lib/bolognese/doi_utils.rb', line 3 def validate_doi(doi) doi = Array(/\A(?:(http|https):\/(\/)?(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}\/.+)\z/.match(doi)).last # remove non-printing whitespace and downcase doi = doi.delete("\u200B").downcase if doi.present? end |
#validate_prefix(doi) ⇒ Object
9 10 11 |
# File 'lib/bolognese/doi_utils.rb', line 9 def validate_prefix(doi) Array(/\A(?:(http|https):\/(\/)?(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.\d{4,5}).*\z/.match(doi)).last end |