-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added URL resolver and source handler for figshare sources (Issue #20)
- Loading branch information
Showing
4 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
const request = require('request'); | ||
const SourceData = require('../model/sourceData'); | ||
const SourceHandler = require('./sourceHandler'); | ||
|
||
const userAgent = 'SoftwareCitationCore'; | ||
|
||
/** | ||
* URL Handler for figshareAPI | ||
* @class FigshareAPIHandler | ||
* @memberof sourceHandlers | ||
* @augments sourceHandlers.SourceHandler | ||
* @property {String} articleID - The ID of the figshare article. | ||
*/ | ||
class FigshareAPIHandler extends SourceHandler { | ||
/** | ||
* Creates a FigshareAPIHandler. | ||
* @param baseUrl {String} - The Base URL to the API. | ||
* @param articleID {String} - The article ID." | ||
*/ | ||
constructor(baseUrl, articleID) { | ||
super(baseUrl); | ||
this.articleID = articleID; | ||
} | ||
|
||
/** | ||
* Creates a SourceData author object from a figshare author object | ||
* @param author {Object} - the figshare author object to be converted | ||
*/ | ||
_createAuthorObject(author) { | ||
const namePieces = (author.full_name == '') ? [] : author.full_name.split(' '); | ||
const result = {}; | ||
|
||
result.email = null; | ||
result.lastName = (namePieces.length > 0) ? namePieces.pop() : null; | ||
result.middleName = (namePieces.length > 1) ? namePieces.splice(1 - namePieces.length).join(' ') : null; | ||
result.firstName = (namePieces.length > 0) ? namePieces.pop() : null; | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Fetches data to be used in the citation from the API, stores it in a SourceData object, and returns that object to | ||
* a callback function | ||
* @param callback {Function} - the callback function. Consumes a SourceData object and an Error Object | ||
*/ | ||
fetch(callback) { | ||
|
||
const options = { | ||
url: this.url + 'articles/' + this.articleID, | ||
headers: { | ||
'User-Agent': userAgent, | ||
}, | ||
}; | ||
|
||
request(options, (error, response, body) => { | ||
const parsedBody = (body != null) ? JSON.parse(body) : null; | ||
|
||
if (error == null && response.statusCode !== 200) { | ||
error = new Error('Received a ' + response.statusCode + ' when making a request to ' + options.url); | ||
} | ||
|
||
|
||
if (error == null) { | ||
const sourceData = new SourceData(); | ||
|
||
sourceData.name = parsedBody.figshare_url.replace(/\/\d+$/,'').replace(/^https?:\/\/(www\.)?figshare\.com\/articles\//, '').replace(/_/g, ' ');; | ||
sourceData.version = parsedBody.version.toString(); | ||
sourceData.releaseDate = new Date(parsedBody.modified_date); | ||
sourceData.url = parsedBody.url_public_html; | ||
sourceData.licence = parsedBody.license.name; | ||
sourceData.description = parsedBody.description; | ||
sourceData.uid = parsedBody.doi; | ||
|
||
sourceData.authors = []; | ||
parsedBody.authors.forEach((author) => { | ||
sourceData.authors.push(this._createAuthorObject(author)); | ||
}); | ||
|
||
callback(sourceData, []); | ||
} | ||
else { | ||
callback(null, [error]); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = FigshareAPIHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const URLResolver = require('./urlResolver'); | ||
const FigshareAPIHandler = require('../sourceHandlers/figshare'); | ||
|
||
const stripHttp = /^(https?:\/\/)?(www\.)?/; | ||
const urlRegex = /^figshare\.com\/articles(\/\w+)?\/\d+$/; | ||
|
||
const apiBaseUrl = 'https://api.figshare.com/v2/'; | ||
|
||
/** | ||
* Creates the supported source handlers for a given URL. | ||
* @class | ||
* @memberof urlResolvers | ||
* @augments urlResolvers.URLResolver | ||
*/ | ||
class FigshareResolver extends URLResolver { | ||
static getSourceHandlers(url) { | ||
const sourceHandlers = []; | ||
const strippedURL = url.replace(stripHttp, ''); | ||
|
||
if (urlRegex.exec(strippedURL)) { | ||
sourceHandlers.push(new FigshareAPIHandler(apiBaseUrl, /\d+$/.exec(strippedURL))); | ||
} | ||
|
||
return sourceHandlers; | ||
} | ||
} | ||
|
||
module.exports = FigshareResolver; |