Browse Source

Refactoring

pull/6129/head
Thomas Kaul 1 month ago
parent
commit
2d250e6c76
  1. 22
      apps/api/src/helper/object.helper.spec.ts
  2. 11
      apps/api/src/helper/object.helper.ts
  3. 29
      apps/api/src/services/data-provider/manual/manual.service.spec.ts
  4. 22
      apps/api/src/services/data-provider/manual/manual.service.ts

22
apps/api/src/helper/object.helper.spec.ts

@ -1,4 +1,24 @@
import { redactAttributes } from './object.helper'; import { query, redactAttributes } from './object.helper';
describe('query', () => {
it('should get market price from stock API response', () => {
const object = {
currency: 'USD',
market: {
previousClose: 273.04,
price: 271.86
},
symbol: 'AAPL'
};
const result = query({
object,
pathExpression: '$.market.price'
})[0];
expect(result).toBe(271.86);
});
});
describe('redactAttributes', () => { describe('redactAttributes', () => {
it('should redact provided attributes', () => { it('should redact provided attributes', () => {

11
apps/api/src/helper/object.helper.ts

@ -1,4 +1,5 @@
import { Big } from 'big.js'; import { Big } from 'big.js';
import jsonpath from 'jsonpath';
import { cloneDeep, isArray, isObject } from 'lodash'; import { cloneDeep, isArray, isObject } from 'lodash';
export function hasNotDefinedValuesInObject(aObject: Object): boolean { export function hasNotDefinedValuesInObject(aObject: Object): boolean {
@ -31,6 +32,16 @@ export function nullifyValuesInObjects<T>(aObjects: T[], keys: string[]): T[] {
}); });
} }
export function query({
object,
pathExpression
}: {
object: object;
pathExpression: string;
}) {
return jsonpath.query(object, pathExpression);
}
export function redactAttributes({ export function redactAttributes({
isFirstRun = true, isFirstRun = true,
object, object,

29
apps/api/src/services/data-provider/manual/manual.service.spec.ts

@ -1,29 +0,0 @@
import { ManualService } from './manual.service';
describe('ManualService', () => {
let manualService: ManualService;
beforeEach(() => {
manualService = new ManualService(null, null, null);
});
describe('extractValueFromJson', () => {
it('should extract market price from stock API response', () => {
const data = {
currency: 'USD',
market: {
previousClose: 273.04,
price: 271.86
},
symbol: 'AAPL'
};
const result = manualService.extractValueFromJson({
data,
pathExpression: '$.market.price'
});
expect(result).toBe('271.86');
});
});
});

22
apps/api/src/services/data-provider/manual/manual.service.ts

@ -1,3 +1,4 @@
import { query } from '@ghostfolio/api/helper/object.helper';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { import {
DataProviderInterface, DataProviderInterface,
@ -26,7 +27,6 @@ import { Injectable, Logger } from '@nestjs/common';
import { DataSource, SymbolProfile } from '@prisma/client'; import { DataSource, SymbolProfile } from '@prisma/client';
import * as cheerio from 'cheerio'; import * as cheerio from 'cheerio';
import { addDays, format, isBefore } from 'date-fns'; import { addDays, format, isBefore } from 'date-fns';
import jsonpath from 'jsonpath';
@Injectable() @Injectable()
export class ManualService implements DataProviderInterface { export class ManualService implements DataProviderInterface {
@ -40,16 +40,6 @@ export class ManualService implements DataProviderInterface {
return true; return true;
} }
public extractValueFromJson({
data,
pathExpression
}: {
data: object;
pathExpression: string;
}) {
return String(jsonpath.query(data, pathExpression)[0]);
}
public async getAssetProfile({ public async getAssetProfile({
symbol symbol
}: GetAssetProfileParams): Promise<Partial<SymbolProfile>> { }: GetAssetProfileParams): Promise<Partial<SymbolProfile>> {
@ -296,12 +286,14 @@ export class ManualService implements DataProviderInterface {
let value: string; let value: string;
if (response.headers.get('content-type')?.includes('application/json')) { if (response.headers.get('content-type')?.includes('application/json')) {
const data = await response.json(); const object = await response.json();
value = this.extractValueFromJson({ value = String(
data, query({
object,
pathExpression: scraperConfiguration.selector pathExpression: scraperConfiguration.selector
}); })[0]
);
} else { } else {
const $ = cheerio.load(await response.text()); const $ = cheerio.load(await response.text());

Loading…
Cancel
Save