<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kevin Gleeson on Medium]]></title>
        <description><![CDATA[Stories by Kevin Gleeson on Medium]]></description>
        <link>https://medium.com/@teapuddles?source=rss-6bec036b5524------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8rK1ElTCYoNcaYE69fTKyg@2x.jpeg</url>
            <title>Stories by Kevin Gleeson on Medium</title>
            <link>https://medium.com/@teapuddles?source=rss-6bec036b5524------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 06:22:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@teapuddles/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Re-Seed Your Heroku Database Everyday With One-Off Dynos]]></title>
            <link>https://medium.com/geekculture/re-seed-your-heroku-database-everyday-with-one-off-dynos-a77822052086?source=rss-6bec036b5524------2</link>
            <guid isPermaLink="false">https://medium.com/p/a77822052086</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[heroku-scheduler]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Kevin Gleeson]]></dc:creator>
            <pubDate>Tue, 15 Jun 2021 21:18:16 GMT</pubDate>
            <atom:updated>2021-06-16T12:59:40.965Z</atom:updated>
            <content:encoded><![CDATA[<p>In my <a href="https://teapuddles.medium.com/seeding-a-rails-backend-with-an-external-api-1eb192271005">last blog</a>, I went into how you can seed your database from an external API. This is great for grabbing information from a source other than your own backend and is a pretty essential tool for building a lot of projects. However, what happens once you’ve pushed your new project onto Heroku? What if the information you seeded is constantly updating (stocks, weekly sales, etc)? Do you plan on resetting your database every single day so your site information stays current? I mean, sure you could, but in the grand scheme of things this is going to spell out trouble for you and your project. Enter Heroku dynos and one-off dynos.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FQKO8gxZKCZ0v6%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FQKO8gxZKCZ0v6%2Fsource.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FQKO8gxZKCZ0v6%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="212" frameborder="0" scrolling="no"><a href="https://medium.com/media/0665299c5d67d9ddfd3558a516590270/href">https://medium.com/media/0665299c5d67d9ddfd3558a516590270/href</a></iframe><blockquote>“Dynos are isolated, virtualized Linux containers that are designed to execute code based on a user-specified command.” -<a href="https://www.heroku.com/dynos#:~:text=Dynos%3A%20the%20heart%20of%20the,simplify%20development%20and%20enhance%20productivity.&amp;text=The%20containers%20used%20at%20Heroku,on%20a%20user%2Dspecified%20command.">Heroku</a></blockquote><p>As the above quote mentions, dynos are containers filled with code that execute on a user-specified command. Meaning that for code bases of any size, you can store code that will simplify workflow and enhance productivity.</p><p>A <a href="https://devcenter.heroku.com/articles/one-off-dynos">one-off dyno</a> is executable code that we can use to manage all of our administrative and maintenance tasks. Seeding our database falls into this category, but what else does? According to the Heroku website, one-off dynos can be used in the following ways:</p><ul><li>Initializing databases or running database migrations. (e.g. rake db:migrate or node migrate.js migrate)</li><li>Running a console or a REPL shell to run arbitrary code or inspect the app’s models against the live database. (e.g. rails console, irb, or node)</li><li>One-time scripts committed into the app’s repo (e.g. ruby scripts/fix_bad_records.rb or node tally_results.js).</li></ul><p>For this guide, we’re going to be re-seeding our database, which falls under the first category. Also, like the previous guide, I’ll be using Ruby on Rails. Before we jump in, first make sure you’ve done the following.</p><ul><li>Attached your backend to a Heroku site.</li><li>Installed the gem ‘<a href="https://github.com/DatabaseCleaner/database_cleaner">database-cleaner</a>’</li><li>Install the Heroku Scheduler on your app (Very easy to do! Just follow the docs linked <a href="https://devcenter.heroku.com/articles/scheduler">here</a>. Once you have it set up, don’t worry about adding anything yet, we’ll get to it.)</li></ul><p>Now that we have all of our pieces together, it’s time to build a one-off dyno! All we need to do to get started is to build a rake task. To do this, create a file called scheduler.rakein /app/db/lib/tasks. Once there, you can take a look at the following code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aYDo7OpaRkk5plBGrEKojA.png" /><figcaption>Using ‘datbase-cleaner’, building a rake task that cleans our database is easy!</figcaption></figure><p>In this example, I created a namespace called :scheduled_tasks that will be the title for all of the tasks that we’ll want to schedule into our scheduler. Doing something like this is nice for scalability and legibility.</p><p>After describing what our rake task will do, we’ll create our task called :update_data , which calls our ‘database-cleaner’ gem to clean our database.</p><p>Once the cleaning is done, we need to re-seed our database. Luckily because db:seed is already a known rake task, we can just invoke it with the line Rake::Task[“db:seed”].invoke</p><p>Before moving on, be sure to push your changes up to your Heroku site.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/605/1*LTynKrdLKB8RdafXlesfiQ.png" /></figure><p>Now that our rake task is built out, all we need to do is connect it to our scheduler. If you followed the instructions properly when installing, you should see the scheduler on your Heroku homepage for the website you’re working on. Click on your scheduler and find the option to Add a Job. A Job-Editor window should pop up that looks like this.</p><p>Here is where you’ll add the rake task that we had built to our application. What we’re essentially doing here is telling Heroku to run the rake task we had built at a certain time every day.</p><p>Once this is set up, check your scheduler after the time the rake task was supposed to run. You’ll be able to see when it was last executed.</p><p>I was really surprised that Heroku had something like this built into their site functionality and am really happy that my application is up and running with new daily information.</p><p>If you wanted to check out that project, a Covid-19 Tracker, you can view it <a href="https://covid19trackerfrontend.herokuapp.com/">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a77822052086" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/re-seed-your-heroku-database-everyday-with-one-off-dynos-a77822052086">Re-Seed Your Heroku Database Everyday With One-Off Dynos</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Seeding a Rails backend with an external API.]]></title>
            <link>https://teapuddles.medium.com/seeding-a-rails-backend-with-an-external-api-1eb192271005?source=rss-6bec036b5524------2</link>
            <guid isPermaLink="false">https://medium.com/p/1eb192271005</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[api-integration]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[seeding]]></category>
            <category><![CDATA[rest-client]]></category>
            <dc:creator><![CDATA[Kevin Gleeson]]></dc:creator>
            <pubDate>Sat, 30 Jan 2021 04:16:46 GMT</pubDate>
            <atom:updated>2021-01-30T04:16:46.163Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iMGiVr3tdYFucuS0TdD7mA.jpeg" /><figcaption>That’s it, that’s the blog.</figcaption></figure><p>Seeding is an ever-important task when you’re building out a backend in any language. Since my primary backend language is Ruby, and because Rails is just plain awesome, I want to detail the above picture a bit more and show you how you can seed your database with information from an external api with Ruby on Rails.</p><h3>What you’ll need:</h3><h4>Gems:</h4><p>1. ‘rest-client’ — Our http/rest client gem for accessing the url for our external api via get request.</p><p>2. ‘dotenv-rails’ — Our gem that will help us hide our api key.</p><p>Don’t forget to do a fresh bundle install after adding these to your gemfile.</p><h4>An API Key:</h4><p>For the purpose of this blog we’re going to use the <a href="https://newsapi.org/">NewsAPI</a> to seed some top headline articles about coronavirus (since we know there will be tons of articles to test our seeds with). All you need to do is sign up with them and you can get a personal API key.</p><h3>Hiding your API Key:</h3><p>Once you’ve received your api key, you’ll want to add it to your backend. If you don’t already have a .env file in your root folder, create one.</p><p>Next, you’ll want to name your api key inside that .env file.</p><pre>THE_BEST_NEWS_API_KEY = your_api_key_here</pre><p>After that, you’re ready to move into your seeds.rb file.</p><h3>Your Seed File:</h3><p>Okay, those first few steps have been the set up, and now we’re getting into the nitty-gritty. We can get started by putting require ‘rest-client’ at the top of our seeds. Next, we’ll need to bring in that api key that we just defined in our .env file. The following method should do the trick.</p><pre>def best_news_secret_key<br>    ENV[&quot;THE_BEST_NEWS_API_KEY&quot;]<br>end</pre><p>We’ll need this to be a string so we can put it properly into our URL.</p><p>Now, let’s build out our method that will do the seeding. The numbers added at the beginning of the lines will be detailed below.</p><pre>def news_dataset</pre><pre>    <strong>1.</strong> api_data = { key: best_news_secret_key }</pre><pre>    <strong>2.</strong> news = RestClient.get(&quot;https://newsapi.org/v2/top-headlines?   q=coronavirus&amp;apiKey=#{api_data[:key]}&quot;)</pre><pre>    <strong>3.</strong> news_array = JSON.parse(news)[&quot;articles&quot;]<br>   <strong> 4.</strong> news_array.each do |s|<br>    Story.create(title: s[&quot;title&quot;], author: s[&quot;author&quot;], content:   s[&quot;content&quot;], url: s[&quot;url&quot;])</pre><pre>end<br>end</pre><pre><strong>5. </strong>news_dataset() </pre><ol><li>We create an object with a key that’s set to the method we created in the previous step.</li><li>Here is the fetch to our api. The url I put here is shaped to search top headlines, with a query of coronavirus, and then an interpolation of our api_data object we created in step one. Definitely check out the <a href="https://newsapi.org/docs">NewsAPIs Documentation</a> because they give a ton of great examples on how to build your url.</li><li>We create a variable that is our response, parsed into JSON at the “articles” array. (You might want to console.log/byebug your response here to see how your data is coming through before moving on.)</li><li>Now that you can see the data you’re receiving in your console/terminal, let’s go through that response and take out the article data that we wanted. Here we’re using the .each method to do so.</li><li>Invoke the news_dataset() method so it triggers on rails db:seed.</li></ol><p>If everything works out, you should be able to use rails db:seed and get a whole bunch of article data seeded from the NewsAPI! I hope you found this useful! Happy Planting!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1eb192271005" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Adding a Filter to your Sound with the Web Audio API]]></title>
            <link>https://teapuddles.medium.com/adding-a-filter-to-your-sound-with-the-web-audio-api-be95b816290b?source=rss-6bec036b5524------2</link>
            <guid isPermaLink="false">https://medium.com/p/be95b816290b</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[api-integration]]></category>
            <category><![CDATA[synthesis]]></category>
            <category><![CDATA[filters]]></category>
            <category><![CDATA[web-audio-api]]></category>
            <dc:creator><![CDATA[Kevin Gleeson]]></dc:creator>
            <pubDate>Fri, 22 Jan 2021 22:20:17 GMT</pubDate>
            <atom:updated>2021-01-22T22:20:17.209Z</atom:updated>
            <content:encoded><![CDATA[<p>Filters! They’re awesome! The practical uses of filters can be seen everywhere from a nice pour over coffee from your favorite cafe to the spam filter in your email. Filters also have their place in the world of audio engineering.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FcJntxjFbmM1YQ%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fmrw-idiots-emails-cJntxjFbmM1YQ&amp;image=https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FcJntxjFbmM1YQ%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ad8d8d3897fee0044b13768c8b4057c/href">https://medium.com/media/1ad8d8d3897fee0044b13768c8b4057c/href</a></iframe><p>In my <a href="https://teapuddles.medium.com/creating-your-first-sound-with-the-web-audio-api-3848de87b123">last blog</a>, we went over the basics of the Web Audio API. We explored how audioContext() is essential to creating a sound. Then we connected our context to an oscillator and then to the sounds destination (our speakers). Now that we know that we have a sound, we can start to experiment a little bit with how we shape that sound. We’re going to do that with, you guessed it, a filter!</p><p>Let’s recall where we left off. This chunk of code gives us a sound coming from our oscillator and from there it goes directly to our destination, or our speakers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KglW6Daio-3-QJaJllZglw.png" /><figcaption>Note how our oscillator is directly connecting to our destination.</figcaption></figure><p>I’d like us to imagine a pour over coffee. Think of our filter as both the coffee and the paper coffee filter, and our sound as the hot water. When water gets put through, our filters give the sound more flavor and refinement. When the sound we created above gets passed to a filter, it does essentially the same thing.</p><p>To get started, we’re going to create a filter with this line of code:</p><pre>let filter = context.createBiquadFilter()</pre><p>Next, we want to change where our oscillator is connected to. This way, our sound goes from our context, to our oscillator, to our filter, finishing at our destination.</p><pre>oscillator.connect(filter)</pre><pre>filter.connect(context.destination)</pre><p>Now for the fun part. Once our filter is set up, we can then call it to change the shape of our sound. I would definitely check out the <a href="https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode">MDN documentation</a> on all of the things you can do with filter. For today, we’re just going to use filter.type and filter.frequency.setTargetAtTime().</p><p>Let’s take a look at an example of both.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B1kszjl8I1brAO9QWNuGWA.png" /></figure><p>The MDN documentation goes over the different types of filters you can add to your sound. We’re going to use a lowpass filter for this one. This will limit the frequency of sound we let through to those under a certain threshold. That threshold is defined in the next line down.</p><p>setTargetAtTime() takes in 3 different arguments:</p><ol><li>Target- This is a number representing the target frequency cutoff. This is the threshold I mentioned when describing our lowpass filter. In this example a lowpass filter would only pass through sounds below a frequency of 2000. See how it changes the sound in your browser!</li><li>startTime- This is when the sound will start. We set it to current time so it starts immediately.</li><li>timeConstant- This is an exponential number that represents how quickly it will take to reach the frequency detailed in target. Try putting 0 then 9 in for this and see if you can hear how the sound becomes more punchy. From a musicians standpoint, this feels a lot like the attack of the note.</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F1fWnLuct9n0vXWMjXy%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fhustle-viceland-1fWnLuct9n0vXWMjXy&amp;image=https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2F1fWnLuct9n0vXWMjXy%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5255ffb4b4937511d4b1a4e1e44844a/href">https://medium.com/media/d5255ffb4b4937511d4b1a4e1e44844a/href</a></iframe><p>I hope you like this little example of how you can shape the sounds you produce with your browser. Now that we’ve started shaping our sounds, things are definitely going to get a bit more interesting from here on out. Keep a lookout for my next blog on the Web Audio API!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be95b816290b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating your first sound with the Web Audio API]]></title>
            <link>https://teapuddles.medium.com/creating-your-first-sound-with-the-web-audio-api-3848de87b123?source=rss-6bec036b5524------2</link>
            <guid isPermaLink="false">https://medium.com/p/3848de87b123</guid>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[web-audio-api]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[audio]]></category>
            <dc:creator><![CDATA[Kevin Gleeson]]></dc:creator>
            <pubDate>Sat, 16 Jan 2021 02:29:51 GMT</pubDate>
            <atom:updated>2021-01-16T02:29:51.750Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever since I started to code I’ve wanted to learn about the browser’s Web Audio API. My long term goal is to create a full synthesizer with it, so give me a follow if you want to watch me on that journey.</p><p>I remember during my last few weeks at Flatiron School I was experimenting a bit with the Web Audio API and was able to create a sound in the browser. I want to show you how simple something like this can be.</p><h3>The Web Audio API</h3><p>According to <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API">MDN</a>, the Web Audio API provides a powerful and versatile system for controlling audio on the Web, allowing developers to choose audio sources, add effects to audio, create audio visualizations, apply spatial effects (such as panning) and much more.</p><p>You can easily see how vast of an API this is. However, getting started with it is as simple as a whistle.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FUFpIg6vXgUUXS%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Flauren-bacall-to-have-and-not-UFpIg6vXgUUXS&amp;image=https%3A%2F%2Fmedia2.giphy.com%2Fmedia%2FUFpIg6vXgUUXS%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="241" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f046b65d6a55d7370db5a8bf6916022/href">https://medium.com/media/3f046b65d6a55d7370db5a8bf6916022/href</a></iframe><p>In order for us to create a sound in our web browser, we need the establish the most important part of our Web Audio API, the AudioContext.</p><pre>let context = new AudioContext()</pre><p>In order for us to access the Web Audio API, we need to create a new AudioContext(). This is very similar to creating a new HTML canvas. The next thing we need is to create an Oscillator to create our sound. An oscillator, for the uninitiated, is a circuit which produces a continuous, repeated, alternating waveform without any input. These waveforms are sound waves. I’ll get into those in a later blog.</p><pre>let oscillator = context.createOscillator()</pre><pre>oscillator.connect(context.destination)</pre><p>Here we’re creating our oscillator with the Web Audio API’s createOscillator() method. Once we have it, we must connect it to our Audio Context by sending it to our destination. If you want any sort of sound to exist in your browser, your audio context needs and oscillator and to connect to a destination.</p><pre>oscillator.start()</pre><pre>setTimeout(function(){</pre><pre>oscillator.stop()</pre><pre>}, 5000);</pre><p>Once you have the above code, all you need to do to create your first sound is to call oscillator.start(). If you load your page on the browser now, you should have a sound!</p><p>If you hear that sound, you’re doing great! But that sound is going on, and on, and on…really annoying, right? Well, all we need to do is set a timeout with setTimeout and call oscillator.stop() and add a value of time to this function. Now we have a sound that lasts 5 seconds.</p><p>If your sound didn’t play, you might have seen an error in your console that looked like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*3JDmBXkHo4wOwlfwaaAvOQ.png" /></figure><p>That means you need to create a button or an event to start the sound. Your browser has a security measure in it that makes a user need to do a gesture before any sounds will play using the Web Audio API. So after cleaning that up you’re code could look something like this…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5ZcwHUMCUdUe9HrCmskhHA.png" /></figure><p>Enjoy your sound making button! I’m going to learn more about how to change our sounds and write a new blog soon!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3848de87b123" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Taking Articles out of Ordered Lists with JavaScript (ES6)]]></title>
            <link>https://teapuddles.medium.com/taking-articles-out-of-ordered-lists-with-javascript-es6-68dc423a561e?source=rss-6bec036b5524------2</link>
            <guid isPermaLink="false">https://medium.com/p/68dc423a561e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[replace]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[js30]]></category>
            <category><![CDATA[sort]]></category>
            <dc:creator><![CDATA[Kevin Gleeson]]></dc:creator>
            <pubDate>Sat, 09 Jan 2021 05:03:03 GMT</pubDate>
            <atom:updated>2021-01-09T05:03:03.986Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve been writing a whole lot of Ruby articles recently off the effort I made on a recent take home code challenge. Though I still have a lot of plans for my Ruby/Rails/Backend journey, I wanted to hop back where I left off picking away at Wes Bos’ JavaScript30. The exercise in week 17 of this course was a perfect way to get back into practicing some routine JavaScript.</p><p>In this exercise, we have a list of band names and as most are aware, lots of band names contain either the articles <strong>the</strong> (The Band), <strong>a</strong> (A Flock of Seagulls), or the more rare <strong>an</strong> (An Albatross). For our list, we want to ignore those articles so when mapped to our page they’ll display in order of the word <em>that is not</em> an article. For example, if we were to use the three names above, the order they should appear is:</p><ul><li>An Albatross</li><li>The Band</li><li>A Flock of Seagulls</li></ul><p>Note how A Flock of Seagulls is below The Band. If we don’t ignore the articles, our order would change. This could be really important for the ease of use a user has while sorting something on your website. Now let’s see how we do it!</p><h3>Step 1: Know your methods</h3><p>In this exercise we’re going to use two methods to create our desired list. They are <strong>sort</strong>, and <strong>replace</strong>.</p><ul><li><strong>.sort()</strong> — The .<strong>sort()</strong> method sorts the elements of an array <a href="https://en.wikipedia.org/wiki/In-place_algorithm"><em>in place</em></a> and returns the sorted array (From MDN). It typically compares two things and changes the array based on your desired outcome of the comparison/alphabetization.</li><li><strong>.replace() — </strong>The .<strong>replace()</strong> method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp">RegExp</a>, and the replacement can be a string or a function to be called for each match. If pattern is a string, only the first occurrence will be replaced (From MDN).</li></ul><h3>Step 2: Build out your .sort( )</h3><p>To get started we’re going to build out our .sort() method. Take a look at the code below.</p><pre>const sortedBands = bands.sort((a, b) =&gt; strip(a) &gt; strip(b) ? 1 : -1) </pre><p>This is a single line ternary expression that’s sorting our bands array to a variable sortedBands. The parameters of (a, b) are two of the band names that we are going to compare alphabetically. The strip function will use our replace method, but for now you can know that this function is going to strip our band names of their articles if they have it.</p><p>So if the strip(a) is higher in the alphabet than the strip(b) it will ‘return 1’, in other words, push the band (a) to the front of the array.</p><p>If the strip(b) is higher in the alphabet than strip(a), then the function will ‘return -1&#39;, in other words, push the band (a) to the back of the array. This will give us an accurate an alphabetized list!</p><h3>Step 2.5: Build out your .replace( )</h3><p>I can’t call this step 3 because realistically our .sort() needs our strip function to work properly. So let’s go ahead and see what that line of code reads like.</p><pre>function strip(bandName) {<br>   return bandName.replace(/^(a |the |an)/i, &#39;&#39;).trim()<br>}</pre><p>We create a function that takes in the parameter of a bandName. This matches our a and b comparison in our sort, so we’re on the right track!</p><p>From there, we can continue on the small code hype train and just write an explicit return. In this return, we’re going to use the replace method on our band name. When I see .replace() I think RegEx immediately. For those who are really new to RegEx here’s a breakdown of what’s inside our replace method.</p><ul><li>Regex always takes place between two / /</li><li>When you see the ^ at the beginning of a RegEx, that means that we’re looking at what the passed in value starts with.</li><li>Everything in the parenthesis are the bits we’ll be replacing. Here we wrote down all of the articles we were looking for and separated them with ‘or’ ( | | ).</li><li>The i at the end means that it is insensitive, in other words, not case sensitive and can be written in capital or lowercase.</li></ul><p>Once the RegEx is over, we write what we would like to replace the values we listed with. In this case, we’ll use an empty string (‘ ‘). The last thing our function does is trim away and extra space created by the replacement of the article and we do that with, you guessed it .trim() .</p><p>Put both our sort and replace methods to good use and you can do fun JS things like this on the fly! Don’t forget to check out Wes Bos, too!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68dc423a561e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RVM (Ruby Version Manager) and You!]]></title>
            <link>https://teapuddles.medium.com/rvm-ruby-version-manager-and-you-343150d5d677?source=rss-6bec036b5524------2</link>
            <guid isPermaLink="false">https://medium.com/p/343150d5d677</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[rvm]]></category>
            <category><![CDATA[versioning]]></category>
            <category><![CDATA[1950s]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Kevin Gleeson]]></dc:creator>
            <pubDate>Sat, 02 Jan 2021 02:08:34 GMT</pubDate>
            <atom:updated>2021-01-02T02:08:34.218Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FjMnOL43X27k1G%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fheadphones-frankenstein-meets-the-space-monster-stand-by-jMnOL43X27k1G&amp;image=https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2FjMnOL43X27k1G%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="243" frameborder="0" scrolling="no"><a href="https://medium.com/media/149127d71fec681a080a1c89966167ab/href">https://medium.com/media/149127d71fec681a080a1c89966167ab/href</a></iframe><p>Have you ever started a project with ‘rails new’ and not wanted to use your default version of ruby?</p><p>Moreover, you might have already begun work on a project to later find you need to change versions.</p><p>Now there’s no need to worry about such problems with…RVM! Also known as the Ruby Version Manager.</p><h4>To Start</h4><p>Just like little Timmy needs to start his day with a tall glass of milk, you should probably know which version of Ruby is currently your default version.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F6hVR7eqxn5Xmo%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fboy-drinking-milk-6hVR7eqxn5Xmo&amp;image=https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2F6hVR7eqxn5Xmo%2Fgiphy-downsized-large.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/1655ae1e2728b9c27a38b3dcc46e8557/href">https://medium.com/media/1655ae1e2728b9c27a38b3dcc46e8557/href</a></iframe><p>RVM has the tools for such a task. In your command line, simply type the ‘rvm list’ to see a list of all of the versions of Ruby currently installed on your machine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*0SkROMzjzVzVXPPwFSTV1Q.png" /><figcaption>Here we can see our current and default version of ruby is 2.6.1</figcaption></figure><p>If your list only contains one version of Ruby and it’s not the version you’d like to use, consider the following.</p><h3>For a New Project</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl4FGoYWQLPZWWrrCo%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fvintage-family-l4FGoYWQLPZWWrrCo&amp;image=https%3A%2F%2Fmedia0.giphy.com%2Fmedia%2Fl4FGoYWQLPZWWrrCo%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="320" frameborder="0" scrolling="no"><a href="https://medium.com/media/f771cf6afa13d770f2b5852fc8854973/href">https://medium.com/media/f771cf6afa13d770f2b5852fc8854973/href</a></iframe><p>Don’t be like Doris and plan ahead! Before creating your new application, install the version of Ruby that you would like with the following line of code.</p><pre>rvm install VERSION.NUMBER.HERE</pre><p>Once this process has completed, once you open your RVM list, you should see two versions of Ruby installed on your device.</p><p>From there, to set the version that you’d like to use in your project as the default version, after opening your RVM list simply type the following code.</p><pre>rvm --default use VERSION.NUMBER.HERE</pre><p>Now this way we’ll start our project with the correct version of Ruby.</p><h3>For an Existing Project</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3oEjHNjKt4S9eLnZm0%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fvintage-atomic-atom-bomb-3oEjHNjKt4S9eLnZm0&amp;image=https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2F3oEjHNjKt4S9eLnZm0%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="330" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a068a6c5adf12e1c9bdc6215bd81eb9/href">https://medium.com/media/8a068a6c5adf12e1c9bdc6215bd81eb9/href</a></iframe><p>Looks like the Smith family needs to make a quick change to their Ruby version to safely survive crunch time. Luckily for them, rvm makes this easy!</p><p>If the version you’d like your project to be in is already installed on your computer, you only need to do one thing!</p><pre>rvm use VERSION.NUMBER.HERE</pre><p>If it’s not installed, install it with the ‘rvm install’ command from above and then simply change versions with ‘rvm use’.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=343150d5d677" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing API Endpoints with RSpec on Rails]]></title>
            <link>https://teapuddles.medium.com/testing-api-endpoints-with-rspec-on-rails-7add6e0b243?source=rss-6bec036b5524------2</link>
            <guid isPermaLink="false">https://medium.com/p/7add6e0b243</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[rspec]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[api-development]]></category>
            <category><![CDATA[endpoints]]></category>
            <dc:creator><![CDATA[Kevin Gleeson]]></dc:creator>
            <pubDate>Sat, 26 Dec 2020 02:02:24 GMT</pubDate>
            <atom:updated>2020-12-26T02:02:24.742Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/218/1*8DVk4U9BQhIp6jTFInX21A.png" /></figure><p>In my <a href="https://teapuddles.medium.com/jest-vs-mocha-getting-started-with-testing-7c6deeb8c9de">recent article</a>, I went over the differences between two very popular types of testing- Mocha and Jest. This time around I wanted to touch on RSpec, a testing suite for Ruby. Ruby on Rails is also my preferred backend, so I was more than happy to take on the task of learning a bit about RSpec for a recent interview.</p><p>My challenge was to create an API with custom endpoints and then build RSpec tests for each one of those endpoints. Lucky for me, my recent dive into the world of testing certainly helped, as the flow of logic is similar to what I had experienced while experimenting with Mocha and Jest. Let’s check out how I tested my endpoints using RSpec via the ‘rspec-rails’ gem.</p><h3>Step 1: Start like a seed</h3><p>When I first began my tests, I wanted my test data that was being created to be destroyed in the beginning every time I ran RSpec, just so I didn’t run into any weird database errors. It reminded me a lot of destroying my seed files before running rails db:seed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qfxMSQefHoT3MSNmBhqu2g.png" /><figcaption>I’m not sure if the phone number I used was real or not, so for the sake of privacy I censored it.</figcaption></figure><p>So on Line 3 I’m initializing the RSpec test itself for all of my Device requests. After that, I create a ‘before do’ in my tests that will trigger first. That lets me take all of the information that is stored in my devices and start with a clean slate every time we run our tests.</p><p>On line 8, because we’re going to need to create some Device instances, I’m creating an instance variable for the attributes of a device. We’ll use this later.</p><h3>Step 2: Testing a ‘POST’ Method to register a device</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XkQTWo88hKDjyyFxRRpgYw.png" /></figure><p>Now we’re getting started writing our tests. I’m going to go over two different types of ‘POST’ requests as well as a ‘PATCH’ request. Don’t get confused that these endpoints don’t fully follow CRUD, we just used some custom routes.</p><p>On line 30, we’re describing what our test should do. What should it do? We answer that on line 32 with our it statement. Now we know that this endpoint should do let’s make it happen!</p><p>On line 34, we’re sending our post request with the params necessary to create a device. We can’t use our instance variable yet because we don’t have a device in our database!</p><p>The next few lines are our assertions. We’re expecting our backend to accept that we’re posting a new Device to the database with the attributes we supplied it and return it in JSON format. In addition, we want to know for sure that this request worked by checking if our status code is ‘201’ or ‘created’.</p><h3>Step 3: Testing a ‘PATCH’ Method to update a device attribute</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vIWLTW36joFpDOXBinElUQ.png" /><figcaption>When a device hits this endpoint, it’s disabled_at attribute should no longer be nil.</figcaption></figure><p>Line 43 describes our test, and this time it’s a patch request. In order for our patch request to work, we need to already have a device in our database. That’s why before we do anything else we’re going to create a device in a ‘before do’ statement on line 45. Here is where we’ll use our ‘@device_attributes’ instance variable.</p><p>Now we’ll say what our test should do. We want it to update our disabled_at attribute to the current DateTime which is currently set to nil by default. We do that after our it statement on line 51.</p><p>Line 53 is our patch request, and we’ll send it along with the device_id because that’s how our backend would like it.</p><p>Finally we have our assertions. Here we check to see if our disabled_at attribute is no longer nil and that our status code is ‘202’ or ‘accepted’.</p><h3>Step 4: Testing a ‘POST’ Method to create instances of other models.</h3><p>In this API, we’re using a single controller to access all of our models. This is great for smaller applications, and means that we can keep everything in a single RSpec file while testing them, too!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kuG1o1DBlF4UaCW8CWEf_Q.png" /><figcaption>This endpoint and the final endpoint were very similar, so we just one example for them!</figcaption></figure><p>Because our Models show that a Device has_many :heartbeats, we’ll need to create a Device before we can create a Heartbeat. Lines 62–64 here follow a similar flow of logic to our previous test’s beginning.</p><p>Line 66 describes our test, saying that it should create a new instance of a Heartbeat for our device. Line 68 does our ‘POST’ request, and like before, we send it with necessary params.</p><p>Our assertions on line 70 are a little different than before. That’s because instead of testing against a device attribute or a JSON.response, we just need to see if our heartbeat was successfully created. We do this by asserting that the size of this device’s heartbeats is equal to 1. Remember that we destroy our test database every time we run RSpec, so it should never be more than 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_FAJZOayFaxsMcbCmjRQKw.png" /><figcaption>I didn’t show all of the tests, but believe that this is mine! haha</figcaption></figure><p>I think I did a pretty good job for my first time really diving into RSpec. I definitely recommend also testing your api with something like Postman so you can be extra sure that your endpoints work! Looking forward to getting better and learning more! Next step, error handling in my tests!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7add6e0b243" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Gem Files: ‘phonelib’]]></title>
            <link>https://teapuddles.medium.com/the-gem-files-phonelib-c738a08a26b0?source=rss-6bec036b5524------2</link>
            <guid isPermaLink="false">https://medium.com/p/c738a08a26b0</guid>
            <category><![CDATA[phone-number]]></category>
            <category><![CDATA[formatting]]></category>
            <category><![CDATA[rubygems]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Kevin Gleeson]]></dc:creator>
            <pubDate>Fri, 18 Dec 2020 21:31:33 GMT</pubDate>
            <atom:updated>2020-12-18T21:31:33.486Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>“If I am a guilty man, my crime is in daring to believe that there’s a ruby gem for every situation, that the truth will pass in my rspec tests and that no one bug can live forever. I believe it still. Much as you try to bury the gems, the truth is out there.” -Coding Mulder, probably.</em></blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FxT9IgEx8SbQ0teblUQ%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fthe-x-files-alien-aliens-xT9IgEx8SbQ0teblUQ&amp;image=https%3A%2F%2Fmedia2.giphy.com%2Fmedia%2FxT9IgEx8SbQ0teblUQ%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="654" frameborder="0" scrolling="no"><a href="https://medium.com/media/acc97574cc7618dcb3c4f1794fc2c8fd/href">https://medium.com/media/acc97574cc7618dcb3c4f1794fc2c8fd/href</a></iframe><h3>The Problem:</h3><p>You have phone numbers as an attribute for a class, but users are able to input their phone numbers in whatever which way they want. They could be from any country, and all lengths and sizes of phone numbers are on the table.</p><h3>The Solution:</h3><p>What would you do if I told you that there’s a gem for that?</p><p>Let’s talk about the ‘phonelib’ gem and why the world needs to know about it’s existence.</p><p>The ‘phonelib’ gem can do everything that our problem asks us for and more! In other words, it’s an excellent way to validate almost every aspect of a phone number if you happen to find yourself in that situation. It’s based off of Google’s libphonenumber, which you can check out the documentation for <a href="https://github.com/google/libphonenumber">here</a>. Also, for the documentation for this gem, which goes over more than this blog does, click <a href="https://github.com/daddyz/phonelib">here</a>!</p><p>Now let’s get started implementing this gem into our Rails backends.</p><h3>Step 1: Install the gem</h3><p>This step is pretty easy, simply add this line of code to your .gemfile and bundle install</p><pre>gem &#39;phonelib&#39;</pre><p>Double check your .gemlock file to make sure that it has made its way into your projects.</p><h3>Step 2: Make a default country code</h3><p>If your project is based in a certain country or you would like your country codes to default to a certain country, you can easily do this with the ‘phonelib’ gem.</p><p>To start, head to your config/initalizers folder and create one called phonelib.rb</p><p>Next, enter this line of code into your new file.</p><pre>Phonelib.default_country = &quot;US&quot;</pre><p><strong>Note</strong>: If your default country is not the United States, you can check out a list of 2 letter country codes <a href="https://www.iban.com/country-codes">here</a>.</p><h3>Step 3: Phonelib Validations</h3><p>Assuming that your phone number attribute is called phone_number your class containing might look like this while implementing the ‘phonelib’ gem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LoxrPVm0g1avPfiBS1vb8g.png" /><figcaption>Use this image for Step 4, too!</figcaption></figure><p>Let’s dissect this first starting with our validation, and the go into what our format_phone method is doing.</p><p>On line 3, our validations start by initializing the phonelib gem using</p><pre>phone: { attributes_you_are_validating } </pre><p>Inside of our phone object, we’re using keywords given to us by the ‘phonelib’ gem. In this case, <strong>possible: true</strong>, is referring to if the information given to the :phone_number attribute could be a phone number. This checks for things like letters and the length of the phone number. The other validation, <strong>allow_blank: false</strong>, tells our database to not allow a user to enter an empty form for the phone_number attribute. For a full list of possible validations, scroll back up to the introduction of the gem and check out the docs!</p><p>The final validation <strong>uniqueness: false</strong>, is allowing a user to have multiple devices with the same number. If uniqueness was set to true, phone numbers could not be duplicates in our database.</p><h3>Step 4: Formatting Phone Numbers</h3><p>Let’s move onto our class method for a device called <strong>format_phone</strong>.</p><p>If you take a look at line 4 in our example, you’ll see how we’re implementing the ‘phonelib’ gem to format our phone numbers. When we use before_save, it’s going to correspond to a .save method in our controller and will trigger before that save happens. This way, a user can type whatever they want, but before it goes to our database, it will get formatted in the way we’ve told it to.</p><p>In our <strong>format_phone</strong> method, we’re calling the phonelib gem itself, telling it to parse what our user inputed as phone_number at to change it into an e164 format. If you’re new to the formatting phone numbers, check out what e164 means <a href="https://www.twilio.com/docs/glossary/what-e164">here</a>. Once it’s changed into the format we like, it will save to our database.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FxULW8CLfa5vZWTq5IA%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fthe-x-files-the-x-files-foxtv-xULW8CLfa5vZWTq5IA&amp;image=https%3A%2F%2Fmedia2.giphy.com%2Fmedia%2FxULW8CLfa5vZWTq5IA%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1bae68eb450da060ccc357526088f4d/href">https://medium.com/media/d1bae68eb450da060ccc357526088f4d/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c738a08a26b0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Primary Keys as UUIDs: A Guide]]></title>
            <link>https://teapuddles.medium.com/primary-keys-as-uuids-a-guide-32a7f88319c9?source=rss-6bec036b5524------2</link>
            <guid isPermaLink="false">https://medium.com/p/32a7f88319c9</guid>
            <category><![CDATA[guides-and-tutorials]]></category>
            <category><![CDATA[primary-keys]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[uuid]]></category>
            <dc:creator><![CDATA[Kevin Gleeson]]></dc:creator>
            <pubDate>Fri, 11 Dec 2020 23:45:02 GMT</pubDate>
            <atom:updated>2020-12-11T23:45:02.284Z</atom:updated>
            <content:encoded><![CDATA[<p>When first getting started with programming in Rails, a new dev will learn about how classes are created implicitly with sequential ids. Sequential ids are nice because they’re easy on the eyes and it’s even easier to track stuff down within your json.</p><p>Sequential ids do have their drawbacks. Take this endpoint for example:</p><pre><a href="https://localhost::3000/users/id:3">https://localhost::3000/users/id:3</a> </pre><p>Following RESTful practices, this URL should bring us to a show page for the user with the id of 3. The largest issue here is how exposed our URI is. If our URLs have exposed URIs, it’s easier for people to access data and that’s never a good thing. Enter UUID.</p><p>UUIDs are a random collection of letters and numbers that create an almost salted id for us. I was really surprised with how easy the implementation is in Rails 6 and I think you will be, too!</p><h4>Step 1: Create a Migration for UUIDs</h4><p>Once you’ve gone ahead and created a new rails application, it’s time to run a migration to help us set up our UUIDs. PostgreSQL knows how to use UUIDs this way, so be sure to change your database from the default!</p><pre>rails g migration enable_uuid </pre><p>This will set us up with a migration file in our database. Next, add the enable_extension ‘pgcrypto’ to the change method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Ev1hq_JRGOWUUAJgM7fHQ.png" /></figure><h4>Step 2: Set UUID as Primary Key Type</h4><p>As mentioned earlier, our primary keys are set to sequential ids by default. Let’s see how we would handle changing this in one of our models.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qw3NzNuQWZOMiQ57lOaafA.png" /></figure><p>Now when we’re creating our table, we’re going to establish that the id for each report will be a uuid. In this model relationship, a <strong>Device</strong> <em>has_many </em><strong>Reports</strong>, so we’ll need to make sure to say that our foreign key is also a uuid instead of the typical t.integer. Now when we migrate our models and look at our schema.rb file…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vpUXFKCSxPSPQ1khKPOUyg.png" /><figcaption>sorry that this is so tiny!</figcaption></figure><p>…our default for our id will generate a random uuid. Now our reports and devices both have uuids!</p><h4>A Single Caveat</h4><p>As you can see, it’s really easy to establish UUIDs as your primary keys. There is one drawback and that’s that the order of your instances will likely become out of order. This is because <strong>Rails will sort UUIDs alphabetically</strong>, so if your second instance starts with ‘a’, it will likely show up as your first instance. Numbers, in this case, go after letters, so you’ll need to figure out a way to re-sort your instances by something else (created_at, maybe?)</p><p>If you want to know a little bit more about those next steps and to check out the resources I used for this blog check them out <a href="https://itnext.io/using-uuids-to-your-rails-6-application-6438f4eeafdf">here</a> and <a href="https://pawelurbanek.com/uuid-order-rails">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32a7f88319c9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rack-CORS and You!]]></title>
            <link>https://teapuddles.medium.com/rack-cors-and-you-b16d8474234b?source=rss-6bec036b5524------2</link>
            <guid isPermaLink="false">https://medium.com/p/b16d8474234b</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[1950s]]></category>
            <category><![CDATA[cors]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Kevin Gleeson]]></dc:creator>
            <pubDate>Sat, 05 Dec 2020 03:28:14 GMT</pubDate>
            <atom:updated>2020-12-05T03:28:14.698Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Are you tired of getting this error in your Rails Applications?</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1glRLzD5yVNcl-3ri4gUxQ.png" /></figure><blockquote>Does not knowing what CORS is got you down?</blockquote><blockquote>Well, have a seat over there and let’s take a look at…</blockquote><blockquote>…RACK-CORS AND YOU!!!</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3oz8xLFwDcVwY1ynvy%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3oz8xLFwDcVwY1ynvy%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3oz8xLFwDcVwY1ynvy%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="311" frameborder="0" scrolling="no"><a href="https://medium.com/media/44b51113c40cd9f60aa9d8f50da7aca3/href">https://medium.com/media/44b51113c40cd9f60aa9d8f50da7aca3/href</a></iframe><p>To get an understanding of why we’re getting this error. It’s important to know what CORS is and what it does for our applications. For starters, CORS stands for Cross Origin Resource Sharing. CORS is essentially a security feature located in our browsers that limits access to our apis from the frontend by monitoring which requests can be allowed through to our backends.</p><blockquote>Sounds simple enough</blockquote><blockquote>But what about that error?</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FxULW8B5lgwk7yauezu%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FxULW8B5lgwk7yauezu%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FxULW8B5lgwk7yauezu%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="322" frameborder="0" scrolling="no"><a href="https://medium.com/media/5128cc89c1e948ecdf6394fa670a0f98/href">https://medium.com/media/5128cc89c1e948ecdf6394fa670a0f98/href</a></iframe><p>According to <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS">MDN</a> “CORS failures result in errors, but for security reasons, specifics about the error <em>are not available to JavaScript</em>. All the code knows is that an error occurred. The only way to determine what specifically went wrong is to look at the browser’s console for details.” This is the main reason why you’re having trouble accessing your backend through your frontend.</p><p>Typically when we look at our console, we see the error from the top of the article. Javascript has an issue with this sort of access and if you’re building out a full stack application you’ll likely need to set up your backend the following way:</p><h4>Step 1. Install the “rack-cors” gem</h4><p>That’s as simple as including gem ‘rack-cors’ in your gem file and doing a fresh bundle install.</p><h4>Step 2. Find cors.rb</h4><p>Go to your config file, and inside the initializers find the cors.rb file.</p><h4>Step 3. Set cors to allow all origins.</h4><p>When you go to your cors.rb file you’ll see something that looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xAfUE3HYQSn3m-knkuy0Ug.png" /><figcaption>Take a look at line 10, Little Suzie.</figcaption></figure><p>What line 10 is saying is that our backend will only allow requests coming from ‘example.com’. All we need to do to change this is allow our origins to come from anywhere. This can be done by replacing ‘example.com’ with an asterisk. ‘*’</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xAfUE3HYQSn3m-knkuy0Ug.png" /><figcaption>You can edit out some of the methods that you want users to have access to this way, too!</figcaption></figure><p>Now our backend should be able to communicate properly with our frontend.</p><blockquote>See kids, that wasn’t so bad.</blockquote><blockquote>Tune in next week for another blog!</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3IKUnoXIoGKIM%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3IKUnoXIoGKIM%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3IKUnoXIoGKIM%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/6eda910c16390c992d88ca8338d97298/href">https://medium.com/media/6eda910c16390c992d88ca8338d97298/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b16d8474234b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>