website-scraper/node-website-scraper

View on GitHub
lib/resource.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import types from './config/resource-types.js';

class Resource {
    constructor (url, filename) {
        this.url = url;
        this.filename = filename;

        this.type = null;
        this.depth = 0;

        this.parent = null;
        this.children = [];

        this.saved = false;
        this.encoding = 'binary';
    }

    createChild (url, filename) {
        const child = new Resource(url, filename);
        let currentDepth = this.getDepth();

        child.parent = this;
        child.depth = ++currentDepth;

        this.children.push(child);

        return child;
    }

    updateChild (oldChild, newChild) {
        const index = this.children.indexOf(oldChild);
        if (index >= 0) {
            this.children[index] = newChild;
        }
    }

    getUrl () {
        return this.url;
    }

    setUrl (url) {
        this.url = url;
    }

    getFilename () {
        return this.filename;
    }

    setFilename (filename) {
        this.filename = filename;
    }

    getText () {
        return this.text;
    }

    setText (text) {
        this.text = text;
    }

    getDepth () {
        return this.depth;
    }

    setType (type) {
        this.type = type;
    }

    getType () {
        return this.type;
    }

    setEncoding (encoding) {
        this.encoding = encoding;
    }

    getEncoding () {
        return this.encoding;
    }

    isHtml () {
        return this.getType() === types.html;
    }

    isCss () {
        return this.getType() === types.css;
    }

    toString () {
        return `{ url: "${this.getUrl()}", filename: "${this.getFilename()}", depth: ${this.getDepth()}, type: "${this.getType()}" }`;
    }

    isSaved () {
        return this.saved;
    }

    setSaved () {
        this.saved = true;
    }

    setMetadata (metadata) {
        this.metadata = metadata;
    }
}

export default Resource;