Skip to content

Instantly share code, notes, and snippets.

@al3rez
Forked from oddlyfunctional/handles.rb
Created June 30, 2016 18:46
Show Gist options
  • Save al3rez/cb7cfc19a95e7bc88aad737725336ad5 to your computer and use it in GitHub Desktop.
Save al3rez/cb7cfc19a95e7bc88aad737725336ad5 to your computer and use it in GitHub Desktop.
# Given a user containing a `first_name` and a `last_name` methods, write a `handle`
# method that returns a generated handle that follow the following rules:
# - It must be preceded by an @
# - It must be all lowercase
# - It contains the first letter of the first name, and all the letters of the last
# name (for "John" "Doe", it'd be @jdoe)
# - If the first name contains more than one name, get the first letter of each name
# ("John Fitzgerald Wood" "Doe" becomes @jfwdoe)
# - If the last name contains more than one name, get all the letters of the last one,
# and the first letter of each other ("John" "Fitzgerald Wood Doe" becomes @jfwdoe)
# - If the name contains any non-alphanumeric characters, remove them ("John" "Doe-Smith" becomes @jdsmith)
# - If the generated handle already exists in a pool of users, add an id to it
# (if there's already a @jdoe, generate a @jdoe1; if there's already both @jdoe and @jdoe1, generate a @jdoe2)
# - If the generated handle contains more than 20 characters, truncate it
# (@abcdefghijklmnopqrstuvwx becomes @abcdefghijklmnopqrs)
#
# The `handle` signature is this: handle(user, pool_of_users) => returns a String
# Given a string, write a `find_mentions` method that look for mentions that follow the
# previously stated pattern for handles and return only the handles that exist in a given pool of existing users.
# For example: "Hey @marcos and @fujinuma, meet my good friend @weirdnamethatdoesntexist!"
# would return @marcos and @fujinuma, but not @weirdnamethatdoesntexist. Mind that the pattern
# matching must take into account special characters that may be together with a handle (like "," or "!"), and ignore them.
#
# The `find_mentions` signature is this: find_mentions(text, pool_of_users) => returns an array of strings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment