This gem helps you to:
- Fetch object attributes from the outside of your app
- Mark objects as synced by a special tag
- Drop this marks automatically when the object updates
Installation
Add this line to your application's Gemfile:
gem 'syncable_models'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install syncable_models
Then generate and run migration for Sync model:
$ rails syncable_models:install:migrations
$ rake db:migrate
Usage
At first you should mark you model as syncable:
class Project < ApplicationRecord
include SyncableModels::ActiveRecord
...
end
Then create a controller with API methods.
class ImportApiController < ApplicationController
include SyncableModels::Controller
sync_for :projects
end
This code will automatically generate two api methods for this controller:
-
projects
- get a list of non-synced Project objects
- receives: destination argument - the tag for syncing and an optional count parameter (default value is 50).
- returns: response with an array of the objects, non-synced by this tag
-
sync_projects
- sync Project objects by a tag
- receives: ids - an array of objects' ids for syncing; destination - tag for syncing
- returns: {status: 200} if everything is Ok.
Example:
http://host.dev/import_api/projects?destination=redmine&count=50
http://host.dev/import_api/sync_projects?ids[]=c4bebd13-7de4-450d-82d7-e9a575f978c7&destination=redmine