This API provides various endpoints to generate random data, useful for testing, mocking, or just for fun! It's built with Ruby on Rails and utilizes the faker gem for data generation.
- Features
- Technologies Used
- Getting Started
- API Endpoints
- Testing the API with Postman
- Contributing
- License
- Diverse Data Generation: Generate random numbers, strings, names, addresses, dates, emails, UUIDs, quotes, credit card numbers, phone numbers, bank account details, product information, company details, images, and Lorem Ipsum words/sentences.
- Parameter Control: Most endpoints support parameters to control the generated data (e.g., number range, string length, date range, quote category).
- API Versioning: All endpoints are under /api/v1/ for future extensibility.
- Token-Based Authentication (JWT): Secure endpoints requiring a valid JSON Web Token.
- Error & Delay Simulation: An endpoint to simulate HTTP error codes and network delays for robust client testing.
Follow these steps to get the API up and running on your local machine.
- Ruby: Install Ruby
- Rails: gem install rails
- Bundler: gem install bundler
- Git: Install Git
- Clone the repository:
git clone https://github.com/stkossman/random-data-api.git
cd random-data-api
- Install dependencies:
bundle install
- Configure JWT Secret key: For development, you can set the JWT_SECRET_KEY environment variable. Create a .env file in your project root
# .env
JWT_SECRET_KEY=your_super_secret_jwt_key_for_development
For production, you should manage this securely via config/credentials.yml.enc or your hosting provider's environment variables.
This project uses SQLite3 by default, so no external database server is required for development.
- Create the database:
rails db:create
- Run migrations:
rails db:migrate
- Seed initial data (create a test user): You'll need a user to test authentication. Open the Rails console and create one:
rails console
User.create!(username: 'testuser', password: 'password123', password_confirmation: 'password123')
Exit the console:
exit
Start the Rails server:
rails server
All API endpoints are prefixed with /api/v1/.
- Post
/api/v1/auth/login
- Description: Authenticates a user and returns a JWT token.
- Request Body:
{ "username": "your_username", "password": "your_password" }
- Response:
{ "token": "YOUR_JWT_TOKEN", "message": "Login successful" }
All these endpoints require JWT Authentication. Include the Authorization: Bearer YOUR_JWT_TOKEN header.
-
GET /api/v1/random_data/number
- Parameters:
min
: Minimum value (default: 1)max
: Maximum value (default: 100)
- Example:
/api/v1/random_data/number?min=10&max=50
- Parameters:
-
GET /api/v1/random_data/string
- Parameters:
length
: Length of string (integer, default: 10)alpha
: If true, only letters (boolean, optional)numeric
: If true, only numbers (boolean, optional)
- Example:
/api/v1/random_data/string?length=20&alpha=true
- Parameters:
-
GET /api/v1/random_data/name
- Parameters:
gender
: Gender of name (string: "male" or "female")
- Example:
/api/v1/random_data/name?gender=male
- Parameters:
-
GET /api/v1/random_data/address
- Example:
/api/v1/random_data/address
- Example:
-
GET /api/v1/random_data/date
- Parameters:
from
: Start date (string in YYYY-MM-DD format)to
: End date (string in YYYY-MM-DD format)
- Example:
/api/v1/random_data/date?from=2023-01-01&to=2023-12-31
- Parameters:
-
GET /api/v1/random_data/email
- Example:
/api/v1/random_data/email
- Example:
-
GET /api/v1/random_data/uuid
- Example:
/api/v1/random_data/uuid
- Example:
-
GET /api/v1/random_data/quote
- Parameters:
category
: Quote category (string: "yoda", "chuck_norris", "singular")
- Example:
/api/v1/random_data/quote?category=chuck_norris
- Parameters:
-
GET /api/v1/random_data/credit_card
- Example:
/api/v1/random_data/credit_card
- Example:
-
GET /api/v1/random_data/phone_number
- Example:
/api/v1/random_data/phone_number
- Example:
-
GET /api/v1/random_data/bank_account
- Example:
/api/v1/random_data/bank_account
- Example:
-
GET /api/v1/random_data/product
- Example:
/api/v1/random_data/product
- Example:
-
GET /api/v1/random_data/company
- Example:
/api/v1/random_data/company
- Example:
-
GET /api/v1/random_data/image
- Parameters:
width
: Image width in pixels (integer, default: 640)height
: Image height in pixels (integer, default: 480)
- Example:
/api/v1/random_data/image?width=800&height=600
- Parameters:
-
GET /api/v1/random_data/lorem_words
- Parameters:
count
: Number of words (integer, default: 5, max: 200)
- Example:
/api/v1/random_data/lorem_words?count=10
- Parameters:
-
GET /api/v1/random_data/lorem_sentences
- Parameters:
count
: Number of sentences (integer, default: 3, max: 50)
- Example:
/api/v1/random_data/lorem_sentences?count=5
- Parameters:
- GET /api/v1/random_data/error_simulation
- Description: Simulates an HTTP error response or a delay.
- Params:
status
(integer, e.g., 400, 401, 403, 404, 429, 500, 502, 503), delay (float, delay in seconds) - Example (404 error with 2 seconds delay):
/api/v1/random_data/error_simulation?status=404&delay=2
- Example (Success with 1-second delay):
/api/v1/random_data/error_simulation?delay=1
You can use Postman to interact with and test all the API endpoints.
- Download Postman
- Import Collection
- Authentication Flow:
- First, send a
POST
request to/api/v1/auth/login
withusername
andpassword
in the JSON body to get a JWT token. - Set up a Postman collection variable (e.g.,
jwt_token
) to store this token automatically using a "Tests" script on the login request. - For all other protected endpoints, add an
Authorization
header with the valueBearer {{jwt_token}}
(using your Postman variable).
- First, send a
Contributions are welcome.
This project is licensed under the MIT License - see the LICENSE file for details.