A Clojure solution for the Nubank's Reward System Code Challenge.
This solution uses Leiningen as it's build tool. The FILE
argument mentioned in the next sections is optional (more details here). If no FILE
is supplied, the registry starts empty.
lein repl
=> (def dev-server (run-dev FILE))
lein run FILE
OR lein run-dev FILE
Gets user's information.
Request
GET /users/:id
Parameters
:id
- User id (string)
Response format
{
"id": "1", // :id
"score": 1.0, // current score
"invited": ["2"] // ids of the users invited by this one
}
Return codes
200 OK
- Success400 Bad Request
-:id
is invalid404 Not Found
-:id
doesn't exist
Invites a new user and returns the updated inviter.
Request
POST /users/:inviter/invite
Body
{
"invitee": :invitee
}
Parameters
:inviter
- The inviter's user id (string):invitee
- The invitee's user id (string)
Response format
{
"id": "1", // :id
"score": 1.0, // current score
"invited": ["2","3"] // ids of the users invited by this one
}
Return codes
200 OK
- Success400 Bad Request
-:invitee
/:inviter
is missing or invalid404 Not Found
-:inviter
doesn't exist
Gets the current ranking. The response list is sorted by score (desc) and id (asc).
Request
GET /ranking
Response format
[
{"id":"1", "score":1.5},
{"id":"2", "score":0},
{"id":"3", "score":0},
{"id":"4", "score":0}
]
Return codes
200 OK
- Success
The tests were implemented using midje
. The whole test suite can be run using the lein midje
command.
This code can be deployed in many fashions. Here are some options...
This code can be easily deployed to Heroku. Just click this button:
- Build an uberjar:
lein uberjar
- Build a Docker image:
sudo docker build -t reward-system
- Run the Docker image:
docker run -p 8080:8080 reward-system
- Build an uberjar:
lein uberjar
- Run it:
java -jar target/reward-system-standalone.jar [FILE]
Both Heroku and Docker starts the application with the default input file.
Optionally, you can supply an input file from where the application will read the invitation records and populate the registry.
The file should have one invitation per line, in the inviterId inviteeId
format. Both inviterId
and inviteeId
will be handled as strings.
Example:
1 2
1 abc
abc c
1 c
2 4
This application provides a input file sample, which can be found at resources/input.txt
.