Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class DealStat {
  2.     constructor(plan, fact, percentage) {
  3.         this.plan = plan;
  4.         this.fact = fact;
  5.         this.percentage = percentage;
  6.     }
  7. }
  8.  
  9. class RateStat {
  10.     constructor(likes, dislikes) {
  11.         this.like_count = likes;
  12.         this.dislike_count = dislikes;
  13.     }
  14. }
  15.  
  16. class TaskStat {
  17.     constructor(opened, closed, failed) {
  18.         this.opened = opened;
  19.         this.closed = closed;
  20.         this.failed = failed;
  21.     }
  22. }
  23.  
  24. class StarStat {
  25.     constructor(number, deals, rate, tasks) {
  26.         this.number = number;
  27.         this.deals = new DealStat(deals.plan, deals.fact, deals.percentage);
  28.         this.rates = new RateStat(rate.likes, rate.dislikes);
  29.         this.tasks = new TaskStat(tasks.opened, tasks.closed, tasks.failed);
  30.     }
  31. }
  32.  
  33. function get(url) {
  34.     return new Promise(function (resolve, reject) {
  35.         var req = new XMLHttpRequest();
  36.         req.open('GET', url);
  37.  
  38.         req.onload = function () {
  39.             if (req.status == 200) {
  40.                 resolve(req.response);
  41.             }
  42.             else {
  43.                 reject(Error(req.statusText));
  44.             }
  45.         };
  46.  
  47.         req.onerror = function () {
  48.             reject(Error("Network Error"));
  49.         };
  50.  
  51.         req.send();
  52.     });
  53. }
  54.  
  55. get('http://127.0.0.1:8080/stat').then(function (response) {
  56.     let res = JSON.parse(response);
  57.     let starStats = [];
  58.     for (let starNum in res) {
  59.         if (res.hasOwnProperty(starNum)){
  60.             let starData = res[starNum];
  61.             let starStat = new StarStat(starNum, starData.deals, starData.rate, starData.tasks);
  62.             starStats.push(starStat)
  63.         }
  64.     }
  65.     console.log(starStats);
  66. }, function (error) {
  67.     console.error("Failed!", error);
  68. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement