Sha256: 800714b010f851ec432f56c677d68d836b2e7f2e6a01fe5b33078894dadfb902

Contents?: true

Size: 1.81 KB

Versions: 38

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module Bolognese
  module DoiUtils
    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

    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

    def doi_resolver(doi, options = {})
      sandbox = Array(/handle.test.datacite.org/.match(doi)).last
      sandbox.present? || options[:sandbox] ? "https://handle.test.datacite.org/" : "https://doi.org/"
    end

    def doi_search(doi, options = {})
      sandbox = Array(/handle.test.datacite.org/.match(doi)).last
      sandbox.present? || options[:sandbox] ? "https://search.test.datacite.org/api" : "https://search.datacite.org/api"
    end

    def normalize_doi(doi, options = {})
      doi_str = validate_doi(doi)
      return nil unless doi_str.present?

      # turn DOI into URL, escape unsafe characters
      doi_resolver(doi, options) + Addressable::URI.encode(doi_str)
    end

    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

    def doi_as_url(doi)
      "https://doi.org/#{doi}" if doi.present?
    end

    # get DOI registration agency
    def get_doi_ra(doi)
      prefix = validate_prefix(doi)
      return nil if prefix.blank?

      url = "https://app.datacite.org/prefixes/#{prefix}"
      result = Maremma.get(url)

      result.body.fetch("data", {}).fetch('attributes', {}).fetch('registration-agency', nil)
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
bolognese-1.0.16 lib/bolognese/doi_utils.rb
bolognese-1.0.15 lib/bolognese/doi_utils.rb
bolognese-1.0.13 lib/bolognese/doi_utils.rb
bolognese-1.0.12 lib/bolognese/doi_utils.rb
bolognese-1.0.11 lib/bolognese/doi_utils.rb
bolognese-1.0.10 lib/bolognese/doi_utils.rb
bolognese-1.0.9 lib/bolognese/doi_utils.rb
bolognese-1.0.8 lib/bolognese/doi_utils.rb
bolognese-1.0.7 lib/bolognese/doi_utils.rb
bolognese-1.0.6 lib/bolognese/doi_utils.rb
bolognese-1.0.5 lib/bolognese/doi_utils.rb
bolognese-1.0.4 lib/bolognese/doi_utils.rb
bolognese-0.15.6 lib/bolognese/doi_utils.rb
bolognese-1.0.3 lib/bolognese/doi_utils.rb
bolognese-1.0.2 lib/bolognese/doi_utils.rb
bolognese-1.0.1 lib/bolognese/doi_utils.rb
bolognese-1.0 lib/bolognese/doi_utils.rb
bolognese-0.15.3 lib/bolognese/doi_utils.rb
bolognese-0.15.1 lib/bolognese/doi_utils.rb
bolognese-0.14.1 lib/bolognese/doi_utils.rb