Skip to content

Commit a00285d

Browse files
committed
[load][m]: rename load to loadUrl and have new load function to load from disk.
1 parent d713c69 commit a00285d

File tree

5 files changed

+99
-22
lines changed

5 files changed

+99
-22
lines changed

README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,46 @@ The following assume you've required datapackage as follows:
1818
var datapackage = require('datapackage-read');
1919
```
2020

21-
## Loading and normalizing a datapackage.json
21+
## load
2222

2323
```
24-
datapackage.load(urlToDataPackage, callback)
24+
datapackage.load(path, callback)
2525
```
2626

27-
Load a datapackage.json from a URL and normalize it as per the normalize function.
27+
* `path` {String} The path to the datapackage.json file
2828

29-
`urlToDataPackage` can be either a url to actual datapackage.json or to a base directory.
29+
Load a datapackage.json from a file and normalize it as per the normalize
30+
function. Will search for README at REAME.md in same directory as
31+
datapackage.json.
32+
33+
## loadUrl
34+
35+
```
36+
datapackage.loadUrl(urlToDataPackage, callback)
37+
```
38+
39+
* `urlToDataPackage` {String} url to datapackage.json or directory in which
40+
datapackage.json contained (also handle provision of github urls e.g.
41+
https://github.com/datasets/gold-prices)
42+
43+
Load a datapackage.json from a URL, related files (e.g. README.md) and
44+
normalize it
45+
46+
## normalize
47+
48+
```
49+
datapackage.normalize(datapackage, url)
50+
```
51+
52+
* `datapackage`: datapackage.json object (already parsed from JSON)
53+
* `url`: [optional] url to datapackage.json or the root directory in which it
54+
is contained online. Used to help auto-generate things homepage
55+
56+
Normalize a DataPackage DataPackage.json in various ways, for example:
57+
58+
* Setting a description if missing (from readme)
59+
* Setting a url for resources
60+
* Guessing a homepage
3061

3162
# Tests
3263

datapackage-read.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
var fs = require('fs')
22
, url = require('url')
3+
, path = require('path')
34
, marked = require('marked')
45
, request = require('request')
56
;
67

7-
// Load a datapackage.json from a URL, related files (e.g. README.md) and normalize it
8-
//
9-
// Normalize is as per `normalize`
10-
exports.load = function(datapackage_url, cb) {
8+
exports.load = function(path_, cb) {
9+
// TODO: support just passing a directory
10+
var dpjsonPath = path_;
11+
var base = path.dirname(dpjsonPath);
12+
fs.readFile(dpjsonPath, function(error, body) {
13+
if (error) {
14+
cb(error);
15+
return;
16+
}
17+
try {
18+
var datapackage = JSON.parse(body);
19+
} catch(e) {
20+
cb({message: 'datapackage.json is invalid JSON. Details: ' + e.message});
21+
return;
22+
}
23+
24+
// now dig up and use README if it exists
25+
var readmePath = path.join(base, 'README.md');
26+
fs.readFile(readmePath, 'utf8', function(err, body) {
27+
if (!err) {
28+
datapackage['readme'] = body.replace(/\r\n/g, '\n');
29+
}
30+
datapackage = exports.normalize(datapackage, base);
31+
cb(null, datapackage);
32+
});
33+
});
34+
}
35+
36+
exports.loadUrl = function(datapackage_url, cb) {
1137
var datapackage_url = exports.normalizeDataPackageUrl(datapackage_url);
1238
var base = datapackage_url.replace(/datapackage.json$/g, '');
1339
request(datapackage_url, function(error, response, body) {
@@ -44,7 +70,7 @@ exports.load = function(datapackage_url, cb) {
4470
// hash (keyed by Data Package names)
4571
//
4672
// @return: via the callback
47-
exports.loadMany = function(urls, callback) {
73+
exports.loadManyUrls = function(urls, callback) {
4874
var output = {}
4975
, count = urls.length
5076
;
@@ -55,7 +81,7 @@ exports.loadMany = function(urls, callback) {
5581
}
5682
}
5783
urls.forEach(function(url) {
58-
exports.load(url, function(err, dpjson) {
84+
exports.loadUrl(url, function(err, dpjson) {
5985
if (err) {
6086
console.error(url, err)
6187
} else {
@@ -78,10 +104,6 @@ exports.normalizeDataPackageUrl = function(url) {
78104
return url;
79105
};
80106

81-
// Normalize a DataPackage DataPackage.json in various ways
82-
//
83-
// @param datapackage: datapackage object (already parsed from JSON)
84-
// @param url: [optional] url to datapackage.json or the root directory in which it is contained
85107
exports.normalize = function(datapackage, url_) {
86108
var base = url_ ? url_.replace(/datapackage.json$/g, '') : '';
87109
// ensure certain fields exist
@@ -98,13 +120,17 @@ exports.normalize = function(datapackage, url_) {
98120
datapackage.readme = datapackage.description;
99121
}
100122

101-
datapackage.readme_html = marked(datapackage.readme);
123+
datapackage.readmeHtml = marked(datapackage.readme);
124+
125+
if (!datapackage.resources) {
126+
datapackage.resources = [];
127+
}
102128

103129
datapackage.resources.forEach(function(info) {
104-
if (!info.url && info.path) {
130+
if (!info.url && info.path && base) {
105131
info.url = base + info.path;
106132
}
107-
if (!info.name) {
133+
if (!info.name && info.url) {
108134
info.name = _nameFromUrl(info.url);
109135
}
110136
// upgrade for change in JTS spec - https://github.com/dataprotocols/dataprotocols/issues/60

test/all.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,23 @@ describe('normalize', function() {
5454

5555
describe('load', function() {
5656
it('works in basic case', function(done) {
57-
tools.load(sourceUrl, function(err, dpout) {
57+
tools.load('test/data/dp1/datapackage.json', function(error, data) {
58+
assert(!error)
59+
assert.equal(data.readme, 'The README.\n');
60+
done();
61+
});
62+
});
63+
it('reports error with bad path', function(done) {
64+
tools.load('test/data/dp1/does-not-exist.json', function(error, data) {
65+
assert(error)
66+
done();
67+
});
68+
});
69+
});
70+
71+
describe('loadUrl', function() {
72+
it('works in basic case', function(done) {
73+
tools.loadUrl(sourceUrl, function(err, dpout) {
5874
assert(err === null);
5975
assert.equal(dpout.resources[0].url, sourceUrlBase + 'data/data.csv');
6076
assert(dpout.readme.length > 50);
@@ -66,15 +82,15 @@ describe('load', function() {
6682
it('works with 404', function(done) {
6783
this.timeout(4000);
6884
var badUrl = 'https://raw.github.com/datasets/gold-prices/master/xyz.txt';
69-
tools.load(badUrl, function(err, dpout) {
85+
tools.loadUrl(badUrl, function(err, dpout) {
7086
assert(err!=null);
7187
done();
7288
});
7389
});
7490

7591
it('works with bad data', function(done) {
7692
var csvurl = 'https://raw.github.com/datasets/gold-prices/master/README.md';
77-
dpout = tools.load(csvurl, function(err, dpout) {
93+
dpout = tools.loadUrl(csvurl, function(err, dpout) {
7894
// disable
7995
// as we now add datapackage.json to url this gives 404 rather than bad JSON
8096
// assert.equal(err.message.indexOf('datapackage.json is invalid JSON'), 0);
@@ -83,10 +99,10 @@ describe('load', function() {
8399
});
84100
});
85101

86-
describe('loadMany', function() {
102+
describe('loadManyUrls', function() {
87103
it('works', function(done) {
88104
gdpUrl = 'https://github.com/datasets/gdp';
89-
tools.loadMany([sourceUrl, gdpUrl], function(err, dpout) {
105+
tools.loadManyUrls([sourceUrl, gdpUrl], function(err, dpout) {
90106
assert.equal(Object.keys(dpout).length, 2);
91107
assert.equal(dpout['gdp'].homepage, gdpUrl);
92108
done();

test/data/dp1/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The README.

test/data/dp1/datapackage.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "abc"
3+
}

0 commit comments

Comments
 (0)