-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOrcid.js
165 lines (147 loc) · 4.84 KB
/
Orcid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
*
* Orcid file includes all functions to work with ORCID API
*
*/
/*
* Content of the orcidSearchUrl.js
* Placed here because we cannot include file directly with <script> tag
* This library forms correct search API URL
*/
(function(exports) {
var baseUrl = 'https://orcid.org/v1.2/search/orcid-bio/';
var quickSearchEDisMax = '{!edismax qf="given-and-family-names^50.0 family-name^10.0 given-names^5.0 credit-name^10.0 other-names^5.0 text^1.0" pf="given-and-family-names^50.0" mm=1}';
var orcidPathRegex = new RegExp("(\\d{4}-){3,}\\d{3}[\\dX]");
var orcidFullRegex = new RegExp(
"^\\s*((http://)?([^/]*orcid\\.org|localhost.*/orcid-web)/)?(\\d{4}-){3,}\\d{3}[\\dX]\\s*$");
function offset(input) {
var start = hasValue(input.start) ? input.start : 0;
var rows = hasValue(input.rows) ? input.rows : 10;
return '&start=' + start + '&rows=' + rows;
}
function hasValue(ref) {
return typeof ref !== 'undefined' && ref !== null && ref !== '';
}
function buildAdvancedSearchUrl(input) {
var query = '';
var doneSomething = false;
if (hasValue(input.givenNames)) {
query += 'given-names:' + input.givenNames.toLowerCase();
doneSomething = true;
}
if (hasValue(input.familyName)) {
if (doneSomething) {
query += ' AND ';
}
query += 'family-name:' + input.familyName.toLowerCase();
doneSomething = true;
}
if (hasValue(input.searchOtherNames) && hasValue(input.givenNames)) {
query += ' OR other-names:' + input.givenNames.toLowerCase();
}
if (hasValue(input.keyword)) {
if (doneSomething) {
query += ' AND ';
}
query += 'keyword:' + input.keyword.toLowerCase();
doneSomething = true;
}
return doneSomething ? baseUrl + '?q=' + encodeURIComponent(query) + offset(input) : null;
}
exports.setBaseUrl = function(url) {
baseUrl = url;
};
exports.isValidInput = function(input) {
var fieldsToCheck = [ input.text, input.givenNames, input.familyName, input.keyword ];
for ( var i = 0; i < fieldsToCheck.length; i++) {
if (hasValue(fieldsToCheck[i])) {
return true;
}
}
return false;
};
function extractOrcidId(string) {
var regexResult = orcidPathRegex.exec(string);
if (regexResult) {
return regexResult[0];
}
return null;
}
exports.buildUrl = function(input) {
if (hasValue(input.text)) {
var orcidId = extractOrcidId(input.text);
if (orcidId) {
// Search for iD specifically
return baseUrl + "?q=orcid:" + orcidId + offset(input);
}
// General quick search
return baseUrl + '?q=' + encodeURIComponent(quickSearchEDisMax + input.text) + offset(input);
} else {
// Advanced search
return buildAdvancedSearchUrl(input);
}
};
exports.isValidOrcidId = function(orcidId) {
if (orcidFullRegex.exec(orcidId)) {
return true;
}
return false;
};
})(typeof exports === 'undefined' ? this.orcidSearchUrlJs = {} : exports);
/***** END OF LIBRARY *****/
/*
* Request persons from ORCID
* @param {string} name Person name to search
*/
function searchPerson(name) {
var options = {
headers : {
"Content-Type": "application/orcid+json",
'Accept': 'application/orcid+json'
}
}
var url = orcidSearchUrlJs.buildUrl({ text: name});
// logger.log(url);
var response = UrlFetchApp.fetch(url, options);
response = JSON.parse(response);
// logger.log(response);
return processPersons(response);
}
/*
* Process persons from original JSON format to simplified objects
* with only attributes we need
* @param {JSON} personJson JSON received from Orcid API
*/
function processPersons(personsJson) {
//// logger.log(personsJson);
var persons = personsJson["orcid-search-results"]["orcid-search-result"];
var processedPersons = [];
for (var i = 0; i < persons.length; i++) {
var person = persons[i];
var orcid_url = person["orcid-profile"]["orcid-identifier"]["uri"];
var orcid_name = getOrcidGivenName(person) +
" " + getOrcidFamilyName(person);
var orcid_id = person["orcid-profile"]["orcid-identifier"]["path"];
processedPersons.push({url: orcid_url, id: orcid_id, name: orcid_name, type: 'person', desc: 'ORCID_ID: ' + orcid_id });
}
return processedPersons;
}
/*
* Getting ORCID given name for a person if presented
*/
function getOrcidGivenName(person) {
var name = 'Noname';
if(person["orcid-profile"]["orcid-bio"]["personal-details"] && person["orcid-profile"]["orcid-bio"]["personal-details"]["given-names"])
name = person["orcid-profile"]["orcid-bio"]["personal-details"]["given-names"]["value"];
return name;
}
/*
* Getting ORCID family name for a person if presented
*/
function getOrcidFamilyName(person) {
// logger.log(person);
var name = 'Noname';
if(person["orcid-profile"]["orcid-bio"]["personal-details"] && person["orcid-profile"]["orcid-bio"]["personal-details"]["family-name"])
name = person["orcid-profile"]["orcid-bio"]["personal-details"]["family-name"]["value"];
return name;
}