ghcrawler/lib/request.js

132 строки
3.8 KiB
JavaScript
Исходник Обычный вид История

2016-11-11 10:59:42 +03:00
const extend = require('extend');
class Request {
2016-11-12 03:46:31 +03:00
constructor(type, url) {
this.type = type;
this.url = url;
2016-11-11 10:59:42 +03:00
}
addMeta(data) {
this.meta = extend({}, this.meta, data);
return this;
}
2016-11-12 11:13:04 +03:00
addRootSelfLink() {
this.addSelfLink('id', 'urn:');
}
addSelfLink(key = 'id', base = null) {
2016-11-11 10:59:42 +03:00
let qualifier = base ? base : this.context.qualifier;
2016-11-12 11:13:04 +03:00
if (!qualifier || (typeof qualifier !== 'string' )) {
console.log('bummer');
}
2016-11-11 10:59:42 +03:00
qualifier = qualifier.endsWith(':') ? qualifier : qualifier + ':';
this.linkSelf('self', `${qualifier}${this.type}:${this.document[key]}`);
}
linkSelf(name, value) {
const links = this.document._metadata.links;
const key = Array.isArray(value) ? 'hrefs' : 'href';
links[name] = { type: 'self' };
links[name][key] = value;
}
linkSiblings(name, href) {
const links = this.document._metadata.links;
links[name] = { type: 'siblings', href: href };
}
queue(type, url, context, queue = null) {
2016-11-12 03:46:31 +03:00
const newRequest = new Request(type, url);
2016-11-11 10:59:42 +03:00
newRequest.context = context;
2016-11-11 21:55:30 +03:00
this.crawler.queue(this, newRequest, queue);
2016-11-11 10:59:42 +03:00
}
2016-11-12 11:13:04 +03:00
queueRoot(type, url, force = false) {
const newRequest = new Request(type, url);
newRequest.context = { qualifier: 'urn:' };
newRequest.force = force;
this.crawler.queue(this, newRequest);
}
queueRoots(type, url, force = false) {
const newRequest = new Request(type, url);
const newContext = {};
newContext.qualifier = this.document._metadata.links.self.href;
newRequest.context = newContext;
newRequest.force = force;
this.crawler.queue(this, newRequest);
2016-11-11 10:59:42 +03:00
}
queueChild(type, url, qualifier) {
2016-11-12 03:46:31 +03:00
const newRequest = new Request(type, url);
2016-11-11 10:59:42 +03:00
newRequest.context = this.context || {};
newRequest.context.qualifier = qualifier;
2016-11-12 11:13:04 +03:00
newRequest.force = this.force;
2016-11-11 21:55:30 +03:00
this.crawler.queue(this, newRequest);
2016-11-11 10:59:42 +03:00
}
queueChildren(type, url, context = null) {
2016-11-12 03:46:31 +03:00
const newRequest = new Request(type, url);
2016-11-11 10:59:42 +03:00
const newContext = extend(this.context || {}, context);
newContext.qualifier = this.document._metadata.links.self.href;
2016-11-12 11:13:04 +03:00
newRequest.context = newContext;
newRequest.force = this.force;
2016-11-11 21:55:30 +03:00
this.crawler.queue(this, newRequest);
2016-11-11 10:59:42 +03:00
}
markSkip(outcome, message) {
2016-11-11 21:55:30 +03:00
this.processControl = 'skip';
2016-11-11 10:59:42 +03:00
this.outcome = this.outcome || outcome;
this.message = this.message || message;
return this;
}
2016-11-12 01:06:10 +03:00
markRequeue(outcome, message) {
2016-11-11 21:55:30 +03:00
this.processControl = 'requeue';
2016-11-12 01:06:10 +03:00
this.outcome = this.outcome || outcome;
2016-11-11 21:55:30 +03:00
this.message = this.message || message;
return this;
}
shouldSkip() {
2016-11-12 01:06:10 +03:00
return this.processControl === 'skip' || this.processControl === 'requeue';
}
markDelay() {
this.flowControl = 'delay';
return this;
}
shouldDelay() {
return this.flowControl === 'delay';
2016-11-11 21:55:30 +03:00
}
shouldRequeue() {
return this.processControl === 'requeue';
}
2016-11-11 10:59:42 +03:00
eventHelper(references) {
const document = this.document;
// TODO understand if the actor is typically the same as the creator or pusher in the payload
const repo = document.repo ? document.repo.id : null;
const urn = repo ? `urn:repo:${repo}` : `urn:org:${document.org.id}`;
this.linkSelf('self', `${urn}:${this.type}:${document.id}`);
this.linkSelf('actor', `urn:login:${document.actor.id}`);
this.linkSelf('repo', `urn:repo:${document.repo.id}`);
this.linkSelf('org', `urn:org:${document.org.id}`);
this.queueRoot('login', document.actor.url);
this.queueRoot('repo', document.repo.url);
this.queueRoot('org', document.org.url);
return document.payload;
}
getCollectionType() {
const collections = {
orgs: 'org', repos: 'repo', issues: 'issue', issue_comments: 'issue_comment', commits: 'commit', teams: 'team', users: 'user'
};
return collections[this.type];
}
}
module.exports = Request;