Skip to content

Commit

Permalink
fetch institution metadata via ROR ID
Browse files Browse the repository at this point in the history
  • Loading branch information
whomingbird committed Dec 13, 2024
1 parent 6166a48 commit 8783b19
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
31 changes: 29 additions & 2 deletions app/assets/javascripts/ror/institution-typeahead.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var ROR_API_URL = "https://api.ror.org/organizations?query=";
var ROR_API_URL = "https://api.ror.org/organizations";

function extractRorId(rorUrl) {
// Define the regular expression to match the ROR ID
Expand All @@ -15,9 +15,36 @@ function extractRorId(rorUrl) {
}
}


function fetchRorData(rorId) {
var url = ROR_API_URL+'/' + rorId;
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`Error fetching ror data: ${response.statusText}`);
}
return response.json();
})
.then(data => {
$j('#ror-id-01').html(JSON.stringify(data, undefined, 4));
$j('#institution_title').val(data.name);
$j('#institution_city').val(data.addresses[0]['city']);
$j('#institution_country').val(data.country.country_name);
$j('#institution_ror_id').val(extractRorId(data.id));
$j('#institution_web_page').val(data.links?.[0] || 'N/A');
})
.catch(error => {
console.error('Error:', error.message);
});
}

$j(document).ready(function() {
var $j = jQuery.noConflict();

$j('#fetch-ror-data-with-id').on('click', function() {
fetchRorData($j('#institution_ror_id').val());
});

$j('#ror_query_name .typeahead').typeahead({
hint: true,
highlight: true,
Expand All @@ -27,7 +54,7 @@ $j(document).ready(function() {
limit: 50,
async: true,
source: function (query, processSync, processAsync) {
var url = ROR_API_URL + encodeURIComponent(query);
var url = ROR_API_URL+'?query=' + encodeURIComponent(query);
return $j.ajax({
url: url,
type: 'GET',
Expand Down
1 change: 1 addition & 0 deletions app/views/institutions/_tab_query_name.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<div class="form-group">
<%= f.label :ror_id, 'ROR ID' -%>
<button type="button" id="fetch-ror-data-with-id" class="btn btn-sm btn-primary">fetch</button>
<%= f.text_field :ror_id, :class=>"form-control" -%>
<small class="form-text text-muted">
<em>The ROR ID (Research Organization Registry Identifier) is a unique 9-character alphanumeric code (e.g., 01f7bcy98).
Expand Down

0 comments on commit 8783b19

Please sign in to comment.