Load CSV/JSON data from the web or from cache.
class AssetLoader
constructor: (@config) ->
@config or= {}
@cache = new UriCache(@config.cacheDir or '/tmp/ocrrect.cache')
initialize : (cb) ->
@cache.initialize cb
_parseData : (data, dataType, cb) ->
if dataType.match /json/
try
res = JSON.parse data
catch err
return cb err
return cb null, res
else if dataType.match /csv/
CsvParse data, cb
else
cb new Error("Unknown dataType #{dataType}")| (None) |
Load data from uri
| Name | Type | Description |
|---|---|---|
uri | String | |
opts | Object | |
datatype | String | Data Type of the asset, 'csv' or 'json' |
cb | Function() |
load : (uri, opts, cb) ->
return cb new Error("Must specify dataType") if not opts.dataType
@cache.get uri, (err, data) =>
return @_parseData data, dataType, cb unless err
Request.get(uri).end (err, res) =>
@cache.put uri, res.text, (err) =>
return @_parseData res.text, dataType, cb unless err
cb err