From 35b8bf4d9c812365d97769f7ed8e32fd3cb27dd8 Mon Sep 17 00:00:00 2001 From: Lachlan Kermode Date: Fri, 7 Dec 2018 13:58:38 +0000 Subject: [PATCH] add _buildBlueprintsAsnc in Fetcher --- src/lib/Fetcher.js | 47 +++++++++++++++++++++++------------------ src/lib/blueprinters.js | 9 ++++++-- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/lib/Fetcher.js b/src/lib/Fetcher.js index d7c3c6d..890ba97 100644 --- a/src/lib/Fetcher.js +++ b/src/lib/Fetcher.js @@ -1,5 +1,6 @@ // FetcherTwo class interfaces with Google Sheet, and saves to a specified db import { google } from 'googleapis' +import { buildDesaturated } from './blueprinters' import { fmtName, fmtBlueprinterTitles, @@ -46,18 +47,8 @@ class Fetcher { * data structure updates as well. It is the model layer that determines the * performance of indexing the blueprints. */ - this.blueprints = this._indexDbForBlueprints() - .then(allUrls => { - const allParts = allUrls.reduce((acc, url) => { - if (url.startsWith(this.id)) { - const parts = url.split('/') - acc.push([ parts[1], parts[2] ]) - return acc - } - }, []) - console.log(allParts) - return {} - }) + this.blueprints = null + this._buildBlueprintsAsync() // NB: modifies this.blueprints on completion /* * Google API setup @@ -69,6 +60,29 @@ class Fetcher { this._saveViaBlueprinter = R.curry(this._saveViaBlueprinter) } + _buildBlueprintsAsync () { + return this.db.index() + .then(allUrls => { + const allParts = allUrls.reduce((acc, url) => { + if (url.startsWith(this.id)) { + const parts = url.split('/') + acc.push([ parts[1], parts[2] ]) + return acc + } + }, []) + return allParts + .map(parts => buildDesaturated( + this.sheetId, + this.sheetName, + parts[0], + parts[1] + )) + }) + .then(res => { + this.blueprints = res + }) + } + /** save data under a given tab name via its blueprinter, which generates * its resource name. Note that this is curried in the constructor. */ @@ -87,14 +101,6 @@ class Fetcher { ) } - /** index the db and produce appropriate blueprints structure **/ - _indexDbForBlueprints () { - return this.db.index() - .then(res => { - return res - }) - } - /** returns a Promise that resolves if access is granted to the account, and rejects otherwise. */ authenticate (clientEmail, privateKey) { const googleAuth = new google.auth.JWT(clientEmail, null, privateKey, [ @@ -155,6 +161,7 @@ class Fetcher { if (Object.keys(this.blueprinters).indexOf(tab) > -1) { const bpConfig = this.blueprinters[tab] + if (isFunction(bpConfig)) { // if bpConfig specifies a single blueprinter return this._saveViaBlueprinter(tab, data, bpConfig) diff --git a/src/lib/blueprinters.js b/src/lib/blueprinters.js index 1d9d03a..62b6fe9 100644 --- a/src/lib/blueprinters.js +++ b/src/lib/blueprinters.js @@ -16,8 +16,12 @@ export const defaultResource = { data: [] } -export function buildDesaturated (tab, resource) { +export function buildDesaturated (sheetId, sheetName, tab, resource) { const bp = R.clone(defaultBlueprint) + bp.name = sheetName + bp.id = sheetId + bp.resources[tab] = resource + return bp } // import all default exports from 'blueprinters' folder @@ -33,5 +37,6 @@ fs.readdirSync(normalizedPath).forEach(file => { // each file in blueprinters folder available for granular import from here. module.exports = Object.assign({ defaultBlueprint, - defaultResource + defaultResource, + buildDesaturated }, allBps)