Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is endpoint serves to generate insults from the API.
- // The following parameters are optional
- // lang - The language of the insult you want. Defaults to English if not provided
- // type - The response type. It supports text, XML and JSON. Defaults to plain text if not provided.
- const API_ENDPOINT = "https://evilinsult.com/generate_insult.php";
- const CORS_GATEWAY = "https://cors-anywhere.herokuapp.com/";
- class EndpointGenerator {
- constructor(CORS_GATEWAY, API_ENDPOINT) {
- this.CORS_GATEWAY = CORS_GATEWAY;
- this.API_ENDPOINT = API_ENDPOINT;
- }
- generate(lang, type) {
- let endpoint = this.CORS_GATEWAY + this.API_ENDPOINT;
- if (lang || type) {
- endpoint += "?";
- }
- if (lang) {
- endpoint += `lang=${lang}`;
- }
- if (lang && type) {
- endpoint += "&";
- }
- if (type) {
- endpoint += `type=${type}`;
- }
- return endpoint;
- }
- }
- class InsultService {
- constructor(
- endpointGenerator,
- jsonInsultParser,
- xmlInsultParser,
- textInsultParser
- ) {
- this.endpointGenerator = endpointGenerator;
- this.parsers = {
- json: jsonInsultParser,
- xml: xmlInsultParser,
- text: textInsultParser
- };
- }
- async getInsult(lang, type) {
- try {
- const endpoint = this.endpointGenerator.generate(lang, type);
- const rawInsult = await fetch(endpoint);
- const parser = this.parsers[type];
- const insult = parser
- ? parser.parse(rawInsult)
- : this.parsers.text.parse(rawInsult);
- return insult;
- } catch (error) {
- alert(error);
- }
- }
- }
- class InsultParser {
- parse(rawInsult) {
- throw new Error("Method not implemented");
- }
- }
- class JsonParser extends InsultParser {
- async parse(rawInsult) {
- try {
- const parsedInsult = await rawInsult.json();
- return parsedInsult;
- } catch (error) {
- throw new Error(`Failed to parse json insult. Error: ${error}`);
- }
- }
- }
- // class XmlParser extends InsultParser {
- // constructor() {
- // super();
- // }
- // async parse(rawInsult) {
- // try {
- // const responseText = await rawInsult.text();
- // const nativeParser = new window.DOMParser();
- // const documentInsult = nativeParser.parseFromString(
- // responseText,
- // "text/xml"
- // );
- // const parsedInsult = {
- // number: documentInsult.getElementsByTagName("number")[0].childNodes[0]
- // .nodeValue,
- // language: documentInsult.getElementsByTagName("language")[0].childNodes[0]
- // .nodeValue,
- // insult: documentInsult.getElementsByTagName("insult")[0].childNodes[0]
- // .nodeValue,
- // created: documentInsult.getElementsByTagName("created")[0].childNodes[0]
- // .nodeValue,
- // shown: documentInsult.getElementsByTagName("shown")[0].childNodes[0]
- // .nodeValue,
- // createdby: documentInsult.getElementsByTagName("createdby")[0]
- // .childNodes[0].nodeValue,
- // active: documentInsult.getElementsByTagName("active")[0].childNodes[0]
- // .nodeValue,
- // comment: documentInsult.getElementsByTagName("comment")[0].childNodes[0]
- // .nodeValue
- // };
- // return parsedInsult;
- // } catch (error) {
- // throw new Error(`Failed to parse xml insult. Error: ${error}`);
- // }
- // }
- // }
- class XmlParser extends InsultParser {
- }
- class TextParser extends InsultParser {
- async parse(rawInsult) {
- try {
- const insult = await rawInsult.text();
- const parsedInsult = {
- insult: insult
- };
- return parsedInsult;
- } catch (error) {
- throw new Error(`Failed to parse json insult. Error: ${error}`);
- }
- }
- }
- class EventHandler {
- constructor(insultService, domHandler) {
- this.insultService = insultService;
- this.domHandler = domHandler;
- }
- async handleGetInsultButton(ev) {
- const lang = document.getElementById("lang").value || "en";
- const type = document.getElementById("type").value || "json";
- const insult = await this.insultService.getInsult(lang, type);
- if (insult) {
- this.domHandler.handleNewInsult(insult);
- }
- }
- }
- class DOMHandler {
- constructor() {
- this.insults = [];
- }
- handleNewInsult(newInsult) {
- const existingInsult = this.insults.find(
- insult => insult.insult === newInsult.insult
- );
- if (existingInsult) {
- return;
- }
- this.insults.push(newInsult);
- this.updateInsultsDOM();
- }
- updateInsultsDOM() {
- const root = document.getElementById("insultsRoot");
- root.innerHTML = "";
- for (const insult of this.insults) {
- const domInsult = this.createDOMInsult(insult);
- root.appendChild(domInsult);
- }
- }
- createDOMInsult(insult) {
- const insultCard = document.createElement("div");
- for (const prop of Object.getOwnPropertyNames(insult)) {
- const propNode = document.createElement("div");
- propNode.innerText = `${prop}:${insult[prop]}`;
- insultCard.appendChild(propNode);
- }
- return insultCard;
- }
- }
- const endpointGenerator = new EndpointGenerator(CORS_GATEWAY, API_ENDPOINT);
- const jsonInsultParser = new JsonParser();
- const xmlInsultParser = new XmlParser();
- const textInsultParser = new TextParser();
- const insultService = new InsultService(
- endpointGenerator,
- jsonInsultParser,
- xmlInsultParser,
- textInsultParser
- );
- const domHandler = new DOMHandler();
- const eventHandler = new EventHandler(insultService, domHandler);
- document
- .getElementById("getInsult")
- .addEventListener("click", ev => eventHandler.handleGetInsultButton(ev));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement