Commit 114045eb authored by Aurel's avatar Aurel

split jio code & remove clearroad parts

parent 384d5dea
...@@ -198,7 +198,7 @@ module.exports = function (grunt) { ...@@ -198,7 +198,7 @@ module.exports = function (grunt) {
'src/jio.date/*.js', 'src/jio.date/*.js',
'src/jio.js', 'src/jio-nodejs.js',
'src/jio.storage/replicatestorage.js', 'src/jio.storage/replicatestorage.js',
'src/jio.storage/uuidstorage.js', 'src/jio.storage/uuidstorage.js',
......
function ClearRoadAccountRegistration(login, password) {
if (!(this instanceof ClearRoadAccountRegistration)) {
return new ClearRoadAccountRegistration();
}
var DATABASE = "cr-account-registration";
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: false,
conflict_handling: 1,
check_local_modification: false,
check_local_creation: true,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: false,
check_remote_deletion: false,
local_sub_storage: {
type: "mapping",
id: ["equalSubProperty", "reference"],
sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
}
},
remote_sub_storage: {
type: "mapping",
id: ["equalSubProperty", "reference"],
property: {"portal_type": ["equalValue", "Road Account Message"],
"parent_relative_url": ["equalValue", "road_account_message_module"]},
sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_view",
login: login,
password: password
}
}
});
}
ClearRoadAccountRegistration.prototype = new ClearRoadAccountRegistration();
ClearRoadAccountRegistration.prototype.constructor = ClearRoadAccountRegistration;
ClearRoadAccountRegistration.prototype.post = function(){
return this.jio.post.apply(this.jio, arguments);
};
ClearRoadAccountRegistration.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
function ClearRoadAccountRegistrationReport(login, password, day_range) {
if (!(this instanceof ClearRoadAccountRegistrationReport)) {
return new ClearRoadAccountRegistrationReport();
}
var DATABASE = "cr-account-registration-report";
var query = 'portal_type:"Road Account Message"';
if (day_range !== undefined){
var from_date = new Date();
from_date.setDate(from_date.getDate() - day_range);
query += ' AND modification_date: >="'+from_date.toJSON()+'"';
}
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: true,
conflict_handling: 1,
query: {
query : query,
sort_on: [['modification_date', 'descending']],
limit: [0, 1234567890]
},
check_local_modification: false,
check_local_creation: false,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: true,
check_remote_deletion: false,
local_sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
},
remote_sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_ingestion_report_view",
login: login,
password: password
}
});
}
ClearRoadAccountRegistrationReport.prototype = new ClearRoadAccountRegistrationReport();
ClearRoadAccountRegistrationReport.prototype.constructor = ClearRoadAccountRegistrationReport;
ClearRoadAccountRegistrationReport.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
ClearRoadAccountRegistrationReport.prototype.allDocs = function(){
return this.jio.allDocs.apply(this.jio, [{select_list: ['reference', 'comment', 'state'], query : 'portal_type:"Road Account Message"'}]);
};
function ClearRoadUsageData(login, password) {
if (!(this instanceof ClearRoadUsageData)) {
return new ClearRoadUsageData();
}
var DATABASE = "cr-road-usage-data";
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: false,
conflict_handling: 1,
check_local_modification: false,
check_local_creation: true,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: false,
check_remote_deletion: false,
local_sub_storage: {
type: "mapping",
id: ["equalSubProperty", "reference"],
sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
}
},
remote_sub_storage: {
type: "mapping",
id: ["equalSubProperty", "reference"],
property: {"portal_type": ["equalValue", "Road Message"],
"parent_relative_url": ["equalValue", "road_message_module"]},
sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_view",
login: login,
password: password
}
}
});
}
ClearRoadUsageData.prototype = new ClearRoadUsageData();
ClearRoadUsageData.prototype.constructor = ClearRoadUsageData;
ClearRoadUsageData.prototype.post = function(){
return this.jio.post.apply(this.jio, arguments);
};
ClearRoadUsageData.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
function ClearRoadUsageDataReport(login, password, day_range) {
if (!(this instanceof ClearRoadUsageDataReport)) {
return new ClearRoadUsageDataReport();
}
var DATABASE = "cr-road-usage-data-report";
var query = 'portal_type:"Road Message"';
if (day_range !== undefined){
var from_date = new Date();
from_date.setDate(from_date.getDate() - day_range);
query += ' AND modification_date: >="'+from_date.toJSON()+'"';
}
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: true,
conflict_handling: 1,
query: {
query : query,
sort_on: [['modification_date', 'descending']],
limit: [0, 1234567890]
},
check_local_modification: false,
check_local_creation: false,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: true,
check_remote_deletion: false,
local_sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
},
remote_sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_ingestion_report_view",
login: login,
password: password
}
});
}
ClearRoadUsageDataReport.prototype = new ClearRoadUsageDataReport();
ClearRoadUsageDataReport.prototype.constructor = ClearRoadUsageDataReport;
ClearRoadUsageDataReport.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
ClearRoadUsageDataReport.prototype.allDocs = function(){
return this.jio.allDocs.apply(this.jio, [{select_list: ['reference', 'comment', 'state'], query : 'portal_type:"Road Message"'}]);
};
function ClearRoadAccountDirectory(login, password) {
if (!(this instanceof ClearRoadAccountDirectory)) {
return new ClearRoadAccountDirectory();
}
var DATABASE = "cr-account-directory";
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: true,
conflict_handling: 1,
query: {
query : 'portal_type:"Road Account"',
sort_on: [['modification_date', 'descending']],
limit: [0, 1234567890]
},
check_local_modification: false,
check_local_creation: false,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: true,
check_remote_deletion: false,
local_sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
},
remote_sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_directory_view",
login: login,
password: password
}
});
}
ClearRoadAccountDirectory.prototype = new ClearRoadAccountDirectory();
ClearRoadAccountDirectory.prototype.constructor = ClearRoadAccountDirectory;
ClearRoadAccountDirectory.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
ClearRoadAccountDirectory.prototype.allDocs = function(){
return this.jio.allDocs.apply(this.jio, [{select_list: ['reference', 'registrations'], query : 'portal_type:"Road Account"'}]);
};
function ClearRoadAccountBalance(login, password, reference) {
if (!(this instanceof ClearRoadAccountBalance)) {
return new ClearRoadAccountBalance();
}
var DATABASE = "cr-account-balance";
if (reference === undefined){
return ;
}
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: true,
conflict_handling: 1,
query: {
query : 'portal_type:"Billing Period" AND reference: ="'+reference+'"'
},
check_local_modification: false,
check_local_creation: false,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: true,
check_remote_deletion: false,
local_sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
},
remote_sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_balance_view",
login: login,
password: password
}
});
}
ClearRoadAccountBalance.prototype = new ClearRoadAccountBalance();
ClearRoadAccountBalance.prototype.constructor = ClearRoadAccountBalance;
ClearRoadAccountBalance.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
ClearRoadAccountBalance.prototype.allDocs = function(){
return this.jio.allDocs.apply(this.jio, [{select_list: ['reference', 'start_date', 'stop_date', 'balance'], query : 'portal_type:"Billing Period"'}]);
};
function ClearRoadEvent(login, password) {
if (!(this instanceof ClearRoadEvent)) {
return new ClearRoadEvent();
}
var DATABASE = "cr-event-data";
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: false,
conflict_handling: 1,
check_local_modification: false,
check_local_creation: true,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: false,
check_remote_deletion: false,
local_sub_storage: {
type: "mapping",
id: ["equalSubProperty", "reference"],
sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
}
},
remote_sub_storage: {
type: "mapping",
id: ["equalSubProperty", "reference"],
property: {"portal_type": ["equalValue", "Road Event Message"],
"parent_relative_url": ["equalValue", "road_event_message_module"]},
sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_view",
login: login,
password: password
}
}
});
}
ClearRoadEvent.prototype = new ClearRoadEvent();
ClearRoadEvent.prototype.constructor = ClearRoadEvent;
ClearRoadEvent.prototype.post = function(){
return this.jio.post.apply(this.jio, arguments);
};
ClearRoadEvent.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
function ClearRoadEventReport(login, password, day_range) {
if (!(this instanceof ClearRoadEventReport)) {
return new ClearRoadEventReport();
}
var DATABASE = "cr-event-report";
var query = 'portal_type:"Road Event Message"';
if (day_range !== undefined){
var from_date = new Date();
from_date.setDate(from_date.getDate() - day_range);
query += ' AND modification_date: >="'+from_date.toJSON()+'"';
}
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: true,
conflict_handling: 1,
query: {
query : query,
sort_on: [['modification_date', 'descending']],
limit: [0, 1234567890]
},
check_local_modification: false,
check_local_creation: false,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: true,
check_remote_deletion: false,
local_sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
},
remote_sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_ingestion_report_view",
login: login,
password: password
}
});
}
ClearRoadEventReport.prototype = new ClearRoadEventReport();
ClearRoadEventReport.prototype.constructor = ClearRoadEventReport;
ClearRoadEventReport.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
ClearRoadEventReport.prototype.allDocs = function(){
return this.jio.allDocs.apply(this.jio, [{select_list: ['reference', 'comment', 'state'], query : 'portal_type:"Road Event Message"'}]);
};
function ClearRoadEventDirectory(login, password, day_range) {
if (!(this instanceof ClearRoadEventDirectory)) {
return new ClearRoadEventDirectory();
}
var DATABASE = "cr-event-directory";
var query = 'portal_type:"Road Event" AND simulation_state: !="rejected"';
if (day_range !== undefined){
var from_date = new Date();
from_date.setDate(from_date.getDate() - day_range);
query += ' AND modification_date: >="'+from_date.toJSON()+'"';
}
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: true,
conflict_handling: 1,
query: {
query : query,
sort_on: [['modification_date', 'descending']],
limit: [0, 1234567890]
},
check_local_modification: false,
check_local_creation: false,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: true,
check_remote_deletion: false,
local_sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
},
remote_sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_view",
login: login,
password: password
}
});
}
ClearRoadEventDirectory.prototype = new ClearRoadEventDirectory();
ClearRoadEventDirectory.prototype.constructor = ClearRoadEventDirectory;
ClearRoadEventDirectory.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
ClearRoadEventDirectory.prototype.allDocs = function(){
return this.jio.allDocs.apply(this.jio, [{select_list: ['event_date', 'report_date', 'obu_reference', 'vehicle_reference', 'type', 'state'], query : 'portal_type:"Road Event"'}]);
};
function ClearRoadBillingPeriodRegistration(login, password) {
if (!(this instanceof ClearRoadBillingPeriodRegistration)) {
return new ClearRoadBillingPeriodRegistration();
}
var DATABASE = "cr-billing-period-registration";
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: false,
conflict_handling: 1,
check_local_modification: false,
check_local_creation: true,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: false,
check_remote_deletion: false,
local_sub_storage: {
type: "mapping",
id: ["equalSubProperty", "reference"],
sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
}
},
remote_sub_storage: {
type: "mapping",
id: ["equalSubProperty", "reference"],
property: {"portal_type": ["equalValue", "Billing Period Message"],
"parent_relative_url": ["equalValue", "billing_period_message_module"]},
sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_view",
login: login,
password: password,
}
}
});
}
ClearRoadBillingPeriodRegistration.prototype = new ClearRoadBillingPeriodRegistration();
ClearRoadBillingPeriodRegistration.prototype.constructor = ClearRoadBillingPeriodRegistration;
ClearRoadBillingPeriodRegistration.prototype.post = function(){
return this.jio.post.apply(this.jio, arguments);
};
ClearRoadBillingPeriodRegistration.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
function ClearRoadBillingPeriodRegistrationReport(login, password, day_range) {
if (!(this instanceof ClearRoadBillingPeriodRegistrationReport)) {
return new ClearRoadBillingPeriodRegistrationReport();
}
var DATABASE = "cr-billing-period-registration-report";
var query = 'portal_type:"Billing Period Message"';
if (day_range !== undefined){
var from_date = new Date();
from_date.setDate(from_date.getDate() - day_range);
query += ' AND modification_date: >="'+from_date.toJSON()+'"';
}
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: true,
conflict_handling: 1,
query: {
query : query,
sort_on: [['modification_date', 'descending']],
limit: [0, 1234567890]
},
check_local_modification: false,
check_local_creation: false,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: true,
check_remote_deletion: false,
local_sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
},
remote_sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_ingestion_report_view",
login: login,
password: password
}
});
}
ClearRoadBillingPeriodRegistrationReport.prototype = new ClearRoadBillingPeriodRegistrationReport();
ClearRoadBillingPeriodRegistrationReport.prototype.constructor = ClearRoadBillingPeriodRegistrationReport;
ClearRoadBillingPeriodRegistrationReport.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
ClearRoadBillingPeriodRegistrationReport.prototype.allDocs = function(){
return this.jio.allDocs.apply(this.jio, [{select_list: ['reference', 'comment', 'state'], query : 'portal_type:"Billing Period Message"'}]);
};
function ClearRoadBillingPeriodDirectory(login, password) {
if (!(this instanceof ClearRoadBillingPeriodDirectory)) {
return new ClearRoadBillingPeriodDirectory();
}
var DATABASE = "cr-billing-period-directory";
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: true,
conflict_handling: 1,
query: {
query : 'portal_type:"Billing Period"',
sort_on: [['modification_date', 'descending']],
limit: [0, 1234567890]
},
check_local_modification: false,
check_local_creation: false,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: true,
check_remote_deletion: false,
local_sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
},
remote_sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_directory_view",
login: login,
password: password
}
});
}
ClearRoadBillingPeriodDirectory.prototype = new ClearRoadBillingPeriodDirectory();
ClearRoadBillingPeriodDirectory.prototype.constructor = ClearRoadBillingPeriodDirectory;
ClearRoadBillingPeriodDirectory.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
ClearRoadBillingPeriodDirectory.prototype.allDocs = function(){
return this.jio.allDocs.apply(this.jio, [{select_list: ['reference', 'start_date', 'stop_date'], query : 'portal_type:"Billing Period"'}]);
};
function ClearRoadTransactionDirectory(login, password, account) {
if (!(this instanceof ClearRoadTransactionDirectory)) {
return new ClearRoadTransactionDirectory();
}
var DATABASE = "cr-transaction-directory";
var query = 'portal_type:"Road Account"';
if (account !== undefined){
query += ' AND reference: ="'+account+'"';
}
this.jio = jIO.createJIO({
type: "replicate",
parallel_operation_amount: 100,
use_remote_post: true,
conflict_handling: 1,
query: {
query : query,
sort_on: [['modification_date', 'descending']],
limit: [0, 1234567890]
},
check_local_modification: false,
check_local_creation: false,
check_local_deletion: false,
check_remote_modification: false,
check_remote_creation: true,
check_remote_deletion: false,
local_sub_storage: {
type: "query",
sub_storage: {
type: "uuid",
sub_storage: {
type: "memory",
database: DATABASE
}
}
},
remote_sub_storage: {
type: "erp5",
url: "https://softinst69465.host.vifib.net/erp5/web_site_module/hateoas",
default_view_reference: "jio_transactions_view",
login: login,
password: password
}
});
}
ClearRoadTransactionDirectory.prototype = new ClearRoadTransactionDirectory();
ClearRoadTransactionDirectory.prototype.constructor = ClearRoadTransactionDirectory;
ClearRoadTransactionDirectory.prototype.sync = function(){
return this.jio.repair.apply(this.jio, arguments);
};
ClearRoadTransactionDirectory.prototype.allDocs = function(){
return this.jio.allDocs.apply(this.jio, [{select_list: ['reference', 'transactions'], query : 'portal_type:"Road Account"'}]);
};
// Exports to node
exports.ClearRoadAccountRegistration = ClearRoadAccountRegistration;
exports.ClearRoadAccountRegistrationReport = ClearRoadAccountRegistrationReport;
exports.ClearRoadUsageData = ClearRoadUsageData;
exports.ClearRoadUsageDataReport = ClearRoadUsageDataReport;
exports.ClearRoadAccountDirectory = ClearRoadAccountDirectory;
exports.ClearRoadAccountBalance = ClearRoadAccountBalance;
exports.ClearRoadEvent = ClearRoadEvent;
exports.ClearRoadEventReport = ClearRoadEventReport;
exports.ClearRoadEventDirectory = ClearRoadEventDirectory;
exports.ClearRoadBillingPeriodRegistration = ClearRoadBillingPeriodRegistration;
exports.ClearRoadBillingPeriodRegistrationReport = ClearRoadBillingPeriodRegistrationReport;
exports.ClearRoadBillingPeriodDirectory = ClearRoadBillingPeriodDirectory;
exports.ClearRoadTransactionDirectory = ClearRoadTransactionDirectory;
{
"name": "clearroad",
"version": "1.0.0",
"description": "ClearRoad Lib based on JIO",
"main": "clearroad.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "lab.nexedi.com/nexedi/jio"
},
"keywords": [
"clearroad",
"jio"
],
"author": "Aurélien Calonne",
"license": "ISC"
}
{
"//1": "describes your app and its dependencies",
"//2": "https://docs.npmjs.com/files/package.json",
"//3": "updating this file will download and update your packages",
"name": "clearroad-nodejs",
"version": "0.0.1",
"description": "ClearRoad API on Node.js",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.14.1",
"stream-buffer": "latest",
"xhr2": "latest",
"rsvp": "git+https://lab.nexedi.com/nexedi/rsvp.js.git",
"uritemplate": "latest",
"moment": "file:lib/moment",
"navigator": "latest",
"rusha": "latest",
"form-data": "latest",
"atob": "latest",
"html5": "file:lib/html5",
"node-localstorage": "latest",
"btoa": "latest",
"xhr2": "latest",
"stream-buffers": "latest",
"clearroad": "file:lib/clearroad",
"jio": "file:lib/jio",
"urijs": "latest"
},
"engines": {
"node": "6.9.x"
},
"repository": {
"url": ""
},
"license": "",
"keywords": [
"node",
"jio",
"clearroad"
]
}
// server.js
// where your node app starts
"use strict"
global.URI = require("urijs");
global.RSVP = require('rsvp');
global.UriTemplate = require("uritemplate");
global.moment = require('moment');
global.navigator = require('navigator');
global.Rusha = require('rusha');
global.FormData = require('form-data');
global.atob = require('atob');
global.FileReader = require("html5").FileReader;
global.Blob = require("html5").Blob;
global.localStorage = require('node-localstorage');
global.btoa = require('btoa');
global.XMLHttpRequest = require('xhr2');
global.StreamBuffers = require('stream-buffers');
global.window = global;
global.sessionStorage = {};
var jIO = require('jio');
var clearroad = require("clearroad");
var i = 0;
var cr = new clearroad.ClearRoadBillingPeriodRegistration("testam", "testam");
console.log("init");
cr.post({
"reference" : "Q421",
"start_date" : "2017-02-01T00:00:00Z",
"stop_date" : "2017-03-01T00:00:00Z"
}).push(function (){
console.log("start sync...");
return cr.sync();
}).push(function (){
console.info("Sync done");
}).push(function() {
cr = new clearroad.ClearRoadBillingPeriodRegistrationReport("testam", "testam", 100);
return cr.sync();
}).push(function (){
console.log("sync done && loading result...")
return cr.allDocs()
}).push(function (result) {
console.log("browsing results : "+result.data.total_rows);
for (i = 0; i < result.data.total_rows; i += 1) {
console.log("Ref : "+result.data.rows[i].value.reference+", state : "+result.data.rows[i].value.state+", comment"+result.data.rows[i].value.comment);
}
console.log("Finished");
}).push(undefined, function (error) {
throw error;
});
// init project
var express = require('express');
var app = express();
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function (request, response) {
response.sendFile(__dirname + '/views/index.html');
});
// listen for requests :)
var listener = app.listen(3000, function () {
console.log('Your app is listening on port ' + listener.address().port);
});
/*global window, RSVP, Blob, XMLHttpRequest, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array, navigator, FormData, StreamBuffers */
(function (window, RSVP, Blob, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array, navigator) {
"use strict";
if (window.openDatabase === undefined) {
window.openDatabase = function () {
throw new Error('WebSQL is not supported by ' + navigator.userAgent);
};
}
/* Safari does not define DOMError */
if (window.DOMError === undefined) {
window.DOMError = {};
}
var util = {},
jIO;
function jIOError(message, status_code) {
if ((message !== undefined) && (typeof message !== "string")) {
throw new TypeError('You must pass a string.');
}
this.message = message || "Default Message";
this.status_code = status_code || 500;
}
jIOError.prototype = new Error();
jIOError.prototype.constructor = jIOError;
util.jIOError = jIOError;
/**
* Send request with XHR and return a promise. xhr.onload: The promise is
* resolved when the status code is lower than 400 with the xhr object as
* first parameter. xhr.onerror: reject with xhr object as first
* parameter. xhr.onprogress: notifies the xhr object.
*
* @param {Object} param The parameters
* @param {String} [param.type="GET"] The request method
* @param {String} [param.dataType=""] The data type to retrieve
* @param {String} param.url The url
* @param {Any} [param.data] The data to send
* @param {Function} [param.beforeSend] A function called just before the
* send request. The first parameter of this function is the XHR object.
* @return {Promise} The promise
*/
function ajax(param) {
var xhr = new XMLHttpRequest();
return new RSVP.Promise(function (resolve, reject, notify) {
var k, buffer = new StreamBuffers.WritableStreamBuffer();
xhr.open(param.type || "GET", param.url, true);
xhr.responseType = param.dataType || "";
if (typeof param.headers === 'object' && param.headers !== null) {
for (k in param.headers) {
if (param.headers.hasOwnProperty(k)) {
xhr.setRequestHeader(k, param.headers[k]);
}
}
}
xhr.setRequestHeader("Accept", "*/*");
xhr.addEventListener("load", function (e) {
if (e.target.status >= 400) {
return reject(e);
}
resolve(e);
});
xhr.addEventListener("error", reject);
xhr.addEventListener("progress", notify);
if (typeof param.xhrFields === 'object' && param.xhrFields !== null) {
for (k in param.xhrFields) {
if (param.xhrFields.hasOwnProperty(k)) {
xhr[k] = param.xhrFields[k];
}
}
}
if (typeof param.beforeSend === 'function') {
param.beforeSend(xhr);
}
if (param.data instanceof FormData) {
xhr.setRequestHeader("Content-Type",
"multipart\/form-data; boundary=" + param.data.getBoundary());
param.data.pipe(buffer);
xhr.send(buffer.getContents());
} else {
xhr.send(param.data);
}
}, function () {
xhr.abort();
});
}
util.ajax = ajax;
function readBlobAsText(blob, encoding) {
var fr = new FileReader();
return new RSVP.Promise(function (resolve, reject, notify) {
fr.addEventListener("load", resolve);
fr.addEventListener("error", reject);
fr.addEventListener("progress", notify);
fr.readAsText(blob, encoding);
}, function () {
fr.abort();
});
}
util.readBlobAsText = readBlobAsText;
function readBlobAsArrayBuffer(blob) {
var fr = new FileReader();
return new RSVP.Promise(function (resolve, reject, notify) {
fr.addEventListener("load", resolve);
fr.addEventListener("error", reject);
fr.addEventListener("progress", notify);
fr.readAsArrayBuffer(blob);
}, function () {
fr.abort();
});
}
util.readBlobAsArrayBuffer = readBlobAsArrayBuffer;
function readBlobAsDataURL(blob) {
var fr = new FileReader();
return new RSVP.Promise(function (resolve, reject, notify) {
fr.addEventListener("load", resolve);
fr.addEventListener("error", reject);
fr.addEventListener("progress", notify);
fr.readAsDataURL(blob);
}, function () {
fr.abort();
});
}
util.readBlobAsDataURL = readBlobAsDataURL;
function stringify(obj) {
// Implement a stable JSON.stringify
// Object's keys are alphabetically ordered
var key,
key_list,
i,
value,
result_list;
if (obj === undefined) {
return undefined;
}
if (obj.constructor === Object) {
key_list = Object.keys(obj).sort();
result_list = [];
for (i = 0; i < key_list.length; i += 1) {
key = key_list[i];
value = stringify(obj[key]);
if (value !== undefined) {
result_list.push(stringify(key) + ':' + value);
}
}
return '{' + result_list.join(',') + '}';
}
if (obj.constructor === Array) {
result_list = [];
for (i = 0; i < obj.length; i += 1) {
result_list.push(stringify(obj[i]));
}
return '[' + result_list.join(',') + ']';
}
return JSON.stringify(obj);
}
util.stringify = stringify;
// https://gist.github.com/davoclavo/4424731
function dataURItoBlob(dataURI) {
if (dataURI === 'data:') {
return new Blob();
}
// convert base64 to raw binary data held in a string
var byteString = atob(dataURI.split(',')[1]),
// separate out the mime component
mimeString = dataURI.split(',')[0].split(':')[1],
// write the bytes of the string to an ArrayBuffer
arrayBuffer = new ArrayBuffer(byteString.length),
_ia = new Uint8Array(arrayBuffer),
i;
mimeString = mimeString.slice(0, mimeString.length - ";base64".length);
for (i = 0; i < byteString.length; i += 1) {
_ia[i] = byteString.charCodeAt(i);
}
return new Blob([arrayBuffer], {type: mimeString});
}
util.dataURItoBlob = dataURItoBlob;
// tools
function checkId(argument_list, storage, method_name) {
if (typeof argument_list[0] !== 'string' || argument_list[0] === '') {
throw new jIO.util.jIOError(
"Document id must be a non empty string on '" + storage.__type +
"." + method_name + "'.",
400
);
}
}
function checkAttachmentId(argument_list, storage, method_name) {
if (typeof argument_list[1] !== 'string' || argument_list[1] === '') {
throw new jIO.util.jIOError(
"Attachment id must be a non empty string on '" + storage.__type +
"." + method_name + "'.",
400
);
}
}
function declareMethod(klass, name, precondition_function, post_function) {
klass.prototype[name] = function () {
var argument_list = arguments,
context = this,
precondition_result;
return new RSVP.Queue()
.push(function () {
if (precondition_function !== undefined) {
return precondition_function.apply(
context.__storage,
[argument_list, context, name]
);
}
})
.push(function (result) {
var storage_method = context.__storage[name];
precondition_result = result;
if (storage_method === undefined) {
throw new jIO.util.jIOError(
"Capacity '" + name + "' is not implemented on '" +
context.__type + "'",
501
);
}
return storage_method.apply(
context.__storage,
argument_list
);
})
.push(function (result) {
if (post_function !== undefined) {
return post_function.call(
context,
argument_list,
result,
precondition_result
);
}
return result;
});
};
// Allow chain
return this;
}
/////////////////////////////////////////////////////////////////
// jIO Storage Proxy
/////////////////////////////////////////////////////////////////
function JioProxyStorage(type, storage) {
if (!(this instanceof JioProxyStorage)) {
return new JioProxyStorage();
}
this.__type = type;
this.__storage = storage;
}
declareMethod(JioProxyStorage, "put", checkId, function (argument_list) {
return argument_list[0];
});
declareMethod(JioProxyStorage, "get", checkId);
declareMethod(JioProxyStorage, "bulk");
declareMethod(JioProxyStorage, "remove", checkId, function (argument_list) {
return argument_list[0];
});
JioProxyStorage.prototype.post = function () {
var context = this,
argument_list = arguments;
return new RSVP.Queue()
.push(function () {
var storage_method = context.__storage.post;
if (storage_method === undefined) {
throw new jIO.util.jIOError(
"Capacity 'post' is not implemented on '" + context.__type + "'",
501
);
}
return context.__storage.post.apply(context.__storage, argument_list);
});
};
declareMethod(JioProxyStorage, 'putAttachment', function (argument_list,
storage,
method_name) {
checkId(argument_list, storage, method_name);
checkAttachmentId(argument_list, storage, method_name);
var options = argument_list[3] || {};
if (typeof argument_list[2] === 'string') {
argument_list[2] = new Blob([argument_list[2]], {
"type": options._content_type || options._mimetype ||
"text/plain;charset=utf-8"
});
} else if (!(argument_list[2] instanceof Blob)) {
throw new jIO.util.jIOError(
'Attachment content is not a blob',
400
);
}
});
declareMethod(JioProxyStorage, 'removeAttachment', function (argument_list,
storage,
method_name) {
checkId(argument_list, storage, method_name);
checkAttachmentId(argument_list, storage, method_name);
});
declareMethod(JioProxyStorage, 'getAttachment', function (argument_list,
storage,
method_name) {
var result = "blob";
// if (param.storage_spec.type !== "indexeddb" &&
// param.storage_spec.type !== "dav" &&
// (param.kwargs._start !== undefined
// || param.kwargs._end !== undefined)) {
// restCommandRejecter(param, [
// 'bad_request',
// 'unsupport',
// '_start, _end not support'
// ]);
// return false;
// }
checkId(argument_list, storage, method_name);
checkAttachmentId(argument_list, storage, method_name);
// Drop optional parameters, which are only used in postfunction
if (argument_list[2] !== undefined) {
result = argument_list[2].format || result;
delete argument_list[2].format;
}
return result;
}, function (argument_list, blob, convert) {
var result;
if (!(blob instanceof Blob)) {
throw new jIO.util.jIOError(
"'getAttachment' (" + argument_list[0] + " , " +
argument_list[1] + ") on '" + this.__type +
"' does not return a Blob.",
501
);
}
if (convert === "blob") {
result = blob;
} else if (convert === "data_url") {
result = new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsDataURL(blob);
})
.push(function (evt) {
return evt.target.result;
});
} else if (convert === "array_buffer") {
result = new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsArrayBuffer(blob);
})
.push(function (evt) {
return evt.target.result;
});
} else if (convert === "text") {
result = new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsText(blob);
})
.push(function (evt) {
return evt.target.result;
});
} else if (convert === "json") {
result = new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsText(blob);
})
.push(function (evt) {
return JSON.parse(evt.target.result);
});
} else {
throw new jIO.util.jIOError(
this.__type + ".getAttachment format: '" + convert +
"' is not supported",
400
);
}
return result;
});
JioProxyStorage.prototype.buildQuery = function () {
var storage_method = this.__storage.buildQuery,
context = this,
argument_list = arguments;
if (storage_method === undefined) {
throw new jIO.util.jIOError(
"Capacity 'buildQuery' is not implemented on '" + this.__type + "'",
501
);
}
return new RSVP.Queue()
.push(function () {
return storage_method.apply(
context.__storage,
argument_list
);
});
};
JioProxyStorage.prototype.hasCapacity = function (name) {
var storage_method = this.__storage.hasCapacity,
capacity_method = this.__storage[name];
if (capacity_method !== undefined) {
return true;
}
if ((storage_method === undefined) ||
!storage_method.apply(this.__storage, arguments)) {
throw new jIO.util.jIOError(
"Capacity '" + name + "' is not implemented on '" + this.__type + "'",
501
);
}
return true;
};
JioProxyStorage.prototype.allDocs = function (options) {
var context = this;
if (options === undefined) {
options = {};
}
return new RSVP.Queue()
.push(function () {
if (context.hasCapacity("list") &&
((options.query === undefined) || context.hasCapacity("query")) &&
((options.sort_on === undefined) || context.hasCapacity("sort")) &&
((options.select_list === undefined) ||
context.hasCapacity("select")) &&
((options.include_docs === undefined) ||
context.hasCapacity("include")) &&
((options.limit === undefined) || context.hasCapacity("limit"))) {
return context.buildQuery(options);
}
})
.push(function (result) {
return {
data: {
rows: result,
total_rows: result.length
}
};
});
};
declareMethod(JioProxyStorage, "allAttachments", checkId);
declareMethod(JioProxyStorage, "repair");
JioProxyStorage.prototype.repair = function () {
var context = this,
argument_list = arguments;
return new RSVP.Queue()
.push(function () {
var storage_method = context.__storage.repair;
if (storage_method !== undefined) {
return context.__storage.repair.apply(context.__storage,
argument_list);
}
});
};
/////////////////////////////////////////////////////////////////
// Storage builder
/////////////////////////////////////////////////////////////////
function JioBuilder() {
if (!(this instanceof JioBuilder)) {
return new JioBuilder();
}
this.__storage_types = {};
}
JioBuilder.prototype.createJIO = function (storage_spec, util) {
if (typeof storage_spec.type !== 'string') {
throw new TypeError("Invalid storage description");
}
if (!this.__storage_types[storage_spec.type]) {
throw new TypeError("Unknown storage '" + storage_spec.type + "'");
}
return new JioProxyStorage(
storage_spec.type,
new this.__storage_types[storage_spec.type](storage_spec, util)
);
};
JioBuilder.prototype.addStorage = function (type, Constructor) {
if (typeof type !== 'string') {
throw new TypeError(
"jIO.addStorage(): Argument 1 is not of type 'string'"
);
}
if (typeof Constructor !== 'function') {
throw new TypeError("jIO.addStorage(): " +
"Argument 2 is not of type 'function'");
}
if (this.__storage_types[type] !== undefined) {
throw new TypeError("jIO.addStorage(): Storage type already exists");
}
this.__storage_types[type] = Constructor;
};
JioBuilder.prototype.util = util;
JioBuilder.prototype.QueryFactory = QueryFactory;
JioBuilder.prototype.Query = Query;
/////////////////////////////////////////////////////////////////
// global
/////////////////////////////////////////////////////////////////
jIO = new JioBuilder();
window.jIO = jIO;
}(window, RSVP, Blob, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array, navigator));
/*global window, RSVP, Blob, XMLHttpRequest, QueryFactory, Query, atob, /*global window, RSVP, Blob, XMLHttpRequest, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array, navigator, FormData, StreamBuffers */ FileReader, ArrayBuffer, Uint8Array, navigator */
(function (window, RSVP, Blob, QueryFactory, Query, atob, (function (window, RSVP, Blob, QueryFactory, Query, atob,
FileReader, ArrayBuffer, Uint8Array, navigator) { FileReader, ArrayBuffer, Uint8Array, navigator) {
"use strict"; "use strict";
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
function ajax(param) { function ajax(param) {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
return new RSVP.Promise(function (resolve, reject, notify) { return new RSVP.Promise(function (resolve, reject, notify) {
var k, buffer = new StreamBuffers.WritableStreamBuffer(); var k;
xhr.open(param.type || "GET", param.url, true); xhr.open(param.type || "GET", param.url, true);
xhr.responseType = param.dataType || ""; xhr.responseType = param.dataType || "";
if (typeof param.headers === 'object' && param.headers !== null) { if (typeof param.headers === 'object' && param.headers !== null) {
...@@ -57,7 +57,6 @@ ...@@ -57,7 +57,6 @@
} }
} }
} }
xhr.setRequestHeader("Accept", "*/*");
xhr.addEventListener("load", function (e) { xhr.addEventListener("load", function (e) {
if (e.target.status >= 400) { if (e.target.status >= 400) {
return reject(e); return reject(e);
...@@ -76,14 +75,7 @@ ...@@ -76,14 +75,7 @@
if (typeof param.beforeSend === 'function') { if (typeof param.beforeSend === 'function') {
param.beforeSend(xhr); param.beforeSend(xhr);
} }
if (param.data instanceof FormData) { xhr.send(param.data);
xhr.setRequestHeader("Content-Type",
"multipart\/form-data; boundary=" + param.data.getBoundary());
param.data.pipe(buffer);
xhr.send(buffer.getContents());
} else {
xhr.send(param.data);
}
}, function () { }, function () {
xhr.abort(); xhr.abort();
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment