Skip to content

Commit

Permalink
Added URL resolver and source handler for figshare sources (Issue #20)
Browse files Browse the repository at this point in the history
  • Loading branch information
faokryn committed May 11, 2017
1 parent 93eed24 commit e390cf5
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
88 changes: 88 additions & 0 deletions sourceHandlers/figshare.js
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;
6 changes: 6 additions & 0 deletions test/urlResolverManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global describe it */

const assert = require('assert');
const FigshareHandler = require('../sourceHandlers/figshare');
const GitHubHandler = require('../sourceHandlers/gitHub');
const UrlResolverManager = require('../urlResolverManager');

Expand All @@ -11,6 +12,11 @@ describe('UrlResolverManager', () => {
assert(handler.constructor === GitHubHandler);
});

it('returns an instance of FigshareHandler when given a figshare URL', () => {
const handler = UrlResolverManager.getHandler('https://figshare.com/articles/4426703');
assert(handler.constructor === FigshareHandler);
});

it('returns null for unknown URL', () => {
const handler = UrlResolverManager.getHandler('garbage url');
assert(handler === null);
Expand Down
3 changes: 2 additions & 1 deletion urlResolverManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const FigshareAPI = require('./urlResolvers/figshare');
const GithubAPI = require('./urlResolvers/gitHub');

const resolvers = [GithubAPI];
const resolvers = [FigshareAPI, GithubAPI];

/**
* A module for resolving a URL to a source handler.
Expand Down
28 changes: 28 additions & 0 deletions urlResolvers/figshare.js
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;

0 comments on commit e390cf5

Please sign in to comment.