RailsFakeApi is a Ruby on Rails engine that provides a simple, in-memory (file-based) fake API server, similar to json-server. It runs alongside your main Rails application, allowing you to easily mock API endpoints for frontend development, testing, or quick prototyping without setting up a full database.
Data is persisted to JSON files within your Rails application's db/fake_data
directory, making it easy to inspect, modify, and reset your mock data.
- File-based Persistence: Data is stored in plain JSON files within your Rails app's
db/fake_data
directory. - Dynamic Resources: Automatically handles any resource name (e.g.,
/fake_api/posts
,/fake_api/users
). - Full CRUD Operations: Supports GET, POST, PUT/PATCH, and DELETE requests.
- Automatic ID Generation: Assigns unique IDs to new records.
- Seamless Integration: Runs as a Rails engine, easily mountable in any Rails application.
Install the gem by running:
$ bundle add rails_fake_api
Or add it manually to your Gemfile:
# Gemfile
gem 'rails_fake_api', path: 'path/to/your/rails_fake_api_gem' # For local development
# gem 'rails_fake_api' # Uncomment when published on RubyGems.org
Then install dependencies:
$ bundle install
If you’re not using Bundler:
$ gem install rails_fake_api
In config/routes.rb
, mount the engine:
Rails.application.routes.draw do
mount RailsFakeApi::Engine, at: "/"
end
$ mkdir -p db/fake_data
Create empty JSON files for your resources, like:
// db/fake_data/posts.json
[]
Start your Rails server:
$ rails s
- GET /fake_api/posts
curl http://localhost:3000/fake_api/posts
# => []
- POST /fake_api/posts
curl -X POST -H "Content-Type: application/json" -d '{"title": "My First Post", "author": "Me"}' http://localhost:3000/fake_api/posts
# => {"title": "My First Post", "author": "Me", "id": 1}
- GET /fake_api/posts/1
curl http://localhost:3000/fake_api/posts/1
- PUT /fake_api/posts/1
curl -X PUT -H "Content-Type: application/json" -d '{"title": "Updated Post", "status": "published"}' http://localhost:3000/fake_api/posts/1
- DELETE /fake_api/posts/1
curl -X DELETE http://localhost:3000/fake_api/posts/1
# => HTTP 204 No Content
After cloning the repo:
$ bin/setup
$ rake test-unit
$ bin/console
To install locally:
$ bundle exec rake install
To release:
- Update the version in
version.rb
- Run:
$ bundle exec rake release
Bug reports and pull requests are welcome at https://github.com/koshtech/rails_fake_api. Please adhere to the code of conduct.
This project is licensed under the terms of the GNU Affero General Public License v3.0. See the LICENSE file or visit https://www.gnu.org/licenses/agpl-3.0.html for details.
All contributors are expected to follow the code of conduct.