{"id":3810,"date":"2023-07-18T02:14:12","date_gmt":"2023-07-18T02:14:12","guid":{"rendered":"https:\/\/nmi.cool\/advweb\/?page_id=3810"},"modified":"2025-11-10T00:42:42","modified_gmt":"2025-11-10T00:42:42","slug":"exercise-cats","status":"publish","type":"page","link":"https:\/\/nmi.cool\/advweb\/unit-three-javascript-frameworks\/week-nine\/exercise-cats\/","title":{"rendered":"Exercise: Cats"},"content":{"rendered":"\n<p>Now that we have worked our way through the dog API, it&#8217;s time to take on a more complicated animal (and api). The cat api (catapi.com), like the dog api, has lots of images but also offers additional information as the screen capture at right indicates. In this exercise we are going to work through the process of getting at that information and, of course, lots of cute and weird kitty images!<\/p>\n\n\n\n<p>To begin our feline journey,\u00a0<a href=\"https:\/\/api.thecatapi.com\/v1\/breeds\">click this link<\/a>\u00a0to see the initial information set we will be drawing from.\u00a0 This url brings up a page with detailed information about 67 breeds of domestic cats in one great big array.\u00a0<\/p>\n\n\n\n<p>Download\u00a0<a href=\"https:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/11\/Cat-Start.zip\" data-type=\"attachment\" data-id=\"4731\">Cat-Start.zip<\/a>, unzip it and move the contents into wherever you are doing the work for this class. Open both files with your editor and take a look at the code.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>As you can see, the code in kitty.js is pretty simple so far. Basically, all we are doing is using axios to pull data from https:\/\/api.thecatapi.com\/v1\/breeds, the url that you just visited.\u00a0For starters, let&#8217;s get some of that information to display on our html page.<\/li>\n\n\n\n<li>Create an empty array variable in your data named catArray.&nbsp; Then use the new catArray to capture the data from response as shown below. <img decoding=\"async\" src=\"https:\/\/apweb.quest\/wp-content\/uploads\/2019\/11\/img_5dc5a13e0a4a0.png\" alt=\"\"> <pre class=\"wp-block-code\"><code>data: {\n        catArray: []\n      },<\/code><\/pre><\/li>\n\n\n\n<li>Use a v-for loop to show the name of each cat breed. You should see a long list of names running from Abyssinian to York Chocolate. <pre class=\"wp-block-code\"><code>&lt;li v-for=\"eachCat in catArray\"&gt;{{ eachCat.name }}&lt;\/li&gt;<\/code><\/pre><\/li>\n\n\n\n<li>Our next job is to get the URL of an image for a given cat breed. If you take a careful look at the json feed, you should see a value at the bottom of each object named&nbsp;<strong>reference_image_id.&nbsp;<\/strong>&nbsp;Well, it just so happens that the CatAPI people have a unique image for each breed at tied to this URL:&nbsp;<strong>https:\/\/cdn2.thecatapi.com\/images\/<br><\/strong>For example, if you visit the url&nbsp;<strong>https:\/\/cdn2.thecatapi.com\/images\/ozEvzdVM-.jpg<br><\/strong>you will see an example of how this URL combined with a reference it, plus .jpg at the end brings up one of those images! So, add two new variables to catArray: <pre class=\"wp-block-code\"><code>dpart1: \"https:\/\/cdn2.thecatapi.com\/images\/\",\ndpart2: \".jpg\"<\/code><\/pre><\/li>\n\n\n\n<li>Add an image tag inside the v-for loop: <pre class=\"wp-block-code\"><code>&lt;img :src=\"dpart1 + eachCat.reference_image_id + dpart2\"&gt;<\/code><\/pre><\/li>\n\n\n\n<li>At this point, names and 67 images should appear but some of those images are quite big, so let&#8217;s add a bit of CSS.<pre class=\"wp-block-code\"><code>&lt;style&gt;\n    body {\n      font-family: system-ui, sans-serif;\n      margin: 20px;\n    }\n    \n    img {\n      width: 100%;\n      max-width: 400px;\n      border-radius: 10px;\n      margin-top: 10px;\n      display: block;\n    }\n  &lt;\/style&gt;<\/code><\/pre> <\/li>\n\n\n\n<li>Now that you know how to bring in names, bring in at least three additional sets of information such as temperament, description, etc. Here is one example: <pre class=\"wp-block-code\"><code> &lt;p&gt;&lt;strong&gt;Origin:&lt;\/strong&gt; {{ eachCat.origin }}&lt;\/p&gt;<\/code><\/pre> <\/li>\n\n\n\n<li>Notice as well that some of those characteristics are tied to numbers. Give some thought to how you would use one or two of those numerical values to make (for example) a different sized bar chart appear for each cat breed. Or make the text color change if the cat is not Child Friendly. For example: <pre class=\"wp-block-code\"><code>&lt;p :class=\"{ 'warn': eachCat.child_friendly &lt;= 2 }\"&gt;\n  &lt;strong&gt;Child Friendly:&lt;\/strong&gt; {{ eachCat.child_friendly }}\/5\n&lt;\/p&gt;<\/code><\/pre><\/li>\n\n\n\n<li>And then add an indicator to CSS: <pre class=\"wp-block-code\"><code>.warn { color:#b91c1c; font-weight:600; }\n<\/code><\/pre><\/li>\n\n\n\n<li>Finally, format your page! Use flex or some other method to create a clean layout.<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that we have worked our way through the dog API, it&#8217;s time to take on a more complicated animal (and api). The cat api (catapi.com), like the dog api, has lots of images but also offers additional information as the screen capture at right indicates. In this exercise we are going to work through &hellip; <a href=\"https:\/\/nmi.cool\/advweb\/unit-three-javascript-frameworks\/week-nine\/exercise-cats\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exercise: Cats<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3700,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3810","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/comments?post=3810"}],"version-history":[{"count":14,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3810\/revisions"}],"predecessor-version":[{"id":4742,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3810\/revisions\/4742"}],"up":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3700"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/media?parent=3810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}