-
Notifications
You must be signed in to change notification settings - Fork 1
/
load.js
56 lines (46 loc) · 1.51 KB
/
load.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var fs = require("fs");
var HTTPRequest = require('HTTPRequest');
var totalRecordsCreated = 0;
fs.readFile('./disc-database.json', parseFile);
function parseFile(err, data) {
if (err) throw err;
var discs = JSON.parse(data);
discs.forEach(function(disc) {
var url = 'http://localhost:3000/discs.json';
var data = {
'disc[name]' : disc.name,
'disc[manufacturer]' : disc.manufacturer,
'disc[throw_type]' : disc.type,
'disc[flight_difficulty]' : disc.flight.difficulty,
'disc[flight_speed]' : disc.flight.speed,
'disc[flight_glide]' : disc.flight.glide,
'disc[flight_turn]' : disc.flight.turn,
'disc[flight_fade]' : disc.flight.fade
};
HTTPRequest.post(url, data, createRespondToDiscCreation(disc.plastic));
});
}
function createRespondToDiscCreation(editions) {
return function respondToDiscCreation(status, headers, content)
{
totalRecordsCreated += 1;
var disc = JSON.parse(content);
console.log('Disc ' + disc.id + ' created.')
var url = 'http://localhost:3000/discs/'+disc.id+'/disc_editions.json'
if (editions) {
editions.forEach(function(edition) {
var data = {
'disc_edition[plastic]' : edition.name,
'disc_edition[price]' : edition.price
}
HTTPRequest.post(url, data, respondToDiscEditionCreation);
});
}
}
}
function respondToDiscEditionCreation(status, headers, content)
{
totalRecordsCreated += 1;
var discEdition = JSON.parse(content);
console.log('Disc Edition ' + discEdition.id + ' created for disc ' + discEdition.disc_id + '.')
}