Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
organize XML
Browse files Browse the repository at this point in the history
  • Loading branch information
therealbenpai committed Jul 21, 2024
1 parent 304de0b commit b33a1ba
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 243 deletions.
73 changes: 73 additions & 0 deletions modules/api/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class Response {

Check failure on line 1 in modules/api/base.js

View check run for this annotation

Codeac.io / Codeac Code Quality

Parsing error: No Babel config file detected for /tmp/commit-1205871-eslint/modules/api/base.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files.
constructor(type) {
this.header = [
new SimpleObj('Created', new Date().toISOString()),
new SimpleObj('Type', type),
];
this.data = [];
}
add(obj) {
this.data.push(obj.XML);
return this;
}
get XML() {
return {
res: [
{
header: this.header.map(obj => obj.XML),
},
{
data: this.data,
},
],
};
}
}

class Obj {
constructor(type, key, value) {
this.type = type;
this.key = key;
this.value = value;
}
}

class ExtendedObj extends Obj {
constructor(key) {
super('extended', key, []);
}
add(obj) {
this.value.push(obj.XML);
return this;
}
get XML() {
return {
object: [
{ _attr: { type: 'extended' } },
{ key: this.key },
{ value: this.value },
],
};
}
}

class SimpleObj extends Obj {
constructor(key, value) {
super('simple', key, value);
}
get XML() {
return {
object: [
{
_attr: {
type: 'simple',
key: this.key,
value: this.value,
},
},
],
};
}
}

module.exports = { Response, Obj, ExtendedObj, SimpleObj };
33 changes: 19 additions & 14 deletions modules/api/location.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const xml = require('xml');

Check failure on line 1 in modules/api/location.js

View check run for this annotation

Codeac.io / Codeac Code Quality

Parsing error: No Babel config file detected for /tmp/commit-1205871-eslint/modules/api/location.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files.
const yaml = require('yaml');
const Base = require('./base');

class Location {
constructor(data) {
Expand All @@ -12,7 +13,7 @@ class Location {
country: data.address.country || '',
postalCode: data.address.postalCode || '',
};
this.pluscode = data.pluscode || '';
this.plusCode = data.pluscode || '';
this.coords = {
lat: data.coords.lat || 0,
lng: data.coords.lng || 0,
Expand All @@ -26,19 +27,23 @@ class Location {
});
}
get XML() {
return xml({
address: [
{ full: this.address.full },
{ houseNumber: this.address.houseNumber },
{ street: this.address.street },
{ city: this.address.city },
{ region: this.address.region },
{ country: this.address.country },
{ postalCode: this.address.postalCode },
],
pluscode: this.pluscode,
coords: this.coords,
});
const res = new Base.Response('Location');
res
.add(new Base.ExtendedObj('address')
.add(new Base.SimpleObj('full', this.address.full))
.add(new Base.SimpleObj('houseNumber', this.address.houseNumber))
.add(new Base.SimpleObj('street', this.address.street))
.add(new Base.SimpleObj('city', this.address.city))
.add(new Base.SimpleObj('region', this.address.region))
.add(new Base.SimpleObj('country', this.address.country))
.add(new Base.SimpleObj('postalCode', this.address.postalCode)),
)
.add(new Base.SimpleObj('plusCode', this.plusCode))
.add(new Base.ExtendedObj('coords')
.add(new Base.SimpleObj('lat', this.coords.lat))
.add(new Base.SimpleObj('lng', this.coords.lng)),
);
return xml(res.XML);
}
get YAML() {
return yaml.stringify({
Expand Down
54 changes: 28 additions & 26 deletions modules/api/phone.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
const xml = require('xml');

Check failure on line 1 in modules/api/phone.js

View check run for this annotation

Codeac.io / Codeac Code Quality

Parsing error: No Babel config file detected for /tmp/commit-1205871-eslint/modules/api/phone.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files.
const yaml = require('yaml');
const Base = require('./base');

class Phone {
constructor(data) {
this.status = data.status || 'error';
this.phone = data.phone || '';
this.phoneValid = data.phone_valid || false;
this.phoneType = data.phone_type || '';
this.phoneRegion = data.phone_region || '';
this.country = data.country || '';
this.countryCode = data.country_code || '';
this.countryPrefix = data.country_prefix || '';
this.internationalNumber = data.international_number || '';
this.localNumber = data.local_number || '';
this.e164 = data.e164 || '';
this.carrier = data.carrier || '';
this.status = data.status ?? 'error';
this.phone = data.phone ?? '';
this.phoneValid = data.phone_valid ?? false;
this.phoneType = data.phone_type ?? '';
this.phoneRegion = data.phone_region ?? '';
this.country = data.country ?? '';
this.countryCode = data.country_code ?? '';
this.countryPrefix = data.country_prefix ?? '';
this.internationalNumber = data.international_number ?? '';
this.localNumber = data.local_number ?? '';
this.e164 = data.e164 ?? '';
this.carrier = data.carrier ?? '';
}
get JSON() {
return JSON.stringify({
Expand All @@ -33,20 +34,21 @@ class Phone {
});
}
get XML() {
return xml({
status: this.status,
phone: this.phone,
phoneValid: this.phoneValid,
phoneType: this.phoneType,
phoneRegion: this.phoneRegion,
country: this.country,
countryCode: this.countryCode,
countryPrefix: this.countryPrefix,
internationalNumber: this.internationalNumber,
localNumber: this.localNumber,
e164: this.e164,
carrier: this.carrier,
});
const res = new Base.Response('Phone');

Check warning on line 37 in modules/api/phone.js

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 16 lines is too similar to modules/api/phone.js:54
res
.add(new Base.SimpleObj('status', this.status))
.add(new Base.SimpleObj('phone', this.phone))
.add(new Base.SimpleObj('phoneValid', this.phoneValid))
.add(new Base.SimpleObj('phoneType', this.phoneType))
.add(new Base.SimpleObj('phoneRegion', this.phoneRegion))
.add(new Base.SimpleObj('country', this.country))
.add(new Base.SimpleObj('countryCode', this.countryCode))
.add(new Base.SimpleObj('countryPrefix', this.countryPrefix))
.add(new Base.SimpleObj('internationalNumber', this.internationalNumber))
.add(new Base.SimpleObj('localNumber', this.localNumber))
.add(new Base.SimpleObj('e164', this.e164))
.add(new Base.SimpleObj('carrier', this.carrier));
return xml(res.XML);
}
get YAML() {
return yaml.stringify({
Expand Down
18 changes: 10 additions & 8 deletions modules/api/quote.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
const xml = require('xml');

Check failure on line 1 in modules/api/quote.js

View check run for this annotation

Codeac.io / Codeac Code Quality

Parsing error: No Babel config file detected for /tmp/commit-1205871-eslint/modules/api/quote.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files.
const yaml = require('yaml');
const Base = require('./base');

class Quote {
constructor(data) {
this.quotes = data.quote;
this.quote = data.quote;
this.author = data.author;
this.tags = data.tags;
}
get JSON() {
return JSON.stringify({
quotes: this.quotes,
quote: this.quote,
author: this.author,
tags: this.tags,
});
}
get XML() {
return xml({
quotes: this.quotes,
author: this.author,
tags: this.tags,
});
const res = new Base.Response('Quote');
res
.add(new Base.SimpleObj('quote', this.quote))
.add(new Base.SimpleObj('author', this.author))
.add(new Base.SimpleObj('tags', this.tags.join(', ')));
return xml(res.XML);
}
get YAML() {
return yaml.stringify({
quotes: this.quotes,
quote: this.quote,
author: this.author,
tags: this.tags,
});
Expand Down
Loading

0 comments on commit b33a1ba

Please sign in to comment.