I am currently working with the following interface:
import * as Bluebird from "bluebird";
import { Article } from '../../Domain/Article/Article';
export interface ITextParsingService {
parsedArticle : Article;
getText(uri : string) : Bluebird<any>;
}
In addition, I have a class that implements this interface:
import * as Bluebird from 'bluebird';
import * as requestPromise from 'request-promise';
import ApplicationConfiguration from '../../../../config/ApplicationConfiguration';
import { ITextParsingService } from '../ITextParsingService';
import { Article } from '../../../Domain/Article/Article';
export class DiffbotTextParsingService implements ITextParsingService {
public parsedArticle: Article;
private articleApiEndpoint;
constructor() {
this.articleApiEndpoint = "https://api.diffbot.com/v3/article";
}
public getText(url: string) : Bluebird<any> {
return requestPromise(this.articleApiEndpoint, {
qs : {
"url" : url,
"token" : ApplicationConfiguration.Diffbot.developerToken
},
headers : {
'User-Agent' : 'Request-Promise'
},
json : true
}).then((diffbotResponse) => {
this.mapReponseToArticle(diffbotResponse.objects[0]);
}).catch((errorMessage) => {
return errorMessage;
})
}
private mapReponseToArticle(response : any) {
this.parsedArticle = new Article(response.diffbotUri,
response.title,
response.author,
new URL(response.authorUrl),
response.text,
new URL(response.pageUrl),
new Date(response.date),
response.humanLanguage);
}
}
I am looking for a way to require all classes that implement ITextParsingService
to also include the mapResponseToArticle
method, which is responsible for mapping service responses to a common domain object. While I believe this method should not be public, I am unsure how to enforce this requirement in implementing classes. Are there any suggestions for an alternative pattern to achieve this?