AssetLoader

Load CSV/JSON data from the web or from cache.

(defined in /src/AssetLoader.coffee:9)
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}")

Parameters

(None)

Parameters

NameTypeDescription
uriString
optsObject
datatypeString Data Type of the asset, 'csv' or 'json'
cbFunction()
(defined in /src/AssetLoader.coffee:38)
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