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 keyvalue
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 eg 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 DateInvoice Number
Client NameAmount
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 YYYYMMDD Ensure that dates conform to
this format and represent valid calendar dates.
Invoice Number: Alphanumeric codes eg INV 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 eg $
Amounts may:
Be whole numbers eg $
Include cents eg $
Have commas as thousand separators eg $
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 eg $
Amounts with thousand separators eg $ $
Client names with special characters eg O'Connor Ltd Smith
& Sons, Inc.
Entries with extra spaces or tabs between fields.
Invalid Variations:
Dates not in YYYYMMDD format eg
Amounts not in US dollars eg without a dollar
sign
Missing fields or malformed lines eg missing the client name
or amount
Incorrectly formatted amounts eg $ $ 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 parseinvoicesinvoicedata
end
invoiceentries INVOICES
INV Acme Corp $
INV Beta LLC $
INV Gamma Inc $
INVOICES
parseinvoicesinvoiceentries
Expected Output:
Date: Invoice Number: INV Client: Acme Corp, Amount: $
Date: Invoice Number: INV Client: Beta LLC Amount: $
Date: Invoice Number: INV Client: Gamma Inc, Amount: $
Write all tests using rspec!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
