Module: Bolognese::DoiUtils
- Included in:
- CLI, MetadataUtils
- Defined in:
- lib/bolognese/doi_utils.rb
Instance Method Summary collapse
- #doi_api_url(doi, options = {}) ⇒ Object
- #doi_as_url(doi) ⇒ Object
- #doi_from_url(url) ⇒ Object
- #doi_resolver(doi, options = {}) ⇒ Object
-
#get_doi_ra(doi) ⇒ Object
get DOI registration agency.
- #normalize_doi(doi, options = {}) ⇒ Object
- #validate_doi(doi) ⇒ Object
- #validate_funder_doi(doi) ⇒ Object
- #validate_prefix(doi) ⇒ Object
Instance Method Details
#doi_api_url(doi, options = {}) ⇒ Object
29 30 31 32 |
# File 'lib/bolognese/doi_utils.rb', line 29 def doi_api_url(doi, = {}) sandbox = Array(/handle.test.datacite.org/.match(doi)).last sandbox.present? || [:sandbox] ? "https://api.test.datacite.org/dois/" + doi_from_url(doi) : "https://api.datacite.org/dois/" + doi_from_url(doi) end |
#doi_as_url(doi) ⇒ Object
49 50 51 |
# File 'lib/bolognese/doi_utils.rb', line 49 def doi_as_url(doi) "https://doi.org/#{doi}" if doi.present? end |
#doi_from_url(url) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/bolognese/doi_utils.rb', line 42 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
24 25 26 27 |
# File 'lib/bolognese/doi_utils.rb', line 24 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 |
#get_doi_ra(doi) ⇒ Object
get DOI registration agency
54 55 56 57 58 59 60 61 62 |
# File 'lib/bolognese/doi_utils.rb', line 54 def get_doi_ra(doi) prefix = validate_prefix(doi) return nil if prefix.blank? url = "https://doi.org/ra/#{prefix}" result = Maremma.get(url) result.body.dig("data", 0, "RA") end |
#normalize_doi(doi, options = {}) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/bolognese/doi_utils.rb', line 34 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
5 6 7 8 9 |
# File 'lib/bolognese/doi_utils.rb', line 5 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.delete("\u200B").downcase if doi.present? end |
#validate_funder_doi(doi) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/bolognese/doi_utils.rb', line 11 def validate_funder_doi(doi) doi = Array(/\A(?:(http|https):\/(\/)?(dx\.)?(doi.org|handle.test.datacite.org)\/)?(doi:)?(10\.13039\/)?(5.+)\z/.match(doi)).last # remove non-printing whitespace and downcase if doi.present? doi.delete("\u200B").downcase "https://doi.org/10.13039/#{doi}" end end |
#validate_prefix(doi) ⇒ Object
20 21 22 |
# File 'lib/bolognese/doi_utils.rb', line 20 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 |