All Projects → igorkasyanchuk → Fake_api

igorkasyanchuk / Fake_api

Licence: mit
The fastest way to prototype API in your Rails application

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Fake api

Api guard
JWT authentication solution for Rails APIs
Stars: ✭ 159 (+33.61%)
Mutual labels:  api, rails, ruby-on-rails
Graphiti
Stylish Graph APIs
Stars: ✭ 783 (+557.98%)
Mutual labels:  api, rails, ruby-on-rails
Jsonapi parameters
Rails-way to consume JSON:API input
Stars: ✭ 50 (-57.98%)
Mutual labels:  api, rails, ruby-on-rails
Pager Api
Easy API pagination for Rails
Stars: ✭ 86 (-27.73%)
Mutual labels:  api, rails
Jsonapi.rb
Lightweight, simple and maintained JSON:API support for your next Ruby HTTP API.
Stars: ✭ 116 (-2.52%)
Mutual labels:  api, rails
Stimulus reflex expo
StimulusReflex demos
Stars: ✭ 85 (-28.57%)
Mutual labels:  rails, ruby-on-rails
Karafka
Framework for Apache Kafka based Ruby and Rails applications development.
Stars: ✭ 1,223 (+927.73%)
Mutual labels:  rails, ruby-on-rails
Pragma
An expressive, opinionated ecosystem for building beautiful RESTful APIs with Ruby.
Stars: ✭ 93 (-21.85%)
Mutual labels:  api, ruby-on-rails
Instuigram
🎓 Learning Ruby on Rails through building the Instagram Application.
Stars: ✭ 88 (-26.05%)
Mutual labels:  rails, ruby-on-rails
Sr mini
A single file Rails app that will have you running a StimulusReflex and CableReady demo in just 2 steps.
Stars: ✭ 98 (-17.65%)
Mutual labels:  rails, ruby-on-rails
Pingcrm
PingCRM on Rails - A Ruby on Rails demo application to illustrate how Inertia.js works
Stars: ✭ 106 (-10.92%)
Mutual labels:  rails, ruby-on-rails
Graphjin
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.
Stars: ✭ 1,264 (+962.18%)
Mutual labels:  rails, ruby-on-rails
Rails Security Checklist
🔑 Community-driven Rails Security Checklist (see our GitHub Issues for the newest checks that aren't yet in the README)
Stars: ✭ 1,265 (+963.03%)
Mutual labels:  rails, ruby-on-rails
Rails React Boilerplate
Ruby on Rails, React, Webpack 4 boilerplate app.
Stars: ✭ 86 (-27.73%)
Mutual labels:  rails, ruby-on-rails
Bhf
Rails-Engine-Gem that offers an admin interface for trusted user
Stars: ✭ 81 (-31.93%)
Mutual labels:  rails, ruby-on-rails
Reactchat
A chat app built with React.js and ActionCable in Ruby on Rails 5.1
Stars: ✭ 90 (-24.37%)
Mutual labels:  rails, ruby-on-rails
Graphql devise
GraphQL interface on top devise_token_auth
Stars: ✭ 100 (-15.97%)
Mutual labels:  api, rails
Gdpr Rails
An example project on building a GDPR compliant application
Stars: ✭ 109 (-8.4%)
Mutual labels:  rails, ruby-on-rails
Simple token authentication
Simple (but safe) token authentication for Rails apps or API with Devise.
Stars: ✭ 1,474 (+1138.66%)
Mutual labels:  api, rails
Ifme
Free, open source mental health communication web app to share experiences with loved ones
Stars: ✭ 1,147 (+863.87%)
Mutual labels:  rails, ruby-on-rails

Fake API

Build Status RailsJazz https://www.patreon.com/igorkasyanchuk

The fastest way to prototype API with most real dummy data, and provide them to other team members.

Instead of creating many new controllers and actions, and sending random data like "First name #{rand(100)}" you'll get real data for developing/testing.

Gem has a syntax similar to rails routes, factory bot, and uses faker to generate dummy data.

Demo

Features:

  • fast API prototyping
  • clear and familiar syntax
  • Faker for test data generation
    • including links to images if needed
  • manage cookies, session, headers with fake response
  • executes your factories in real-time, so you always get fresh and random data
  • has generator
  • has standalone mode (see documentation for more details)

Installation & Usage

Installation process is very simple.

  • add gem 'fake_api' to the Gemfile
  • run bundle install
  • mount gem in routes.rb mount FakeApi::Engine => '/api'
  • rails g fake_api Product. It will generate factory and routing files.
  • edit app/fake_api/*.rb, define your routing and factories.
  • open localhost:300/api/projects.json (see step 5) or localhost:300/api/projects.xml (to return XML in API response)
  • profit :)

You can keep your routing and factories in many files.

Examples

Sample of the factory:

# app/fake_api/factory.rb
class Factory < FakeApi::Factoring

  # Example of User object
  # you can see that it will generate link to fake image
  factory(:user) do
    {
      id: rand(100),
      first_name: Faker::Name.first_name,
      last_name: Faker::Name.first_name,
      avatar_url: Faker::Avatar.image(size: '128x128'),
      age: rand(100)
    }
  end

  # heare you can put "relations" to other factories
  # see "author" node
  factory(:project) do
    {
      id: rand(1_000),
      title: Faker::Company.name,
      description: Faker::Company.catch_phrase,
      type: Faker::Company.type,
      author: object(:user)
    }
  end

  # or have factory which contains from the list of Projects and a single user
  factory(:complex) do
    {
      projects: create_list(:project, 2),
      user: object(:user)
    }
  end

end

And sample of routing:

# app/fake_api/app_routing.rb
class AppRouting < FakeApi::Routing
  get('/projects').and_return           { create_list(:project, 5) }.with_status(202).with_headers({TOKEN: "SECRET"})
  get(%r{/projects/\d+$}).and_return    { object(:project) }
  post('/projects').and_return          { object(:project).merge({created: 'ok'}) }
  delete(%r{/projects/\d+$}).and_return { { result: :deleted } }.with_status(333)

  post('/auth')
    .and_return { { status: "OK" } }
    .with_cookies({x: "A"})
    .with_session({y: "B"})
    .with_headers({token: "C"})
end

Factory Methods

  • create_list(:factory_name, 10) to create an array of 10 factories.
  • object(:factory_nane) to return a factory

Routing Methods

  • get/post/put/patch/delete to define route
  • and_return to specify the response. This response will be converted to FORMAT (json, xml, js, csv(for arrays) etc)
  • with_cookies to list returned cookies
  • with_session to list changes in session
  • with_headers to list returned headers

Standalone mode

Developing standalone version of the gem:

cd test
rackup -p 3000 -o 0.0.0.0

Edit config.ru to change example code.

Standalone mode

This is an example how you can start just fake_api server and define your factories and responses just inside one single file.

Please check example below and instructions how to start fakea_api in standalone mode.

Since this is a rack app it could be just deployed to the server.

# start it:
# rackup -p 3000 -o 0.0.0.0

# create file
# config.ru

require 'rubygems'
require 'fake_api/standalone'

factory(:user) do
  {
    id: rand(100),
    first_name: Faker::Name.first_name,
    last_name: Faker::Name.last_name,
    age: rand(100)
  }
end

get('/users').and_return do
  create_list(:user, 5)
end

get(%r{/users/\d+$}).and_return do
  object(:user)
end

run FakeApi::Standalone.app on: '/api'

Theare is only one thing which is not convinient is a need to restart server after code changes.

TODO

  • render ERB?
  • exclude from code converage generator and dummy app
  • make code coverage 100%
  • access controller in the gem?
  • reload code in standalone app after code changes in dev mode

Contributing

You are welcome to contribute.

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].