A Ruby on Rails API service that provides user management functionality with simulated random failures for testing purposes.
Before you begin, ensure you have the following installed:
-
Ruby:
- Required version: 3.4.1
- Installation guides:
- macOS: Use rbenv or RVM
- Windows: Use RubyInstaller
- Linux: Use rbenv or package manager
-
PostgreSQL:
- Required version: 14.0 or higher
- Installation guides:
- macOS:
brew install postgresql@14or Postgres.app - Windows: PostgreSQL Installer
- Linux:
sudo apt-get install postgresqlor equivalent
- macOS:
-
Node.js and npm (for frontend development):
- Required version: Node.js 18+ and npm 9+
- Installation: Download from official site
- Ruby: ruby 3.4.1
- Rails: Rails 8.0.2
- Database: PostgreSQL
- Testing: Minitest
Endpoint: GET /id
- Returns a universally unique identifier (UUID)
- Response time: < 50ms
- Response Format:
{ "id": "123e4567-e89b-12d3-a456-426614174000" }
Endpoint: GET /user
- Returns a random user's details from the database
- Response time: ~300ms
- Response Format:
{ "id": "123e4567-e89b-12d3-a456-426614174000", "name": "John Doe", "biography": "Software engineer..." }
Endpoint: POST /user
- Creates a new user with 50% chance of success/failure
- Validates presence of name and biography
- Response time: Variable
Request Format:
{
"name": "John Doe",
"biography": "A software engineer..."
}Success Response (200 OK):
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"name": "John Doe",
"biography": "A software engineer...",
"created_at": "2024-01-01T12:00:00.000Z",
"updated_at": "2024-01-01T12:00:00.000Z"
}Failure Responses:
- Random Failure (422 Unprocessable Entity):
{
"error": "Failed to save user (random failure simulation)"
}- Validation Failure (422 Unprocessable Entity):
{
"errors": {
"name": ["can't be blank"],
"biography": ["can't be blank"]
}
}-
Install Ruby (if not already installed):
# Using rbenv on macOS/Linux: rbenv install 3.4.1 rbenv global 3.4.1 # Verify installation: ruby -v
-
Install Rails and Bundler:
gem install rails -v 8.0.2 gem install bundler
-
Clone the repository:
git clone <repository-url> cd user_api
-
Install project dependencies:
bundle install
-
Set up PostgreSQL:
# Start PostgreSQL service # On macOS: brew services start postgresql@14 # On Linux: sudo service postgresql start # On Windows: # Use Task Manager or Services app
-
Configure database:
# Copy and edit database configuration if needed cp config/database.yml.example config/database.yml -
Database setup:
# For first-time setup: bin/rails db:create db:migrate db:seed # For resetting existing database: bin/rails db:reset
-
Run the server:
bin/rails server
Common issues and solutions:
-
PostgreSQL connection errors:
- Ensure PostgreSQL is running
- Check database.yml configuration
- Verify PostgreSQL user permissions
-
Ruby version mismatch:
- Use rbenv or RVM to install and select correct version
- Run
rbenv rehashafter installing gems
-
Bundle install fails:
- Run
gem install pgif PostgreSQL gem fails - Install required system libraries:
# Ubuntu/Debian: sudo apt-get install libpq-dev # macOS: brew install postgresql
- Run
Run the test suite:
bin/rails test- Get a UUID:
curl http://localhost:3000/id- Get a random user:
curl http://localhost:3000/user- Create a new user:
curl -X POST http://localhost:3000/user \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "biography": "A software engineer..."}'The project includes GitHub Actions workflows for:
- Automated testing
- Code linting (RuboCop)
- Security scanning (Brakeman)
- The application uses UUID for primary keys
- Includes comprehensive test coverage
- Uses Github Actions CI/CD
- Follows Rails API-only configuration
- Implements proper error handling