Module: Bolognese::AuthorUtils
- Included in:
- MetadataUtils
- Defined in:
- lib/bolognese/author_utils.rb
Constant Summary collapse
- IDENTIFIER_SCHEME_URIS =
include BenchmarkMethods
benchmark :get_authors
{ "ORCID" => "http://orcid.org/" }
Instance Method Summary collapse
- #authors_as_string(authors) ⇒ Object
- #cleanup_author(author) ⇒ Object
-
#get_authors(authors) ⇒ Object
parse array of author strings into CSL format.
-
#get_name_identifiers(author) ⇒ Object
parse nameIdentifier from DataCite.
- #get_one_author(author) ⇒ Object
- #is_personal_name?(author) ⇒ Boolean
-
#name_exists?(name) ⇒ Boolean
recognize given name if we have loaded ::NameDetector data, e.g.
Instance Method Details
#authors_as_string(authors) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/bolognese/author_utils.rb', line 137 def () Array.wrap().map do |a| if a["familyName"].present? [a["familyName"], a["givenName"]].join(", ") elsif a["type"] == "Person" a["name"] elsif a["name"].present? "{" + a["name"] + "}" end end.join(" and ").presence end |
#cleanup_author(author) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bolognese/author_utils.rb', line 72 def () return nil unless .present? # detect pattern "Smith J.", but not "Smith, John K." = .gsub(/[[:space:]]([A-Z]\.)?(-?[A-Z]\.)$/, ', \1\2') unless .include?(",") # remove spaces around hyphens = .gsub(" - ", "-") # titleize strings # remove non-standard space characters .my_titleize.gsub(/[[:space:]]/, ' ') end |
#get_authors(authors) ⇒ Object
parse array of author strings into CSL format
104 105 106 |
# File 'lib/bolognese/author_utils.rb', line 104 def () Array.wrap().map { || () }.unwrap end |
#get_name_identifiers(author) ⇒ Object
parse nameIdentifier from DataCite
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/bolognese/author_utils.rb', line 109 def get_name_identifiers() name_identifiers = Array.wrap(.fetch("nameIdentifier", nil)).reduce([]) do |sum, n| n = { "__content__" => n } if n.is_a?(String) scheme = n.fetch("nameIdentifierScheme", nil) scheme_uri = n.fetch("schemeURI", nil) || IDENTIFIER_SCHEME_URIS.fetch(scheme, "https://orcid.org") scheme_uri = "https://orcid.org/" if validate_orcid_scheme(scheme_uri) scheme_uri << '/' unless scheme_uri.present? && scheme_uri.end_with?('/') identifier = n.fetch("__content__", nil) if scheme_uri == "https://orcid.org/" identifier = validate_orcid(identifier) else identifier = identifier.gsub(" ", "-") end if identifier.present? && scheme_uri.present? sum << scheme_uri + identifier else sum end end # return array of name identifiers, ORCID ID is first element if multiple name_identifiers.select { |n| n.start_with?("https://orcid.org") } + name_identifiers.reject { |n| n.start_with?("https://orcid.org") } end |
#get_one_author(author) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/bolognese/author_utils.rb', line 15 def () if .fetch("creatorName", nil).is_a?(Array) # malformed XML return nil elsif .fetch("type", nil).present? type = .fetch("type").titleize elsif .fetch("creatorName", nil).is_a?(Hash) type = .dig("creatorName", "nameType") == "Organizational" ? "Organization" : "Person" else type = nil end name_identifiers = get_name_identifiers() id = .fetch("id", nil).presence || name_identifiers.first identifier = name_identifiers.length > 1 ? name_identifiers.unwrap : nil name = parse_attributes(.fetch("creatorName", nil)) || parse_attributes(.fetch("contributorName", nil)) || .fetch("name", nil) given_name = .fetch("givenName", nil) family_name = .fetch("familyName", nil) name = (name) name = [family_name, given_name].join(", ") if name.blank? && family_name.present? = { "type" => type || "Person", "id" => id, "name" => name, "givenName" => given_name, "familyName" => family_name, "identifier" => identifier }.compact return if family_name.present? if is_personal_name?() names = Namae.parse(name) parsed_name = names.first if parsed_name.present? given_name = parsed_name.given family_name = parsed_name.family name = [given_name, family_name].join(" ") else given_name = nil family_name = nil end { "type" => "Person", "id" => id, "name" => name, "givenName" => given_name, "familyName" => family_name, "identifier" => identifier }.compact else { "type" => type, "name" => name }.compact end end |
#is_personal_name?(author) ⇒ Boolean
86 87 88 89 90 91 92 93 94 |
# File 'lib/bolognese/author_utils.rb', line 86 def is_personal_name?() return false if .fetch("type", "").downcase == "organization" return true if .fetch("id", "").start_with?("https://orcid.org") || .fetch("familyName", "").present? || (.fetch("name", "").include?(",") && .fetch("name", "").exclude?(";")) || name_exists?(.fetch("name", "").split(" ").first) false end |
#name_exists?(name) ⇒ Boolean
recognize given name if we have loaded ::NameDetector data, e.g. in a Rails initializer
97 98 99 100 101 |
# File 'lib/bolognese/author_utils.rb', line 97 def name_exists?(name) return false unless name_detector.present? name_detector.name_exists?(name) end |