Coder Social home page Coder Social logo

restful_sync's Introduction

Restful sync

You can use Restful sync to synchronize two databases that share the same structure. Each application defines its Restful API and observers that call the other API.

Dependencies

Configuration

  1. Add this line to your Gemfile
gem 'restful_sync', git: "git://github.com/vala/restful_sync.git"
  1. Generate config file :
rails generate restful_sync:config

You will be able to define the namespace for the routes of the API (default: /api)

Define the application configs

in config/initializers/restful_sync.rb

RestfulSync.config do |config|
  # Define the observed resources models
  config.observed_resources = [Product, User]

  # Define the accessible resources models
  config.accessible_resources = [Order]

  # Define the targeted API authentication token
  config.api_token = "testapitoken" 
end

observed_resources is a list of models that trigger a call to the distant API when created, updated or deleted

You shouldn't include models that are nested in another observed resource

accessible_resources is a list of models that can be accessed through the API

api_token is the app token to ensure access to distant API (must have this token registered in its DB)

A model must not be define in both accessible and observed resources.

Override API controllers

in config/initializers/restful_sync.rb

RestfulSync.config do |config|
  # Define models with specific behavior
  config.override_api_controller = []
end

override_api_controller is a list of custom API controllers

Example for model User

routes will be the following :

  • POST /users => restful_sync/users#create
  • PUT /users/:id => restful_sync/users#update
  • DELETE /users/:id => restful_sync/users#destroy

Then you need write your actions in app/controllers/restful_sync/users_controller.rb

module RestfulSync
  class UsersController < RestfulSync::ApiController
    def create
      user = User.new
      user.encrypted_password = params[:restful_sync].delete(:encrypted_password)
      user.save(validate: false)

      user.update_attributes(params[:restful_sync])
      if user.save
        @status = 200
      else
        @response = user.errors 
      end

      render_json
    end
  end
end

restful_sync's People

Contributors

skaradams avatar

Watchers

Valentin Ballestrino avatar James Cloos avatar

Forkers

skaradams

restful_sync's Issues

Observers config

Initializing observers might crash if no observable model is set (default to RestfulSync::Api that doesn't exist)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.