Skip to content

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.

License

Notifications You must be signed in to change notification settings

koshtech/rails_fake_api

Repository files navigation

RailsFakeApi

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.

Features

  • 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.

Installation

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

Setup

Mount the Engine

In config/routes.rb, mount the engine:

Rails.application.routes.draw do
  mount RailsFakeApi::Engine, at: "/"
end

Create Data Directory

$ mkdir -p db/fake_data

Initialize Data Files

Create empty JSON files for your resources, like:

// db/fake_data/posts.json
[]

Usage

Start your Rails server:

$ rails s

Examples

  • 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

Development

After cloning the repo:

$ bin/setup
$ rake test-unit
$ bin/console

To install locally:

$ bundle exec rake install

To release:

  1. Update the version in version.rb
  2. Run:
$ bundle exec rake release

Contributing

Bug reports and pull requests are welcome at https://github.com/koshtech/rails_fake_api. Please adhere to the code of conduct.

License

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.

Code of Conduct

All contributors are expected to follow the code of conduct.

Available in Other Languages

About

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.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published