Sha256: dc9012d08b1b2f01ce38f78a410a0ec5b375220d11ed25cd162a19feb554d7b5

Contents?: true

Size: 1.23 KB

Versions: 32

Compression:

Stored size: 1.23 KB

Contents

module Bolognese
  module DoiUtils
    def validate_doi(doi)
      Array(/\A(?:(http|https):\/(\/)?(dx\.)?doi.org\/)?(doi:)?(10\.\d{4,5}\/.+)\z/.match(doi)).last
    end

    def validate_prefix(doi)
      Array(/\A(?:(http|https):\/(\/)?(dx\.)?doi.org\/)?(doi:)?(10\.\d{4,5})\/.+\z/.match(doi)).last
    end

    def normalize_doi(doi)
      doi = validate_doi(doi)
      return nil unless doi.present?

      # remove non-printing whitespace and downcase
      doi = doi.delete("\u200B").downcase

      # turn DOI into URL, escape unsafe characters
      "https://doi.org/" + Addressable::URI.encode(doi)
    end

    def doi_from_url(url)
      if /\A(?:(http|https):\/\/(dx\.)?doi.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/#{clean_doi(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://api.datacite.org/prefixes/#{prefix}"
      result = Maremma.get(url)

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

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
bolognese-0.9.31 lib/bolognese/doi_utils.rb
bolognese-0.9.30 lib/bolognese/doi_utils.rb
bolognese-0.9.29 lib/bolognese/doi_utils.rb
bolognese-0.9.28 lib/bolognese/doi_utils.rb
bolognese-0.9.27 lib/bolognese/doi_utils.rb
bolognese-0.9.26 lib/bolognese/doi_utils.rb
bolognese-0.9.25 lib/bolognese/doi_utils.rb
bolognese-0.9.24 lib/bolognese/doi_utils.rb
bolognese-0.9.23 lib/bolognese/doi_utils.rb
bolognese-0.9.22 lib/bolognese/doi_utils.rb
bolognese-0.9.21 lib/bolognese/doi_utils.rb
bolognese-0.9.20 lib/bolognese/doi_utils.rb
bolognese-0.9.19 lib/bolognese/doi_utils.rb
bolognese-0.9.18 lib/bolognese/doi_utils.rb
bolognese-0.9.17 lib/bolognese/doi_utils.rb
bolognese-0.9.16 lib/bolognese/doi_utils.rb
bolognese-0.9.15 lib/bolognese/doi_utils.rb
bolognese-0.9.14 lib/bolognese/doi_utils.rb
bolognese-0.9.13 lib/bolognese/doi_utils.rb
bolognese-0.9.11 lib/bolognese/doi_utils.rb