Class: Whedon::OrcidValidator
- Inherits:
-
Object
- Object
- Whedon::OrcidValidator
- Defined in:
- lib/whedon/orcid_validator.rb
Instance Attribute Summary collapse
-
#orcid ⇒ Object
readonly
Returns the value of attribute orcid.
Instance Method Summary collapse
- #check_chars ⇒ Object
- #check_length ⇒ Object
- #check_structure ⇒ Object
- #checksum ⇒ Object
-
#checksum_char ⇒ Object
Returns the last character of the string.
- #first_11 ⇒ Object
-
#initialize(orcid) ⇒ OrcidValidator
constructor
A new instance of OrcidValidator.
- #packed_orcid ⇒ Object
-
#validate ⇒ Object
Returns true or false.
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
#orcid ⇒ Object (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_chars ⇒ Object
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_length ⇒ Object
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_structure ⇒ Object
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 |
#checksum ⇒ Object
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_char ⇒ Object
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_11 ⇒ Object
37 38 39 |
# File 'lib/whedon/orcid_validator.rb', line 37 def first_11 packed_orcid.chop end |
#packed_orcid ⇒ Object
28 29 30 |
# File 'lib/whedon/orcid_validator.rb', line 28 def packed_orcid orcid.gsub('-', '') end |
#validate ⇒ Object
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 |