PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/js/animal.js

https://gitlab.com/HonestidaMordaz/pet-store
JavaScript | 138 lines | 127 code | 10 blank | 1 comment | 13 complexity | dfb2ea63e716a4f94e2543b71e2ff8ff MD5 | raw file
  1// Generated by CoffeeScript 1.9.3
  2(function() {
  3  window.Animal = (function() {
  4    function Animal() {}
  5
  6    Animal.CATEGORIES = ["All", "Dog", "Cat", "Rabbit", "Horse"];
  7
  8    Animal.prototype.behaviors = function() {
  9      switch (this.type) {
 10        case "cat":
 11          return ["meow", null];
 12        case "dog":
 13          return ["bark", "wag"];
 14        case "rabbit":
 15          return [null, "hop hop"];
 16        case "bird":
 17          return ["chirp", "flap"];
 18        case "horse":
 19        case "donkey":
 20          return ["neigh", null];
 21        default:
 22          return [null, null];
 23      }
 24    };
 25
 26    Animal.prototype.matchesFilter = function(criteria) {
 27      if (criteria == null) {
 28        criteria = 'All';
 29      }
 30      return criteria === "All" || criteria.toLowerCase() === this.type;
 31    };
 32
 33    Animal.prototype.fetchBreedInfo = function(callback) {
 34      return reqwest({
 35        url: "https://api.duckduckgo.com/",
 36        data: {
 37          q: this.breed,
 38          format: "json",
 39          t: "CoffeeScriptBook"
 40        },
 41        type: "jsonp",
 42        success: (function(_this) {
 43          return function(response) {
 44            var topic, topics;
 45            if (response.Abstract) {
 46              _this.breedInfo = {
 47                description: response.Abstract,
 48                source: response.AbstractSource,
 49                url: response.AbstractURL
 50              };
 51            }
 52            callback();
 53            topics = (function() {
 54              var i, len, ref, results;
 55              ref = response.RelatedTopics;
 56              results = [];
 57              for (i = 0, len = ref.length; i < len; i++) {
 58                topic = ref[i];
 59                if (topic.FirstURL != null) {
 60                  results.push(topic.FirstURL);
 61                }
 62              }
 63              return results;
 64            })();
 65            return _this.fetchExtraLinks(topics, callback);
 66          };
 67        })(this)
 68      });
 69    };
 70
 71    Animal.prototype.fetchExtraLinks = function(topics, callback) {
 72      var expected, i, len, results, topic;
 73      this.extraLinks = {};
 74      expected = topics.length;
 75      results = [];
 76      for (i = 0, len = topics.length; i < len; i++) {
 77        topic = topics[i];
 78        results.push((function(_this) {
 79          return function(topic) {
 80            return reqwest({
 81              url: topic,
 82              data: {
 83                format: "json"
 84              },
 85              type: "jsonp",
 86              success: function(response) {
 87                if (response.Heading) {
 88                  _this.extraLinks[response.Heading] = topic;
 89                  expected -= 1;
 90                  if (expected === 0) {
 91                    return callback();
 92                  }
 93                }
 94              }
 95            });
 96          };
 97        })(this)(topic));
 98      }
 99      return results;
100    };
101
102    Animal.fromHash = function(data) {
103      var animal, key, val;
104      animal = new this;
105      for (key in data) {
106        val = data[key];
107        animal[key] = val;
108      }
109      return animal;
110    };
111
112    Animal.loadSeedData = function(callback) {
113      return reqwest({
114        url: "/pets",
115        type: "json",
116        success: (function(_this) {
117          return function(response) {
118            var animal, animals;
119            animals = (function() {
120              var i, len, results;
121              results = [];
122              for (i = 0, len = response.length; i < len; i++) {
123                animal = response[i];
124                results.push(this.fromHash(animal));
125              }
126              return results;
127            }).call(_this);
128            return callback(animals);
129          };
130        })(this)
131      });
132    };
133
134    return Animal;
135
136  })();
137
138}).call(this);