Making a 404 Finder. Part 2

So in my last post, I walked you through the beginning steps of creating a 404 finder. What we discovered was that Ruby has a nice little class (Net::HTTP) and a method (.get_response) that when used returned to us, a string value indicating the URL's response code. I also laid out what we can now do with this response code. I chose (possibly due to a lack of experience) to use an if...else statement and I'm going to sort it into 6 classes:

  1. Informational (100 code)
  2. Successful (200 code) 
  3.  Redirectional (300 code)
  4. Client Error (400 code)
  5. Server Error (500 code)
  6. 404 Errors (404 code)
I can sort this for instance by saying if the response code is greater than or equal to 100 or less than 200 then it is informational. Or if it is greater than or equal to 400, not equal to 404, and less than 500 then it is a client error and will be sorted into that bunch. Now be careful, if you choose to sort more than that, you might run into trouble: the get_response() method returns a string. Remember that in Ruby, if a = '33' and b = '230,' Ruby will return a as larger when compared to b due to being a string. If you use a to_i method on both variables, you will get a correct comparison. 

Before we do that, we must go back to our original user story: take a .txt file filled with URL's and then run them through our program to find out which links are working and which ones are broken. So we need to a group of some string data's, which happen to be URL's, and then sort through them using our code and place them into their own groups. So, we will be working with array's. We will be putting our URL's into an unsorted arrays, run our code through the array, then place them into their own arrays.

The next part here is on you. Go ahead and create a .txt file and put some URL's of each type in there. Broken, successful, informational etc. Once you've done that, we need to figure out how to put that information into an array we can read. To do this we need to use the File class. The easiest way to do this is assign a variable to File.open('yourtxtfile.txt', 'r'). This tells the computer to: open the .txt file and then read (hence the 'r') it. However, that variable we just assigned is still a file type and we can't run any methods on it besides read and write. To check this you can always put a variable and .class next to it. The computer will tell what kind of data it is!

So we have a variable that is reading the data inside the file. If we assign that variable to another variable and use a .read method on it we'll have a string value to work with. Here's what your code should look like (I called my URL file ural.txt):



We're still not done. We now have one big long string with a bunch of URL's in it. How do we turn that into an array. One thing I remember from JavaScript that is also in Ruby is the .split() method. This takes an argument between the parenthesis that takes a string and splits it with whatever character you provide. Here's how it works:
So in my nonsensical string, the .split method with the argument 'g,' removed the 'g's and returned 5 strings in one array. This is quite useful. I'm counting on my user inputting the URL's into the file with one space in between each URL. So now, I can run .split(' ') on the string variable contents and I should return an array with each URL occupying its own space.

Now we can program our computer to check each URL's response code and we won't have input them manually! But that's a topic for a different day. 

No comments:

Post a Comment