Last active
October 31, 2016 22:33
-
-
Save dimpiax/bf284ed47371f8375d077fe2dd9f6bb9 to your computer and use it in GitHub Desktop.
Ukraine's declaration parser example (part)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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