lib/commonmeta/utils.rb in commonmeta-ruby-3.2.15 vs lib/commonmeta/utils.rb in commonmeta-ruby-3.3
- old
+ new
@@ -541,10 +541,15 @@
def validate_orcid(orcid)
orcid = Array(%r{\A(?:(?:http|https)://(?:(?:www|sandbox)?\.)?orcid\.org/)?(\d{4}[[:space:]-]\d{4}[[:space:]-]\d{4}[[:space:]-]\d{3}[0-9X]+)\z}.match(orcid)).last
orcid.gsub(/[[:space:]]/, "-") if orcid.present?
end
+ def validate_ror(ror)
+ ror = Array(%r{\A(?:(?:http|https)://ror\.org/)?([0-9a-z]{7}\d{2})\z}.match(ror)).last
+ ror.gsub(/[[:space:]]/, "-") if ror.present?
+ end
+
def validate_orcid_scheme(orcid_scheme)
Array(%r{\A(http|https)://(www\.)?(orcid\.org)}.match(orcid_scheme)).last
end
def validate_url(str)
@@ -632,10 +637,18 @@
# turn ORCID ID into URL
"https://orcid.org/" + Addressable::URI.encode(orcid)
end
+ def normalize_ror(ror)
+ ror = validate_ror(ror)
+ return nil unless ror.present?
+
+ # turn ROR ID into URL
+ "https://ror.org/" + Addressable::URI.encode(ror)
+ end
+
# pick electronic issn if there are multiple
# format issn as xxxx-xxxx
def normalize_issn(input, options = {})
content = options[:content] || "__content__"
@@ -1382,10 +1395,27 @@
str = "#{suffix[0, 5]}-#{suffix[5, 10]}"
end
"https://doi.org/#{prefix}/#{str}"
end
+ def encode_doi_for_uuid(uuid, options = {})
+ # look up prefix for rogue scholar blog associated with uuid
+ # returns nil if unknown uuid or doi registration is not enabled for blog
+ json_feed_by_uuid(uuid)
+ # DOI suffix is a generated from a random number, encoded in base32
+ # suffix has 8 digits plus two checksum digits. With base32 there are
+ # 32 possible digits, so 8 digits gives 32^8 possible combinations
+ if options[:uuid]
+ str = Base32::URL.encode_uuid(options[:uuid], split: 7, checksum: true)
+ else
+ random_int = SecureRandom.random_number(32 ** 7..(32 ** 8) - 1)
+ suffix = Base32::URL.encode(random_int, checksum: true)
+ str = "#{suffix[0, 5]}-#{suffix[5, 10]}"
+ end
+ "https://doi.org/#{prefix}/#{str}"
+ end
+
def decode_doi(doi, options = {})
suffix = doi.split("/", 5).last
if options[:uuid]
Base32::URL.decode_uuid(suffix)
else
@@ -1412,8 +1442,12 @@
"https://rogue-scholar.org/api/posts/unregistered"
end
def json_feed_by_blog_url(blog_id)
"https://rogue-scholar.org/api/blogs/#{blog_id}"
+ end
+
+ def json_feed_item_by_uuid_url(uuid)
+ "https://rogue-scholar.org/api/posts/#{uuid}"
end
end
end