Objective: I have been tasked to create a script that will determine whether URL is a broken link or not. This script will take a text (.txt) file with a list of URL's and check its status.
So, how do we do this? First we need to understand the internet and what a HTTP code really is. What is a 404 error? When you get on the internet you use a browser to interact with web pages. Your browser sends a request to a server which holds the contents of your intended web page. That server sends a response back. This response is key to our URL finder. Here are the HTTP responses in a nutshell (keyword: nutshell):
100 series code: Informational URL's
200 series code: Successful URL's
300 series code: Redirectional URL (Think typing Wikipedia.org instead of Wikipedia.com)
400 series code: Client Side error
500 series code: Server Side error
So, let's go back to one of our original questions: what is a 404 error? A 404 error is a client side error, or more specifically a response code sent to indicate that the link is not functional. The question is what can we type into our text editor to see get said response data and use it to determine whether something is a broken link or not?
Well you have to do a little research (which is what coding often times is). When you do that, you discover that Ruby has a wonderful class to manipulate HTTP. The class Net:HTTP has a method that can get us the response code of a specific URL. Firstly, you'll have to require the class by the command require 'net/http'.
Now we can start using some of the methods! Net::HTTP can send a request to get a response code through the code line: 'Net::HTTP.get_response(URI(#link))'. Try running the code yourself and you should get some type of code that falls between 100 and 500. Your output is a string. So for 'https://www.wikipedia.com,' you'll receive a string with the value of '200.' To make things we can assign this to a variable and convert it to an integer using .to_i and then sort it. We can use a case statement or a comparison operator to have our computer sort it for us.
Now that we have things sorted (pun) out and we know how to turn a website's response code into something we can manipulate, our work is pretty much laid out for us. What are the user stories? We can discuss this more in detail in our next part.
The information which you have provided is really nice and awesome. Provides some very useful insights of how things actually work.
ReplyDeletedigital marketing jobs in hyderabad
Informative post! I really like and appreciate your work, thank you for sharing such a useful information about web errors strategies, hear i prefer some more information to build your career in digital marketing by DoDigital
ReplyDelete