Class: Identifier::Global::Orcid

Inherits:
Identifier::Global show all
Defined in:
app/models/identifier/global/orcid.rb

Overview

ORCID iD - orcid.org/

Constant Summary

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary

Attributes inherited from Identifier::Global

#relation

Attributes inherited from Identifier

#cached, #identifier, #identifier_object_id, #namespace_id, #project_id, #type

Attributes included from Shared::DualAnnotator

#is_community_annotation

Instance Method Summary collapse

Methods inherited from Identifier::Global

#build_cached, #permit_only_one_global_without_relation_supplied_per_type, #set_cached, #sv_resolves?

Methods included from SoftValidation

#clear_soft_validations, #fix_for, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations, #soft_validators

Methods inherited from Identifier

prototype_identifier, #set_cached, #type_name

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::Labels

#labeled?

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods included from Shared::DualAnnotator

#is_community_annotation?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Method Details

#generate_check_digit(base_digits) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'app/models/identifier/global/orcid.rb', line 24

def generate_check_digit(base_digits)
  total = 0

  base_digits.each_char { |digit|
    total = (total + digit.to_i) * 2
  }
  remainder = (total % 11)
  result = (12 - remainder) % 11
  return result == 10 ? 'X' : result.to_s
end

#using_orcid_classObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/identifier/global/orcid.rb', line 6

def using_orcid_class
  unless identifier.nil?
    orcid = identifier.upcase

    /^(?<preamble>http[s]?:\/\/){0,1}(?<domain>orcid\.org\/){1}(?<part_1>\d{4})-(?<part_2>\d{4})-(?<part_3>\d{4})-(?<part_4>\d{3})(?<last>.)$/i =~ orcid

    if domain.nil? or part_1.nil? or part_2.nil? or part_3.nil? or part_4.nil? or last.nil?
      errors.add(:identifier, "'#{identifier}' is an improperly formed ORCID ID.")
      return
    end

    if last != generate_check_digit(part_1 + part_2 + part_3 + part_4)
      errors.add(:identifier, "'#{identifier}' has bad check digit.")
      return
    end
  end
end