eps / username_generator

Pass the tests to build a username generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Username Generator

Your goal is to write a ruby function that generates a username. It should behave in the following way:

inputs output
James Bond 2007 jbond07
John Doe 1978 jdoe78
John Doe 1978 jdoe78_1
John Doe 1978 jdoe78_2
Chico Marx 1887 1 seller-cmarx87
Harpo Marx 1888 2 manager-hmarx88
Groucho Marx 1890 3 admin-gmarx90

Setup

git clone THIS_REPO
cd username_generator

Testing

We'll use a tool called rspec to outline our objectives AND test our code as we go... hooray for Test Driven Development (TDD)!

rspec is available as a Ruby gem, so start in your terminal by running the command:

gem install rspec

Test Driven Development -- Red, Green, Refactor.

Take a look inside the /spec folder, and you'll see our test suite broken up into 2 files.

  • /spec/the_warmup_spec.rb
  • /spec/username_spec.rb

To run the tests for each level, type the following in the command line:

rspec spec/the_warmup_spec.rb
# or
rspec spec/the_warmup_spec.rb --format documentation

You should see roughly the following output:

Followed by a more detailed look at each of the individual test outputs:

image

Hooray!

  • The first tests are failing. We're seeing RED.
  • Now we need to write code to pass the tests and turn them GREEN.
  • When all the tests are green, we're ready to stop and REFACTOR!

Warmup Challenge

Run: rspec spec/the_warmup_spec.rb

Code your solutions in warmup.rb.

Tips:

  • Pay close attention to the rspec output in your terminal
  • Run your tests frequently and read the output carefully.
  • BE VERY CAREFUL to use the correct function name.

Username Challenges

Run: rspec spec/username_spec.rb

Level 1

Create a function called format_name that accepts a user's first_name and last_name and returns a lowercase string that joins the first character of the first_name with the last_name.

Create a function called format_year that accepts a four digit integer representing the year (YYYY) and parses out the last two digits of the year and returns a a string ("YY").

Create a function called build_username that takes a user's first_name, last_name, and birth_year, and returns a string with the following format: it starts with the first letter of the first_name, followed by the last_name, followed by the last two digits of the birth_year.

Level 2

Create a function called check_privilege that takes a level (integer) and returns the corresponding user_type (string).

  • Allocate privilege using the following table:
    • 0 --> "user" (the default privilege level)
    • 1 --> "seller"
    • 2 --> "manager"
    • 3 --> "admin"

Create a function called user_type_prefix that takes a level (integer) and returns the corresponding prefix, e.g. "admin-", "manager-", "seller-". See the tests for more details.

Update your build_username function to use prefixes. It should now accept a privilege_level (in addition to the other parameters), and tack it on to the beginning of the username (e.g. "seller-jdoe78"). See the tests for more details.

Level 3

Create a function called generate_username that has four arguments (first_name, last_name, birth_year, privilege_level) and returns a unique username. Come up with a simple way to store usernames as you create them.

STRETCH: If a username already exists, append "_1". Then, increment the counter each time you reuse the username (e.g. "jdoe78", "jdoe78_1", "jdoe78_2", "bbunny60", "bbunny60_1").

BONUS

Remove special characters
  • If there are any non-alphabetical characters entered as part of the user's first or last name, make sure they are removed using a regular expression.
Command-line inputs
  • OPTION 1 -- Interactively get user input from the command line and output a username to the console. I.e. when I run ruby username.rb I should be prompted (from the command line) for my name, birth year, etc.
  • OPTION 2 -- Accept command line arguments (e.g. ruby username.rb john doe 1980) and output a username to the console.

About

Pass the tests to build a username generator


Languages

Language:Ruby 100.0%