Skip to content

Instantly share code, notes, and snippets.

@dimpiax
Last active October 31, 2016 22:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimpiax/bf284ed47371f8375d077fe2dd9f6bb9 to your computer and use it in GitHub Desktop.
Save dimpiax/bf284ed47371f8375d077fe2dd9f6bb9 to your computer and use it in GitHub Desktop.
Ukraine's declaration parser example (part)
// link to declaration.json – https://public-api.nazk.gov.ua/v1/declaration/41462809-89e2-4ad9-a9c9-54bb258541df
// usage: node declaration_example.js
'use strict';
var fs = require('fs');
function init() {
let file = fs.readFileSync('declaration.json');
let json = JSON.parse(file);
let declaration = new Declaration();
declaration.fromJSON(json);
console.log(`owners: ${declaration.data.family.members}`);
}
class CustomStringConvertible {
toString() {
var d = `[ ${this.constructor.name} `;
Object.keys(this).forEach(el => d += `${el}="${this[el]}" `);
return d+']';
}
}
class Declaration {
fromJSON(value) {
this.id = value.id;
this.createdDate = value.created_date;
this.lastModifiedDate = value.lastmodified_date;
this.serverInfo = value.server_info;
if(value.data != null) {
this.data = new DeclarationData();
this.data.fromJSON(value.data);
}
}
}
class DeclarationData {
buildPart(value, classs) {
if(this.notExist(value)) return null;
let o = new classs();
o.fromJSON(value);
return o;
}
fromJSON(value) {
this.info = this.buildPart(value.step_0, Info);
console.log(`info: ${this.info.toString()}`);
this.owner = this.buildPart(value.step_1, Owner);
console.log(`owner: ${this.owner}`);
this.family = this.buildPart(value.step_2, Family);
console.log(`family: ${this.family}`);
}
notExist(value) {
return value == null || !Object.keys(value).length;
}
}
class Info extends CustomStringConvertible {
fromJSON(value) {
this.declarationType = value.declarationType;
// strict assign etc
}
}
class Owner extends CustomStringConvertible {
fromJSON(value) {
for(var i in value) {
this[i] = value[i];
}
}
}
class Family extends CustomStringConvertible {
fromJSON(value) {
this.members = [];
for(var i in value) {
this.members.push(new Member(i));
}
this.members.forEach(el => el.fromJSON(value[el.id]))
}
}
class Member extends CustomStringConvertible {
constructor(id) {
super();
this.id = id;
}
fromJSON(value) {
for(var i in value) {
this[i] = value[i];
}
}
}
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment