As requested, this is a very simple Messenger API. With two available endpoints, this API can create new text-based messages between two users, and return messages for a given recipient.
You may review the feature work for these endpoints in this pull request.
Timeframe:
- ~3 hours coding
- ~1 hour planning + documentation
- Users (senders and recipients) have unique integer IDs that will be passed to the API endpoints as parameters
- Anonymous messages are not allowed (there must be a sender_id given)
- Empty messages are not allowed (there must be content given)
- Authorization, Authentication and Registration can be ignored (this is a global API that can be leveraged by anyone, security is not a concern)
Endpoint | Parameters | Returns |
---|---|---|
GET /api/v1/messages |
sender_id (integer, optional field - returns messages from all senders if field is not specified), recipient_id (integer, required), limit ("100" or "30d" , required) |
If Successful: 200 response and a list of all messages meeting the specified parameters, returned in the format outlined by JSON:API, If Unsuccessful: 400 Bad Request (missing parameters) |
POST /api/v1/messages |
sender_id (integer, required), recipient_id (integer, required), content (string, required) |
If Successful: 201 Created , If Unsuccessful: 400 Bad Request (missing parameters) |
Request
Verb + Path: POST http://localhost:3000/api/v1/messages
Body:
{
"sender_id": 1234,
"recipient_id": 5678,
"content": "Don't worry, about a thing"
}
Response
Status: 201 Created
Body:
{
"response": "Created"
}
Request
Verb + Path: GET http://localhost:3000/api/v1/messages
Body:
{
"sender_id": 1234,
"recipient_id": 5678,
"limit": "100"
}
Response
Status: 200 OK
Body:
{
"data": [
{
"id": "5",
"type": "message",
"attributes": {
"sender_id": 1234,
"recipient_id": 5678,
"content": "Don't worry, about a thing",
"created_at": "2021-08-27T13:55:26.670Z"
}
},
{
"id": "4",
"type": "message",
"attributes": {
"sender_id": 1234,
"recipient_id": 5678,
"content": "Don't worry, about a thing",
"created_at": "2021-08-27T00:17:17.236Z"
}
},
{
"id": "1",
"type": "message",
"attributes": {
"sender_id": 1234,
"recipient_id": 5678,
"content": "This is a message for you-ooh-ooh",
"created_at": "2021-08-26T22:16:28.820Z"
}
}
]
}
- Ruby 2.7.2
- Rails 6.0.4.1
- PostgreSQL 13.4
- Gems:
To setup these endpoints locally:
- clone this repo to your local machine
- run
bundle install
- run
rails db:{create,migrate}
to setup the database - run
rails s
and navigate to http://localhost:3000 with the desired endpoint forget
requests, and/or use a tool like Postman forpost
requests.
Given additional time, here's what I'd like to work on:
- Refactor! Cleanup methods for increased readability, DRY-ness and SRP
- Add an ErrorSerializer to return 400 responses in the JSON:API convention
- Refactor to provide more detailed error responses (which field is mising, e.g.)
- Include additional edge case handling:
- Set a character limit on message content to ensure it fits within the database
- Leverage a gem like FactoryBot to cleanup test setup