add _buildBlueprintsAsnc in Fetcher

This commit is contained in:
Lachlan Kermode
2018-12-07 13:58:38 +00:00
parent 41e2ba8299
commit 35b8bf4d9c
2 changed files with 34 additions and 22 deletions

View File

@@ -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)

View File

@@ -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)