Question: Need help with a project coded in Ruby. It is required that i use counting hashes and the Regexp#match method and the MatchData object to

Need help with a project coded in Ruby. It is required that i use counting hashes and the Regexp#match method and the MatchData object to get the fields. I am also only supposed to iterate over the array once throughout the entire program.

Below is the output it should generate:

Start with a descriptive header followed by at least one blank line, using the variable for the file name:

----------------------------------------------------

Statistics for the Apache log file access_log

-----------------------------------------------------

The first sub-report is a histogram of IP addresses (in a field of 20 spaces) using the asterisk (*) character, with a short header to explain what the data represent. For example:

Frequency of Client IP Addresses:

::1 *******

192.168.1.142 ************************************

192.168.1.1 *************

192.168.1.34 ****

192.168.1.127 ***************

192.168.1.56 *********

192.168.1.138 *********

192.168.1.156 ***************

The second sub-report is a table showing the number of times each unique URL appears in the file with a short header. Note that the forward slashes (/) are part of the URL patterns and that patterns such as /javajam4 are distinct from /javajam4/. (We are tracking what end-users type in the browser, not the number of pages in the site.) Use printf to display the URL in a consistent field width such that the totals are aligned as shown below. You may have to experiment with different widths to find a spacing that works for all URLs in the file.

Frequency of URLs Accessed:

* 7

/ 10

/noindex/css/bootstrap.min.css 1

/noindex/css/open-sans.css 1

/images/apache_pb.gif 1

/images/poweredby.png 1

/noindex/css/fonts/Bold/OpenSans-Bold.woff 3

/noindex/css/fonts/Light/OpenSans-Light.woff 3

/noindex/css/fonts/Light/OpenSans-Light.ttf 3

/noindex/css/fonts/Bold/OpenSans-Bold.ttf 3

/favicon.ico 4

/index.html 2

/first 1

... and so on ...

The last sub-report is a list of HTTP status codes with percentages of URLs that resulted in each status code, which should be displayed as shown. The percentage is based on the count of each unique code divided by the total number of lines in the file, which you should derive from the size of the Array of lines.

HINT: One or both of the numbers must be converted to a Float using to_f to force the result to be a decimal number. Once you have a decimal result, use round() to get two digits to the right of the decimal point.

HTTP Status Codes Summary:

200: 45.37%

403: 11.11%

404: 35.19%

301: 5.56%

304: 2.78%

The critera for a complete program is below for reference:

1. Create three initially-empty counting Hashes as local variables to tabulate the totals for unique IP addresses, URLs, and HTTP Status codes

2. Read in the Apache log file to create an Array of lines

3. You may only iterate over the Array of lines once in the entire program.

3. In that single iteration, process each line using the Regexp#match method to create one of more MatchData objects that capture the three required fields.

4. In the same iteration, populate each of the three Hashes using IP addresses, URLs, and HTTP Status codes as keys and the count of each unique field as the values.

5. The Hash keys for all the ip address and url Hashes should be Symbols, but the key for status code Hash must be a Fixnum

6. Use the Hash#each iterator on each Hash in turn and a do end block to generate the output for each of the three reports, following the sample output abovePlace IP addresses in a field of 20 characters so that the histogram *'s are aligned and use a similar implementation for the URL report (appr. 45-50 characters should be enough).

HINT: Look up Kernel#printf in the Ruby docs.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!