Skip to content

Instantly share code, notes, and snippets.

@igmarin
Created January 4, 2024 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igmarin/e4133641428b75a1c40718f858a90297 to your computer and use it in GitHub Desktop.
Save igmarin/e4133641428b75a1c40718f858a90297 to your computer and use it in GitHub Desktop.
Interview Exercise2

Ruby on Rails Exercise for Interview

Task Description:

In this exercise, you are required to create a simple Ruby on Rails API endpoint. This task is designed to assess your knowledge of Rails routing, controllers, and basic data handling.

Duration:
20 minutes

Objective:

Create a Rails API endpoint that performs the following operations:

  1. Endpoint Name: /api/v1/messages
  2. HTTP Method: POST
  3. Functionality:
    • Accept a JSON request with two parameters: username (string) and message (string).
    • Validates that both username and message are present. If either is missing, the endpoint should return a 400 Bad Request status with a relevant error message.
    • If both parameters are present, the endpoint should return a 200 OK status with a JSON response that includes a timestamp and echoes back the received username and message.

Expected JSON Request Format:

{
  "username": "sampleUser",
  "message": "Hello, Rails!"
}

Expected JSON Response Format:

On Success:

{
  "timestamp": "2021-01-01T00:00:00Z",
  "username": "sampleUser",
  "message": "Hello, Rails!"
}

On Failure (e.g., missing parameters):

{
  "error": "Missing required parameters"
}

Guidelines:

  • Create a new controller named MessagesController.
  • Define the POST route in config/routes.rb.
  • Implement the logic in the create action of the controller.
  • Ensure the response is in JSON format.
  • Write clean, readable code and handle potential errors gracefully.

Evaluation Criteria:

  • Correct implementation of the endpoint.
  • Proper handling of HTTP methods and status codes.
  • Code readability and structure.
  • Handling of edge cases and errors.

Notes:

  • You don't need to set up a database for this exercise. The focus is on routing, controller logic, and data handling.
  • Use comments to explain your thought process if needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment