Class: Whedon::OrcidValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/whedon/orcid_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(orcid) ⇒ OrcidValidator

Returns a new instance of OrcidValidator.



11
12
13
# File 'lib/whedon/orcid_validator.rb', line 11

def initialize(orcid)
  @orcid = orcid.strip
end

Instance Attribute Details

#orcidObject (readonly)

Returns the value of attribute orcid.



9
10
11
# File 'lib/whedon/orcid_validator.rb', line 9

def orcid
  @orcid
end

Instance Method Details

#check_charsObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/whedon/orcid_validator.rb', line 58

def check_chars
  valid = true
  first_11.each_char do |c|
    if !c.numeric?
      warn("Invalid ORDIC digit (#{c})")
      valid = false
    end
  end

  return valid
end

#check_lengthObject



50
51
52
53
54
55
56
# File 'lib/whedon/orcid_validator.rb', line 50

def check_length
  if packed_orcid.length == 16
    return true
  else
    warn("ORCID looks to be the wrong length") and return false
  end
end

#check_structureObject



41
42
43
44
45
46
47
48
# File 'lib/whedon/orcid_validator.rb', line 41

def check_structure
  groups = orcid.split('-')
  if groups.size == 4
    return true
  else
    warn("ORCID looks malformed") and return false
  end
end

#checksumObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/whedon/orcid_validator.rb', line 71

def checksum
  total = 0
  first_11.each_char do |c|
    total = (total + c.to_i) * 2
  end

  remainder = total % 11
  result = (12 - remainder) % 11

  return result
end

#checksum_charObject

Returns the last character of the string



33
34
35
# File 'lib/whedon/orcid_validator.rb', line 33

def checksum_char
  packed_orcid[-1]
end

#first_11Object



37
38
39
# File 'lib/whedon/orcid_validator.rb', line 37

def first_11
  packed_orcid.chop
end

#packed_orcidObject



28
29
30
# File 'lib/whedon/orcid_validator.rb', line 28

def packed_orcid
  orcid.gsub('-', '')
end

#validateObject

Returns true or false



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/whedon/orcid_validator.rb', line 16

def validate
  return false unless check_structure
  return false unless check_length
  return false unless check_chars

  if checksum_char == "X" || checksum_char == "x"
    return checksum == 10
  else
    return checksum == checksum_char.to_i
  end
end