Class: Identifiers::ORCID

Inherits:
Object
  • Object
show all
Defined in:
lib/identifiers/orcid.rb

Constant Summary collapse

REGEXP =
/\b(?:\d{4}-){3}\d{3}[\dx]\b/i

Class Method Summary collapse

Class Method Details

.calculate_digit(str) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/identifiers/orcid.rb', line 16

def self.calculate_digit(str)
  return unless str =~ REGEXP

  base_digits = str.chop.tr('-', '')
  total = 0
  base_digits.each_char { |c| total = (total + Integer(c)) * 2 }
  remainder = total % 11
  result = (12 - remainder) % 11
  result == 10 ? 'X' : result.to_s
end

.extract(str) ⇒ Object



5
6
7
# File 'lib/identifiers/orcid.rb', line 5

def self.extract(str)
  str.to_s.scan(REGEXP).select { |orcid| valid?(orcid) }.map(&:upcase)
end

.valid?(str) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
# File 'lib/identifiers/orcid.rb', line 9

def self.valid?(str)
  digit = calculate_digit(str)
  return false unless digit

  digit == str[-1].upcase
end