The goal of this coding kata is to write code to parse HipChat messages. Atlassian’s infamous instant messaging app handles mentions, emoticons, and links:
- @mentions – A way to mention a user. Always starts with an ‘@’ and ends when hitting a non-word character
- Emoticons – For this exercise, you only need to consider ‘custom’ emoticons which are ASCII strings, no longer than 15 characters, contained in parenthesis. You can assume that anything matching this format is an emoticon
- Links – Any URLs contained in the message, along with the page’s title
For example, the message:
@bob @john (success) sweet feature https://www.google.com/
…should produce the following JSON:
{ "mentions": [ "bob", "john" ], "emoticons": [ "success" ], "links": [ { "url": "https://www.google.com/", "title": "Google" } ] }
I wrote a simple object-oriented solution whereby special callout classes (ie: mentions, emoticons, links, future features) directly inherited from an abstract class that provided business logic functionality common to all callouts. The actual parsing code was broken up into smaller modules.
By coding these classes to an interface, I was able to decouple the business logic from the parsing code. I employed the strategy pattern to meld everything together. As such, the approach is easily extendable to new functionality.