Sha256: 5db41667b34082c94d31a3bd4b788bf0ecec4ebc66bfb102dd12bb612dd13969

Contents?: true

Size: 1.77 KB

Versions: 17

Compression:

Stored size: 1.77 KB

Contents

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 = 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://app.test.datacite.org" : "https://app.datacite.org"
    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

17 entries across 17 versions & 1 rubygems

Version Path
bolognese-0.9.92 lib/bolognese/doi_utils.rb
bolognese-0.9.91 lib/bolognese/doi_utils.rb
bolognese-0.9.89 lib/bolognese/doi_utils.rb
bolognese-0.9.88 lib/bolognese/doi_utils.rb
bolognese-0.9.87 lib/bolognese/doi_utils.rb
bolognese-0.9.86 lib/bolognese/doi_utils.rb
bolognese-0.9.85 lib/bolognese/doi_utils.rb
bolognese-0.9.84 lib/bolognese/doi_utils.rb
bolognese-0.9.83 lib/bolognese/doi_utils.rb
bolognese-0.9.82 lib/bolognese/doi_utils.rb
bolognese-0.9.81 lib/bolognese/doi_utils.rb
bolognese-0.9.80 lib/bolognese/doi_utils.rb
bolognese-0.9.79 lib/bolognese/doi_utils.rb
bolognese-0.9.78 lib/bolognese/doi_utils.rb
bolognese-0.9.77 lib/bolognese/doi_utils.rb
bolognese-0.9.76 lib/bolognese/doi_utils.rb
bolognese-0.9.75 lib/bolognese/doi_utils.rb