404 Finder. Possible User Stories

So we have a completed 404 Finder, but in my view it is only a skeleton project. What if the user accidentally enters a bunch of white space? What if the user doesn't input or incorrectly inputs the "http://" part? I'm going to answer each of these questions and more in this post.

User Story: Excessive Whitespace
Let's say our user enters too much whitespace by accident (or to test our script). What do we do? Honestly, I'm not sure how to handle whitespace in the middle of a URL. For instance, the user inputs "http://www.g o o g l e.com." I'm not sure how to handle that other than send it back to the user and tell them that it was malformed. However if the user inputs "     http://www.google.com    " I know how to handle that and its quite easy. Just run .strip for each item in your good URL array. This method removes trailing and leading whitespace. But this brings a bigger question: how do we know which URL's are good and which are poor input.

User Story: Run poor URL's without breaking the script!
So let's say we enter in a bad URL: "htp://www.goole.com." Our script is designed to get response codes! You can't get the response code of something that doesn't exist. We need to tell the computer to sort these out. Originally my instinct was to use .slice and sort through the array's first characters, for instance .slice(0,7). However, I discovered the wonderful regular expression and the website: regexpr.com. So I made a regular expression that found matches for "http://" and then sorted them. Now, whenever you have a match that is more than one character, you will be returned an integer 0. Whenever you have only one match, you will be returned an integer telling you which character in the string it matches. For instance if I were matching 'o' in the string 'dog', I would get back integer 1. However, if there are no matches, then I will get back a nil. This helps me sort my URL's. I create two arrays. One for malformed URL's and then one for the one's I'll be running. Here's a good demonstration of a regular expression sorting the URL's. Keep in mind, the regular expression is defined globally.











This checks to see if the regular expression has a match by testing for being returned a nil response. If it does, then it is malformed. If not, then the regular expression returned an integer and it matched. In this case it must return 0 or nil since I'm testing for more than one character. Now, you might be wondering why I'm typing "https?:\/\/" into my regular expression. Regular expressions are situated between two slash marks. One slash begins the regular expression and one ends it. This means, like using quotes within quotes in a string, that I must break slashes. So I use "\/" to mean "/". And the reason for the 'https?'? A user could enter in "https://www.amazon.com" and I should still be able to test for it. It's not wrong or malformed. So entering a 's?' along with the 'http' means that I can test for 'http' and 'https' with the 's' character being optional.


I hope this helps folks in some way and that I'm explaining it well! I work full time while learning to code, so if you see any errors or have any suggestions, let me know!

No comments:

Post a Comment