ojp-sdk
Version:
OJP (Open Journey Planner) Javascript SDK
88 lines (87 loc) • 4.09 kB
JavaScript
import { EMPTY_API_CONFIG } from '../../types/stage-config';
import { OJPBaseRequest } from '../base-request';
import { StopEventRequestParser } from './stop-event-request-parser';
import { OJP_Helpers } from '../../helpers/ojp-helpers';
export class StopEventRequest extends OJPBaseRequest {
constructor(stageConfig, language, stopPlaceRef, geoPosition, stopEventType, stopEventDate) {
super(stageConfig, language);
this.stopPlaceRef = stopPlaceRef;
this.geoPosition = geoPosition;
this.depArrTime = stopEventDate;
this.numberOfResults = 10;
this.stopEventType = stopEventType;
this.includePreviousCalls = true;
this.includeOnwardCalls = true;
this.includeRealtimeData = true;
this.enableExtensions = true;
}
static Empty(stageConfig = EMPTY_API_CONFIG) {
const request = new StopEventRequest(stageConfig, 'en', null, null, 'departure', new Date());
return request;
}
static initWithMock(mockText) {
const request = StopEventRequest.Empty();
request.mockResponseXML = mockText;
return request;
}
static initWithRequestMock(mockText, stageConfig = EMPTY_API_CONFIG) {
const request = StopEventRequest.Empty(stageConfig);
request.mockRequestXML = mockText;
return request;
}
static initWithStopPlaceRef(stageConfig, language, stopPlaceRef, stopEventType, stopEventDate) {
const stopEventRequest = new StopEventRequest(stageConfig, language, stopPlaceRef, null, stopEventType, stopEventDate);
return stopEventRequest;
}
buildRequestNode() {
super.buildRequestNode();
const dateNowF = new Date().toISOString();
const dateF = this.depArrTime.toISOString();
this.serviceRequestNode.ele('RequestTimestamp', dateNowF);
this.serviceRequestNode.ele("RequestorRef", OJP_Helpers.buildRequestorRef());
const requestNode = this.serviceRequestNode.ele('ojp:OJPStopEventRequest');
requestNode.ele('RequestTimestamp', dateNowF);
const locationNode = requestNode.ele('ojp:Location');
if (this.stopPlaceRef) {
const requestPlaceRefNode = locationNode.ele('ojp:PlaceRef');
requestPlaceRefNode.ele('ojp:StopPlaceRef', this.stopPlaceRef);
requestPlaceRefNode.ele('ojp:LocationName').ele('Text', '');
}
locationNode.ele('ojp:DepArrTime', dateF);
const requestParamsNode = requestNode.ele('ojp:Params');
requestParamsNode.ele('ojp:NumberOfResults', this.numberOfResults);
requestParamsNode.ele('ojp:StopEventType', this.stopEventType);
requestParamsNode.ele('ojp:IncludePreviousCalls', this.includePreviousCalls);
requestParamsNode.ele('ojp:IncludeOnwardCalls', this.includeOnwardCalls);
requestParamsNode.ele('ojp:IncludeRealtimeData', this.includeRealtimeData);
if (this.enableExtensions) {
const extensionsNode = requestNode.ele('Extensions');
extensionsNode.ele('ojp:ParamsExtension').ele('ojp:PrivateModeFilter').ele('ojp:Exclude', 'false');
}
}
async fetchResponse() {
await this.fetchOJPResponse();
const promise = new Promise((resolve) => {
const response = {
stopEvents: [],
message: null,
};
if (this.requestInfo.error !== null || this.requestInfo.responseXML === null) {
response.message = 'ERROR';
resolve(response);
return;
}
const parser = new StopEventRequestParser();
parser.callback = ({ stopEvents, message }) => {
response.stopEvents = stopEvents;
response.message = message;
if (message === 'StopEvent.DONE') {
this.requestInfo.parseDateTime = new Date();
resolve(response);
}
};
parser.parseXML(this.requestInfo.responseXML);
});
return promise;
}
}