Question: Activity: Regular Expression Writing Practice Make sure all test code is done via rspec in ruby!!! ( A ) : URL Extractor Objective: Develop a

Activity: Regular Expression Writing Practice
Make sure all test code is done via rspec in ruby!!!
(A): URL Extractor
Objective: Develop a Ruby program to extract all URLs from a block of text. Your
program should recognize URLs and handle various domain names and paths.
Task:
Write a Ruby script that takes a string as input and outputs each URL found
within the string on a new line.
Use regular expressions to identify the URLs within the text.
Ensure your regex pattern matches different types of URLs, including:
URLs with or without "www"
Any domain extension
URLs that may include:
Subdomains
Port Numbers after the domain
Paths indicating specific pages or resources
Query Parameters starting with ? and containing key-value
pairs
Fragments indicated by # followed by a section identifier
Testing Requirement:
Design a set of test cases to validate your script's ability to correctly extract
URLs, including challenging and edge cases.
Consider variations such as:
URLs with unusual but valid characters, like hyphens, underscores, or
international characters in the domain or path.
URLs containing port numbers
URLs with IP addresses instead of domain names
URLs with query parameters
URLs with fragments
Edge cases like:
URLs missing certain parts (e.g., no path).
URLs adjacent to punctuation in the text
Strings that resemble URLs but are invalid
Include negative tests where the input contains strings that look like URLs
but are not valid, ensuring your script does not mistakenly identify them as
URLs.
(B): Invoice Parser
Objective: Create a Ruby program that parses structured text containing multiple
invoice entries and extracts key details from each entry.
Task:
Construct a Ruby script that processes a string with multiple lines, where
each line represents an invoice entry formatted as [Date]-[Invoice Number]
[Client Name]-[Amount].
Extract the date, invoice number, client name, and amount from each line
and display them in a structured format.
Specifications:
Date: Should follow the format YYYY-MM-DD. Ensure that dates conform to
this format and represent valid calendar dates.
Invoice Number: Alphanumeric codes (e.g., INV001). They may include
letters and numbers.
Client Name: May contain letters, spaces, and common punctuation marks
(like periods, commas, apostrophes).
Amount: Should be in US dollars, prefixed with a dollar sign (e.g., $1000).
Amounts may:
Be whole numbers (e.g., $1000).
Include cents (e.g., $2050.75).
Have commas as thousand separators (e.g., $1,000).
Testing Requirement:
Develop a set of test cases to assess your script's ability to handle various
invoice formats and edge cases.
Consider variations such as:
Valid Variations:
Amounts with cents (e.g., $2050.75).
Amounts with thousand separators (e.g., $1,000, $10,000.50).
Client names with special characters (e.g., O'Connor Ltd., Smith
& Sons, Inc.).
Entries with extra spaces or tabs between fields.
Invalid Variations:
Dates not in YYYY-MM-DD format (e.g.,03/01/2023,2023.03.01).
Amounts not in US dollars (e.g.,1500,1000 without a dollar
sign).
Missing fields or malformed lines (e.g., missing the client name
or amount).
Incorrectly formatted amounts (e.g., $1000.7, $1000.789 with
more than two decimal places).
Include tests that check how your script handles invalid entries, ensuring it
can gracefully handle or report errors.
Example Code Template:
def parse_invoices(invoice_data)
end
invoice_entries =-INVOICES
2023-03-01- INV001- Acme Corp - $1000
2023-03-02- INV002- Beta LLC - $2050
2023-03-03- INV003- Gamma Inc - $3500
INVOICES
parse_invoices(invoice_entries)
Expected Output:
Date: 2023-03-01, Invoice Number: INV001, Client: Acme Corp, Amount: $1000
Date: 2023-03-02, Invoice Number: INV002, Client: Beta LLC, Amount: $2050
Date: 2023-03-03, Invoice Number: INV003, Client: Gamma Inc, Amount: $3500
Write all tests using rspec!
Activity: Regular Expression Writing Practice

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 Programming Questions!