<?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 Brett Schwartz on Medium]]></title>
        <description><![CDATA[Stories by Brett Schwartz on Medium]]></description>
        <link>https://medium.com/@bschwartz92?source=rss-41335e50cbe5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*d0s9I_SW5Gg1umbLElm7Xw.jpeg</url>
            <title>Stories by Brett Schwartz on Medium</title>
            <link>https://medium.com/@bschwartz92?source=rss-41335e50cbe5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 11:51:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bschwartz92/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[Experimenting with custom React Hooks]]></title>
            <link>https://medium.com/@bschwartz92/experimenting-with-custom-react-hooks-378034bb9a97?source=rss-41335e50cbe5------2</link>
            <guid isPermaLink="false">https://medium.com/p/378034bb9a97</guid>
            <category><![CDATA[knicks]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[dark-mode]]></category>
            <category><![CDATA[react-hook]]></category>
            <dc:creator><![CDATA[Brett Schwartz]]></dc:creator>
            <pubDate>Wed, 23 Oct 2019 19:36:17 GMT</pubDate>
            <atom:updated>2019-10-23T19:49:42.480Z</atom:updated>
            <content:encoded><![CDATA[<h3>Experimenting with React Custom Hooks</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*htAZPYyhEjkTJ1pmag13IQ.png" /></figure><p>After the release of React 16.8 (The One With Hooks), I have made a concerted effort to write new components using React’s new Hooks API. The feature that most interested me was sharing a custom hook across multiple components within an application. This is when I noticed the flexibility that hooks could provide which was not possible before they were introduced into the React ecosystem.</p><p>I became curious as to what custom hooks the community was creating to solve their own challenges. I came across <a href="https://usehooks.com/">https://usehooks.com/</a> which is a great resource for sharing custom hooks with the open source community.</p><p>I decided to try out the use-dark-mode hook (<a href="https://github.com/donavon/use-dark-mode">https://github.com/donavon/use-dark-mode</a>) to create a simple application that can toggle between <strong>light</strong> and <strong>dark</strong> mode. The custom hook made it incredibly seamless to transition between the two different modes by simply adding a .dark-mode class to the body attribute whenever <strong>dark</strong> mode is activated. There are numerous options to customize the functionality of the hook but the default settings were sufficient for my application.</p><h3>Demo</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*Yz_Vwoaw_jdO_zSXyTxIBg.gif" /></figure><p><a href="https://codesandbox.io/s/usedarkmode-demo-vv14q?fontsize=14">useDarkMode Knicks - CodeSandbox</a></p><p>Toggling between <strong>dark</strong> and <strong>light</strong> mode on my application will swap out the New York Knicks primary logo with their secondary logo. It will also generate a random Walt “Clyde” Frazier quote because we could all use some more “Clyde” in our lives.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=378034bb9a97" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Zelda: Breadth of the Wild with CSS (Part 2)]]></title>
            <link>https://medium.com/@bschwartz92/zelda-breadth-of-the-wild-with-css-part-2-e76c4c587d72?source=rss-41335e50cbe5------2</link>
            <guid isPermaLink="false">https://medium.com/p/e76c4c587d72</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[zelda]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[html]]></category>
            <dc:creator><![CDATA[Brett Schwartz]]></dc:creator>
            <pubDate>Fri, 18 Oct 2019 16:02:32 GMT</pubDate>
            <atom:updated>2019-10-22T02:50:37.728Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://gedd.ski/">Dave Geddes</a> created a detailed tutorial for how to build the foundation for the Inventory screen in Zelda: Breadth of the Wild using CSS Grids and Flexbox. The tutorial can be found here: <a href="https://gedd.ski/build/zelda/">https://gedd.ski/build/zelda/</a>.</p><p><strong>The end product of the tutorial is pictured below:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EAHV6jerDbEOclpvrXm7zQ.png" /></figure><p>Dave mentions this exercise is meant to be Part (1/2) but I was unable to find Part 2 of the series. With the help of Dave’s foundation, I did my best to fill in the rest of the implementation details.</p><p><strong>Here are the results:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6jqYMv8OGHry-adQgZQySg.png" /></figure><h3>What I learned</h3><ul><li>CSS Grids are very useful for setting up the layout of a page.</li><li>Many simple layouts can be achieved using Flexbox. Don’t reach for CSS Grids if you don’t have to.</li><li>CSS Grids and Flexbox play very nicely together.</li><li>Displaying the contents inside the grid can be achieved by both CSS Grids and Flexbox. Choose which tool you feel most comfortable with.</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fbschwartz92%2Fembed%2Fpreview%2FBaaLdEp%3Fheight%3D600%26slug-hash%3DBaaLdEp%26default-tabs%3Dcss%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2Fbschwartz92%2Fpen%2FBaaLdEp&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F1171370.BaaLdEp.small.7da2e49e-c3df-4064-87b9-7062ac3e1062.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/879662f2880f3c435f638cbca70aab9e/href">https://medium.com/media/879662f2880f3c435f638cbca70aab9e/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e76c4c587d72" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Ruby Struct’s to Create Flexible Interfaces]]></title>
            <link>https://medium.com/@bschwartz92/using-ruby-structs-to-create-flexible-interfaces-ae3e78c81ba1?source=rss-41335e50cbe5------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae3e78c81ba1</guid>
            <category><![CDATA[turing-school]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[sandi-metz]]></category>
            <dc:creator><![CDATA[Brett Schwartz]]></dc:creator>
            <pubDate>Mon, 11 Dec 2017 22:51:56 GMT</pubDate>
            <atom:updated>2017-12-11T23:03:24.922Z</atom:updated>
            <content:encoded><![CDATA[<p>What is a Ruby Struct? The official Ruby documentation (<a href="https://ruby-doc.org/core-2.4.2/Struct.html">https://ruby-doc.org/core-2.4.2/Struct.html</a>) defines a Struct as “A convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.”</p><p>If you’re like me, a real life example can do a superior job of explaining a coding concept than a fancy worded definition. The following is an overview of a recent problem I was faced with and how I used Ruby Struct’s to create a solution.</p><p>The task was to import data from a .txt file and generate some output. A common exercise for any developer which can be hacked together pretty quickly but begs the question, “What is the best way to accomplish this?”.</p><p>My search history consisted of:</p><p><em>Ruby, what is the best way to import data from a text file</em></p><p><em>Ruby, how to maintain flexibility if the structure of text file changes</em></p><p>My searches usually led my to the official documentation for the Ruby File IO class which didn’t help answer my questions. I found my solution in Sandi Metz’s book, <em>Practical Object Oriented Programming in Ruby. </em>In chapter 2, under the section <em>Writing Code That Embraces Change, </em>Sandi writes about the consequences of referencing external data structures in your code. This makes your code <em>leaky </em>and violates the encapsulation principle. She goes on to explain this is a major problem because every reference in your code will need to be changed if/when the structure of the external data changes. To solve this Sandi says, “In Ruby it’s easy to separate structure from meaning. Just as you can use a method to wrap an instance variable, you can use the Ruby Struct class to wrap a structure.”</p><p>Again more fancy definitions, I apologize. Let’s dive into a piece of code to look at how Structs can help us create flexible code.</p><h3>Pre-Struct Implementation</h3><pre>class Player<br>  attr_reader :player<br><br>  def initialize(player_data)<br>    @player = player_data<br>  end</pre><pre>  def format_date_of_birth<br>    player[3].tr!(&#39;-&#39;, &#39;/&#39;)<br>  end<br><br>  def format_gender<br>    player[2] = player[2].start_with?(&#39;M&#39;) ? &#39;Male&#39; : &#39;Female&#39;<br>  end</pre><pre>end</pre><p>In this snippet of code I am initializing an instance of Player with player_data which I am importing from an external .txt file. As you can see, I am directly referencing the structure of the array in the format_date_of_birth and format_gender methods. <strong>This is BAD! </strong>My program will break if the structure of the incoming .txt file changes at any point.</p><p>Our goal is to <em>encapsulate</em> the contents of the external data structure into once location and then reference that object throughout the rest of the program. Lets take a look at how we would accomplish that.</p><h3>Post-Struct Implementation</h3><pre>class Player  <br>  attr_reader :player<br>  <br>  def initialize(player_data)    <br>    @player = create_player(player_data)  <br>  end<br> <br>  RawPlayer = Struct.new(:last_name, <br>                         :first_name, <br>                         :gender,  <br>                         :favorite_color, <br>                         :date_of_birth)</pre><pre>  def create_player(player_data) <br>    RawPlayer.new(player_data[0], <br>                  player_data[1], <br>                  player_data[2], <br>                  player_data[3], <br>                  player_data[4])  <br>  end</pre><pre>  def format_date_of_birth<br>    player.date_of_birth.tr!(&#39;-&#39;, &#39;/&#39;)  <br>  end   </pre><pre>  def format_gender    <br>    player.gender = player.gender.start_with?(&#39;M&#39;) ? &#39;Male&#39;:&#39;Female&#39;  <br>  end</pre><pre>end</pre><p>In this snippet of code I am still initializing an instance of Player with player_data which I am importing from an external .txt file. The difference is during the initialization, I am calling the create_player method which creates a RawPlayer Struct with the player_data attributes from the .txt file.</p><p>In Sandi Metz’s words, “This style of code allows you to protect against changes in externally owned data structures and to make your code more readable and intention revealing. It trades indexing into a structure for sending messages to an object.”</p><p>Lets take a look at the format_date_of_birth method to contrast the differences between the two implementations. In the Pre-Struct implementation we were directly referencing an external data structure by calling its indices (i.e. player[3]) in the method. In the Post-Struct method we are sending messages (i.e. player.date_of_birth) and referencing an internal object. <strong>This is much better!</strong></p><p>The overall benefit of using this approach is protecting against the external changes. In this example, if the structure of the external .txt file changes, we only need to adjust our create_player method. The format_date_of_birth and format_gender methods will still work as expected. The methods have no knowledge of the external data structure and are only reliant on receiving an object that will respond to message calls.</p><h3>Summary / TL;DR</h3><p>Use Structs if you are working with rigid data from an external source. <strong>Never </strong>reference the external data structure directly. <strong>Always </strong>encapsulate the external data structure in one location and reference that object throughout the rest of the program.</p><h4>Further Reading</h4><p><strong>Sandi Metz</strong>, <em>Practical Object Oriented Design in Ruby, </em>Chapter 2: Designing Classes with Single Responsibility</p><p>Official Ruby Struct Documentation, <a href="https://ruby-doc.org/core-2.4.2/Struct.html">https://ruby-doc.org/core-2.4.2/Struct.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae3e78c81ba1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing I/O in Ruby with Minitest]]></title>
            <link>https://medium.com/@bschwartz92/testing-i-o-in-ruby-with-minitest-f775133adf6c?source=rss-41335e50cbe5------2</link>
            <guid isPermaLink="false">https://medium.com/p/f775133adf6c</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Brett Schwartz]]></dc:creator>
            <pubDate>Wed, 06 Dec 2017 04:06:40 GMT</pubDate>
            <atom:updated>2017-12-06T04:06:40.049Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently built a command line Tic Tac Toe game in Ruby. Like many other command line applications, the app is dependent on user input based on output generated by the console. Specifically for my Tic Tac Toe application, all game settings and game moves were handled by user input and computer output. Manually testing the application to check if all the features are working can be very time consuming and cumbersome.</p><p>Let’s take a look at how to automate our I/O responsibilities using <a href="https://github.com/seattlerb/minitest">Minitest</a>, the Ruby <a href="https://ruby-doc.org/stdlib-1.9.3/libdoc/stringio/rdoc/StringIO.html">StringIO</a> library and the <a href="https://github.com/codegourmet/o_stream_catcher">OStreamCatcher</a> gem.</p><h3>Input</h3><p>StringIO is a standard Ruby library that can be included in any project by requiring it at the top of the file like so:</p><pre>require &#39;stringio&#39;</pre><p>This gives us access to its methods .puts and .rewind that will be valuable to simulate user input. Lets take a look at a code snippet below which shows StringIO in action.</p><pre>class PlayerTest &lt; Minitest::Test<br>  attr_reader :player<br><br>  def setup<br>    @player = Player.new(&#39;X&#39;)<br>  end<br><br>  def test_turn_updates_board_with_corresponding_spot<br>    board = Board.new</pre><pre>    <strong>string_io = StringIO.new #New instance of StringIO<br>    string_io.puts &#39;0&#39;       #Stub user input of 0<br>    string_io.rewind         #Start form first stub<br>    $stdin = string_io       #Override Ruby&#39;s standard input<br>    <br>    player.turn!(board)      #Call method<br>    $stdin = STDIN           #Reset Ruby&#39;s standard input</strong></pre><pre>    spaces = [&quot;X&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;]    <br>    assert_equal spaces, board.spaces<br>  end<br>end</pre><p>Lets focus on the middle block of code within the test_turn_updates_board_with_corresponding_spot method. The first thing we do is instantiate a new instance of StringIO and save it to a local variable. We then use the .puts method on our StringIO instance to mock user input. In this example we are only recording one round of user input but this can be used as many times as needed. We then call the .rewind method on our StringIO instance to set the order from first → last for the inputs we mocked. We then override Ruby’s $stdin with our StringIO instance. Therefore, every time the console requests input, our StringIO instance will supply the input with the hardcoded answers we assigned before. Next we call the method. In this case I was simulating a user selecting space 0 on the board. Lastly we reset Ruby’s $stdin to STDIN and check our assertions.</p><p>With that solution in place, the <strong>I </strong>in I/O is taken care of<strong>. </strong>There’s only one problem though. The <strong>output</strong> is running wild in the console like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9kQYTeZ-yiZH0pN1u-Yczg.png" /><figcaption>The test ARE passing but we can do better!</figcaption></figure><h3><strong>Output</strong></h3><p>I highly recommend using <a href="https://github.com/codegourmet/o_stream_catcher">OStreamCatcher</a> to hide the output in the console from your tests. I found the gem very easy to use and they have great documentation with examples. When integrating OStreamCatcher into the example from above we get the following:</p><pre>class PlayerTest &lt; Minitest::Test<br>  attr_reader :player<br><br>  def setup<br>    @player = Player.new(&#39;X&#39;)<br>  end<br><br>  def test_turn_updates_board_with_corresponding_spot<br>    board = Board.new</pre><pre>    string_io = StringIO.new #New instance of StringIO<br>    string_io.puts &#39;0&#39;       #Stub user input of 0<br>    string_io.rewind         #Start form first stub<br>    $stdin = string_io       #Override Ruby&#39;s standard input<br>    <br>    <strong>result, stdout, stderr = OStreamCatcher.catch do<br>      player.turn!(board)    #Call method<br>    end</strong></pre><pre>    $stdin = STDIN           #Reset Ruby&#39;s standard input</pre><pre>    spaces = [&quot;X&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;]    <br>    assert_equal spaces, board.spaces<br>  end<br>end</pre><p>By wrapping the method call in the OStreamCatcher block, we store the <strong>result</strong>, <strong>stdout</strong>, and <strong>stderr</strong>(if any) in local variables. This helps us for two reasons. First, it solves our wild output problem from before and now our console looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I7wA5gftQw_CRmjFNhjwAg.png" /><figcaption>Much better!</figcaption></figure><p>Second, it stores <strong>stdout</strong> in a local variable that we can then test against to make sure our program is outputting the correct statements.</p><p>With this updated solution in place, both the <strong>I</strong> and <strong>O</strong> are correctly being handled by our automated tests.</p><p>Thanks for reading. Please reach out with any inquiries/questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f775133adf6c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pick a Ticket, Any Ticket!]]></title>
            <link>https://medium.com/@bschwartz92/pick-a-ticket-any-ticket-e2d6468365df?source=rss-41335e50cbe5------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2d6468365df</guid>
            <category><![CDATA[rubygems]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[turing-school]]></category>
            <dc:creator><![CDATA[Brett Schwartz]]></dc:creator>
            <pubDate>Sat, 29 Jul 2017 18:00:50 GMT</pubDate>
            <atom:updated>2017-07-29T18:00:50.316Z</atom:updated>
            <content:encoded><![CDATA[<p>The open-source community is one of the top reasons I wanted to have a career in the tech industry. It embraces the collective efforts of a distributed network to improve the software product being created. These values expand beyond the actual software and into the development community itself. Throughout my short time of being a part of the tech community, I have felt the warm embrace from others in the industry. Whether it be a teacher sitting down with me to go over design strategies, my mentor spending part of his work day to pair with me, or a random hero on Stack Overflow coming to the rescue with a solution to my question; the community always seems to come through. So after six months of using various community resources to guide my growth, I felt it was time to give back and contribute to an open source project!</p><p>Trying to find an open-source project to contribute to was pretty overwhelming at first. I used CodeTriage to browse issues I could assist with. Per recommendation, I was in search of project with 50 or less contributors. The first page was filled with massive projects such as the Rails and homebrew source code so I dug a few pages deeper and came upon the faker project. Faker provides an easy and quick way to generate different types of objects in your project. I had used the faker gem many times in projects to seed my development database and thought it could be a fun project to dive into.</p><p>The issue I decided to tackle was titled “Put counts of each faker element in the documentation”. I thought it was a very interesting issue and could be a valuable addition to the project. The current documentation doesn’t provide the user any method of knowing how many faker elements belong to each object. For example, if a developer wanted to seed 75 users using a faker object they should choose <em>Game of Thrones characters</em> which has 1957 possible elements instead of <em>Hey Arnold characters</em> which only has 67 possible elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xHnirtV5HiJON_z-Ug9jtA.png" /></figure><p>I solved the issue by creating a rake task that iterated through several .yaml files where the faker elements were being stored. My method took two dynamic parameters, filename and faker_type, and printed out the number of elements for each object of the faker_type. I then updated the documentation with the counts for each faker object.</p><p>Overall it was a great experience contributing to the open-source community and I am looking forward to future opportunities to get involved with.</p><p>You can view my WIP pull request here: <a href="https://github.com/stympy/faker/pull/970">https://github.com/stympy/faker/pull/970</a></p><p><strong>Update</strong>: After feedback from the faker team, my classmate Sam Landfried is building upon my contribution. His WIP pull request can be viewed here: <a href="https://github.com/stympy/faker/pull/989">https://github.com/stympy/faker/pull/989</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2d6468365df" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Password Reset Using Twilio’s SMS Two-Factor Authentication in Rails]]></title>
            <link>https://medium.com/@bschwartz92/password-reset-using-twilios-sms-two-factor-authentication-in-rails-904af47357b2?source=rss-41335e50cbe5------2</link>
            <guid isPermaLink="false">https://medium.com/p/904af47357b2</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[password-reset]]></category>
            <category><![CDATA[turing-school]]></category>
            <category><![CDATA[twilio]]></category>
            <category><![CDATA[two-factor-authentication]]></category>
            <dc:creator><![CDATA[Brett Schwartz]]></dc:creator>
            <pubDate>Mon, 29 May 2017 23:40:40 GMT</pubDate>
            <atom:updated>2017-05-29T23:45:55.896Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Source Code: </strong><a href="https://github.com/glassjoseph/grab_bag/commit/db99da38db313ac9f5b749e810f9237bbb036550">https://github.com/glassjoseph/grab_bag/commit/db99da38db313ac9f5b749e810f9237bbb036550</a></p><p><strong>Notice:</strong> This tutorial assumes your Rails application has a working authentication system in place. If not, please refer to the video below to hand roll an authentication process.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F134451454&amp;url=https%3A%2F%2Fvimeo.com%2F134451454&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F527956395_640.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=vimeo" width="720" height="450" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2ba1131d676a0dbb76555b64c980510/href">https://medium.com/media/c2ba1131d676a0dbb76555b64c980510/href</a></iframe><p><strong>Twilio Sign Up</strong>: In order to use Twilio’s API, users are required to sign up for an account (<a href="https://www.twilio.com/try-twilio">https://www.twilio.com/try-twilio</a>). When asked “What Product Do You Plan to Use First” select SMS. After signing up, you will receive a unique Twilio sid, auth token and phone number. These will eventually be set as environment variables in your application.</p><p><strong>Gem Install: </strong>Refer to the installation section to correctly install the Twilio-Ruby gem. At this point you should also add your Twilio sid, auth token, and phone number information to wherever you are storing your private environmental variables.</p><p><a href="https://github.com/twilio/twilio-ruby">twilio/twilio-ruby</a></p><p><strong>CodeGenerator Model: </strong>Create a CodeGenerator model. Within, create a generate method to produce a random 6 digit string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*agyfTxamAnko7CMtX8EsZQ.png" /></figure><p><strong>ConfirmationSender Model: </strong>The second model required to carry out this process is ConfirmationSender. Line 3 calls the generate method you previously created and stores it to a local variable named verification_code. Notice on line 4 the user’s verification_code column is being updated by the aforementioned variable. At this point you should run a migration to update the user model with a verification_code column with a string data type. Line 5 calls for a send_code method on the MessageSender class which we will now create.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VFyd81NIpq3Q3eCGLM2ArQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*07AbEKuhEpqd3GUCwtblXA.png" /></figure><p><strong>MessageSender Model: </strong>Create the MessageSender model. Within, create a send_code method. For this method to work correctly all users must have a valid phone number stored in the database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hPawHRpYMXx53QCe-2kfA.png" /></figure><p><strong>Create Button to Update Password:</strong> In a view, create the button for a user to begin the password reset process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J08d0yuP6QtyGZpBGXt9Fg.png" /></figure><p>Then create the corresponding route for the button in your routes file. In the example code snippet below the route is nested under users. This was a design choice for my application and is not required.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fOx7rIB7FGi1R8uvTGBdvg.png" /></figure><p><strong>Passwords Controller:</strong> Create a passwords controller with an update action to house the send_confirmation method and then redirect the user to the confirmation controller which we will now create.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rhaZPCL7z7rnhI6cGOhvkw.png" /></figure><p><strong>Confirmations Controller: </strong>Within the newly created confirmations controller, create a new action to render a form for the current user. You will also need to create the corresponding routes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_7sH2mG9wF-5P108ef7Y4w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OWVUb8l84X6IV1EDXhB-pQ.png" /></figure><p>In the view, enter the following text to create a form for the user to enter their verification code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*taCO1dRbomeXm12g1NVPGw.png" /></figure><p>After the user submits their form, they will hit the create action in the confirmation controller. If the verification code is correct, an authenticated session is created and the user is redirected to a form to create a new password. If incorrect, the verification form is rendered again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IANpKh56pC9x6L1PfHBR9w.png" /></figure><p><strong>Back in the Passwords Controller: </strong>Create an edit action that sets the current user and renders a form for the user to input a new password. The authenticated? method ensures users can only reach the edit path if they previously typed in the correct verification code. The corresponding edit/update routes will also need to be created (line 21/22).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*edZPwnanoRZ9-vm98pHRZQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MsugE5Atajz30N2BwxWejQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oz7KRB_-CPuRIxwPTZGe6Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vwPHpcuY80l0XVReYwL2uA.png" /></figure><p>Still in the Passwords Controller, create an update method that checks to see if the password fields were filled correctly. If so, the user’s password will be updated through the password params.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0wCDiV1KnWGmKGJqroqgNw.png" /></figure><p><strong>Conclusion: </strong>After completing all of the previous steps, our user should now have a new password using Twilio’s SMS two-factor authentication process.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=904af47357b2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>