rename source to sheet

This commit is contained in:
Lachlan Kermode
2018-12-06 16:40:19 +00:00
parent fb6af6daff
commit e6c83762b6
11 changed files with 84 additions and 85 deletions

View File

@@ -9,20 +9,20 @@ class Controller {
this.fetchers = fetchers
}
sourceExists (source) {
return (Object.keys(this.fetchers).indexOf(source) >= 0)
sheetExists (sheet) {
return (Object.keys(this.fetchers).indexOf(sheet) >= 0)
}
blueprints () {
return Object.keys(this.fetchers).map(
source => this.fetchers[source].blueprints
sheet => this.fetchers[sheet].blueprints
)
}
update () {
return Promise.all(
Object.keys(this.fetchers).map(source => {
return this.fetchers[source].update()
Object.keys(this.fetchers).map(sheet => {
return this.fetchers[sheet].update()
})
).then(results => {
if (results.every(r => r)) {
@@ -33,21 +33,21 @@ class Controller {
})
}
retrieve (source, tab, resource) {
if (this.sourceExists(source)) {
const fetcher = this.fetchers[source]
retrieve (sheet, tab, resource) {
if (this.sheetExists(sheet)) {
const fetcher = this.fetchers[sheet]
return fetcher.retrieve(tab, resource)
} else {
return Promise.reject(new Error(copy.errors.noResource(source)))
return Promise.reject(new Error(copy.errors.noResource(sheet)))
}
}
retrieveFrag (source, tab, resource, frag) {
if (this.sourceExists(source)) {
const fetcher = this.fetchers[source]
retrieveFrag (sheet, tab, resource, frag) {
if (this.sheetExists(sheet)) {
const fetcher = this.fetchers[sheet]
return fetcher.retrieveFrag(tab, resource, frag)
} else {
return Promise.reject(new Error(copy.errors.noResource(source)))
return Promise.reject(new Error(copy.errors.noResource(sheet)))
}
}
}

View File

@@ -1,7 +1,7 @@
// FetcherTwo class interfaces with Google Sheet, and saves to a specified db
import { google } from 'googleapis'
import {
fmtSourceTitle,
fmtSheetTitle,
fmtBlueprinterTitles,
bp,
isFunction
@@ -10,7 +10,7 @@ import { byRow } from './blueprinters'
import R from 'ramda'
class Fetcher {
constructor (db, sourceName, sourceId, blueprinters) {
constructor (db, sheetName, sheetId, blueprinters) {
/*
* The database that the fetcher should use. This should be an instance of a model-compliant class.
* See models/Interface.js for the specifications for a model-compliant class.
@@ -18,15 +18,15 @@ class Fetcher {
this.db = db
/*
* ID of the Google Sheet where the data is sourced. Note that the privateKey.clientEmail
* ID of the Google Sheet where the data is sheetd. Note that the privateKey.clientEmail
* loaded here must be added to the sheet as an editor.
*/
this.sourceId = sourceId
this.sheetId = sheetId
/*
* The name of the source. This will prefix tabs saved in the database.
* The name of the sheet. This will prefix tabs saved in the database.
*/
this.sourceName = sourceName
this.sheetName = sheetName
/*
* These are the available tabs for storing and retrieving data.
@@ -53,8 +53,8 @@ class Fetcher {
this._saveBp = R.curry((tab, title, data, blueprinter) => {
const saturatedBp = blueprinter(
tab,
this.sourceName,
this.sourceId,
this.sheetName,
this.sheetId,
data
)
const blueprint = bp(saturatedBp) // TODO: come up with better semantics.
@@ -69,14 +69,14 @@ class Fetcher {
'https://www.googleapis.com/auth/spreadsheets'
])
this.auth = googleAuth
const { sourceId } = this
const { sheetId } = this
return new Promise((resolve, reject) => {
googleAuth.authorize(function (err) {
if (err) {
reject(err)
} else {
resolve(`Connected to ${sourceId}.`)
resolve(`Connected to ${sheetId}.`)
}
})
})
@@ -88,13 +88,13 @@ class Fetcher {
return this.sheets.spreadsheets
.get({
auth: this.auth,
spreadsheetId: this.sourceId
spreadsheetId: this.sheetId
})
.then(response => {
tabTitles = response.data.sheets.map(sheet => sheet.properties.title)
return this.sheets.spreadsheets.values.batchGet({
auth: this.auth,
spreadsheetId: this.sourceId,
spreadsheetId: this.sheetId,
ranges: tabTitles
})
})
@@ -116,7 +116,7 @@ class Fetcher {
}
save (tab, data) {
const title = fmtSourceTitle(tab)
const title = fmtSheetTitle(tab)
if (Object.keys(this.blueprinters).indexOf(title) > -1) {
const bpConfig = this.blueprinters[title]
@@ -127,20 +127,20 @@ class Fetcher {
}
} else {
// If it can't find a blueprinter for the tab title, default to byRow
return this.db.save(byRow(tab, this.sourceName, this.sourceId, data))
return this.db.save(byRow(tab, this.sheetName, this.sheetId, data))
}
}
// NB: could combine these functions by checking kwargs length
retrieve (tab, resource) {
const title = fmtSourceTitle(tab)
const url = `${this.sourceName}/${tab}/${resource}`
const title = fmtSheetTitle(tab)
const url = `${this.sheetName}/${tab}/${resource}`
return this.db.load(url, this.blueprints[title])
}
retrieveFrag (tab, resource, frag) {
const title = fmtSourceTitle(tab)
const url = `${this.sourceName}/${tab}/${resource}/${frag}`
const title = fmtSheetTitle(tab)
const url = `${this.sheetName}/${tab}/${resource}/${frag}`
return this.db.load(url, this.blueprints[title])
}
}

View File

@@ -55,27 +55,27 @@ export const idxSearcher = R.curry((attrName, searchValue, myArray) => {
/* more site specific functions. TODO: maybe move to another folder? */
export function fmtSourceTitle (name) {
export function fmtSheetTitle (name) {
return name.replaceAll(' ', '-').toLowerCase()
}
export function fmtBlueprinterTitles (tabs) {
const obj = {}
Object.keys(tabs).forEach(tab => {
const name = fmtSourceTitle(tab)
const name = fmtSheetTitle(tab)
obj[name] = tabs[tab]
})
return obj
}
export function deriveFilename (source, tab) {
return `${fmtSourceTitle(source)}-${fmtSourceTitle(tab)}.json`
export function deriveFilename (sheet, tab) {
return `${fmtSheetTitle(sheet)}-${fmtSheetTitle(tab)}.json`
}
export function bp (full) {
const blueprint = {
name: R.clone(full.name),
source: R.clone(full.source),
sheet: R.clone(full.sheet),
dialects: R.clone(full.dialects),
routes: {}
}