Advertisement
Guest User

Zajęcia 11.12

a guest
Dec 11th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This is endpoint serves to generate insults from the API.
  2.  
  3. // The following parameters are optional
  4.  
  5. //     lang - The language of the insult you want. Defaults to English if not provided
  6.  
  7. //     type - The response type. It supports text, XML and JSON. Defaults to plain text if not provided.
  8.  
  9. const API_ENDPOINT = "https://evilinsult.com/generate_insult.php";
  10. const CORS_GATEWAY = "https://cors-anywhere.herokuapp.com/";
  11. class EndpointGenerator {
  12.   constructor(CORS_GATEWAY, API_ENDPOINT) {
  13.     this.CORS_GATEWAY = CORS_GATEWAY;
  14.     this.API_ENDPOINT = API_ENDPOINT;
  15.   }
  16.  
  17.   generate(lang, type) {
  18.     let endpoint = this.CORS_GATEWAY + this.API_ENDPOINT;
  19.     if (lang || type) {
  20.       endpoint += "?";
  21.     }
  22.     if (lang) {
  23.       endpoint += `lang=${lang}`;
  24.     }
  25.     if (lang && type) {
  26.       endpoint += "&";
  27.     }
  28.     if (type) {
  29.       endpoint += `type=${type}`;
  30.     }
  31.     return endpoint;
  32.   }
  33. }
  34.  
  35. class InsultService {
  36.   constructor(
  37.     endpointGenerator,
  38.     jsonInsultParser,
  39.     xmlInsultParser,
  40.     textInsultParser
  41.   ) {
  42.     this.endpointGenerator = endpointGenerator;
  43.     this.parsers = {
  44.       json: jsonInsultParser,
  45.       xml: xmlInsultParser,
  46.       text: textInsultParser
  47.     };
  48.   }
  49.  
  50.   async getInsult(lang, type) {
  51.     try {
  52.       const endpoint = this.endpointGenerator.generate(lang, type);
  53.       const rawInsult = await fetch(endpoint);
  54.       const parser = this.parsers[type];
  55.       const insult = parser
  56.         ? parser.parse(rawInsult)
  57.         : this.parsers.text.parse(rawInsult);
  58.       return insult;
  59.     } catch (error) {
  60.       alert(error);
  61.     }
  62.   }
  63. }
  64.  
  65. class InsultParser {
  66.   parse(rawInsult) {
  67.     throw new Error("Method not implemented");
  68.   }
  69. }
  70.  
  71. class JsonParser extends InsultParser {
  72.  
  73.  
  74.   async parse(rawInsult) {
  75.     try {
  76.       const parsedInsult = await rawInsult.json();
  77.       return parsedInsult;
  78.     } catch (error) {
  79.       throw new Error(`Failed to parse json insult. Error: ${error}`);
  80.     }
  81.   }
  82. }
  83.  
  84. // class XmlParser extends InsultParser {
  85. //   constructor() {
  86. //     super();
  87. //   }
  88. //   async parse(rawInsult) {
  89. //     try {
  90. //       const responseText = await rawInsult.text();
  91. //       const nativeParser = new window.DOMParser();
  92. //       const documentInsult = nativeParser.parseFromString(
  93. //         responseText,
  94. //         "text/xml"
  95. //       );
  96. //       const parsedInsult = {
  97. //         number: documentInsult.getElementsByTagName("number")[0].childNodes[0]
  98. //           .nodeValue,
  99. //         language: documentInsult.getElementsByTagName("language")[0].childNodes[0]
  100. //           .nodeValue,
  101. //         insult: documentInsult.getElementsByTagName("insult")[0].childNodes[0]
  102. //           .nodeValue,
  103. //         created: documentInsult.getElementsByTagName("created")[0].childNodes[0]
  104. //           .nodeValue,
  105. //         shown: documentInsult.getElementsByTagName("shown")[0].childNodes[0]
  106. //           .nodeValue,
  107. //         createdby: documentInsult.getElementsByTagName("createdby")[0]
  108. //           .childNodes[0].nodeValue,
  109. //         active: documentInsult.getElementsByTagName("active")[0].childNodes[0]
  110. //           .nodeValue,
  111. //         comment: documentInsult.getElementsByTagName("comment")[0].childNodes[0]
  112. //           .nodeValue
  113. //       };
  114. //       return parsedInsult;
  115. //     } catch (error) {
  116. //       throw new Error(`Failed to parse xml insult. Error: ${error}`);
  117. //     }
  118. //   }
  119. // }
  120.  
  121. class XmlParser extends InsultParser {
  122.  
  123. }
  124.  
  125. class TextParser extends InsultParser {
  126.  
  127.   async parse(rawInsult) {
  128.     try {
  129.       const insult = await rawInsult.text();
  130.       const parsedInsult = {
  131.         insult: insult
  132.       };
  133.       return parsedInsult;
  134.     } catch (error) {
  135.       throw new Error(`Failed to parse json insult. Error: ${error}`);
  136.     }
  137.   }
  138. }
  139.  
  140. class EventHandler {
  141.   constructor(insultService, domHandler) {
  142.     this.insultService = insultService;
  143.     this.domHandler = domHandler;
  144.   }
  145.  
  146.   async handleGetInsultButton(ev) {
  147.     const lang = document.getElementById("lang").value || "en";
  148.     const type = document.getElementById("type").value || "json";
  149.     const insult = await this.insultService.getInsult(lang, type);
  150.     if (insult) {
  151.       this.domHandler.handleNewInsult(insult);
  152.     }
  153.   }
  154. }
  155.  
  156. class DOMHandler {
  157.   constructor() {
  158.     this.insults = [];
  159.   }
  160.  
  161.   handleNewInsult(newInsult) {
  162.     const existingInsult = this.insults.find(
  163.       insult => insult.insult === newInsult.insult
  164.     );
  165.     if (existingInsult) {
  166.       return;
  167.     }
  168.     this.insults.push(newInsult);
  169.     this.updateInsultsDOM();
  170.   }
  171.  
  172.   updateInsultsDOM() {
  173.     const root = document.getElementById("insultsRoot");
  174.     root.innerHTML = "";
  175.     for (const insult of this.insults) {
  176.       const domInsult = this.createDOMInsult(insult);
  177.       root.appendChild(domInsult);
  178.     }
  179.   }
  180.  
  181.   createDOMInsult(insult) {
  182.     const insultCard = document.createElement("div");
  183.     for (const prop of Object.getOwnPropertyNames(insult)) {
  184.       const propNode = document.createElement("div");
  185.       propNode.innerText = `${prop}:${insult[prop]}`;
  186.       insultCard.appendChild(propNode);
  187.     }
  188.     return insultCard;
  189.   }
  190. }
  191.  
  192. const endpointGenerator = new EndpointGenerator(CORS_GATEWAY, API_ENDPOINT);
  193. const jsonInsultParser = new JsonParser();
  194. const xmlInsultParser = new XmlParser();
  195. const textInsultParser = new TextParser();
  196. const insultService = new InsultService(
  197.   endpointGenerator,
  198.   jsonInsultParser,
  199.   xmlInsultParser,
  200.   textInsultParser
  201. );
  202. const domHandler = new DOMHandler();
  203. const eventHandler = new EventHandler(insultService, domHandler);
  204.  
  205. document
  206.   .getElementById("getInsult")
  207.   .addEventListener("click", ev => eventHandler.handleGetInsultButton(ev));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement