This is a RESTful API that allows users to record donations to projects and retrieve their total donations converted to a specified currency.
- Token-based authentication using
api_token
. - Users can record donations with an amount, currency, and project identifier.
- Users can retrieve their total donations, converted into a specified currency.
- Uses PostgreSQL as the database.
- Runs inside Docker for easy setup and testing.
Then:
cd donation_api
Create a .env
file with:
EXCHANGE_RATE_API_KEY=<YOUR_API_KEY>
To get an exchange rate API key, you can follow this link.
Ensure you have Docker installed, then run:
docker-compose up --build
This will:
- Set up PostgreSQL databases (development & test).
- Run the Rails server at
http://localhost:3000
.
Note: The
--build
flag is needed only the first time or when dependencies change.
Once the Docker containers are running, run:
docker-compose exec api rails db:create db:migrate db:seed
Run the test suite inside the test
container:
docker-compose run --rm test
- Every User has an
api_token
stored in the database. - Requests must include an
Authorization
header with the token. - Users can only access their own donation data.
- To create a new user with an API token, run:
This will generate and display a new user ID and API token.
docker-compose exec api rake user:create
To create donations, a project identifier is necessary. A rake task can be used to create projects in the database:
docker-compose exec api rake project:create[<YOUR_PROJECT_NAME>]
Records a user's donation to a project.
{
"Authorization": "<API_TOKEN>"
}
{
"amount": 100,
"currency": "USD",
"project_id": 1
}
{
"donation": {
"id": 1,
"amount": 100,
"currency": "USD",
"project_id": 1
}
}
Returns the total donation amount converted to the specified currency.
{
"Authorization": "<API_TOKEN>"
}
{
"total_amount": 150,
"currency": "EUR"
}
{
"error": "Currency must be ISO 4217."
}
The API supports 161 commonly used world currencies based on the ISO 4217 standard. Some examples:
Currency Code | Currency Name | Country |
---|---|---|
USD | United States Dollar | United States |
EUR | Euro | European Union |
GBP | Pound Sterling | United Kingdom |
JPY | Japanese Yen | Japan |
AUD | Australian Dollar | Australia |
For the full list of supported currencies, check ISO 4217.
I followed RESTful principles, ensuring a clean separation of concerns:
- Controllers handle API requests.
- Models manage data.
- Services (e.g.,
CurrencyConverter
) handle business logic.
- Implemented token-based authentication (
api_token
) for user-specific access. - Users can record donations with an amount, currency, and project ID.
- The
CurrencyConverter
service fetches real-time exchange rates and converts donations efficiently. - Optimized the process by only passing necessary data.
- Modular design for easy extension.
- Error handling ensures graceful failures.
- Dockerized for seamless setup and testing.
MIT License