A ruby library for the RhoConnect App Integration Server.
Using rhoconnect-rb, your application's model data will transparently synchronize with a mobile application built on the Rhodes framework, or any of the available RhoConnect clients. This client includes built-in support for ActiveRecord and DataMapper models.
Load the rhoconnect-rb
library:
require 'rhoconnect-rb'
Note, if you are using datamapper, install the dm-serializer
library and require it in your application. rhoconnect-rb
depends on this utility to interact with Rhoconnect applications using JSON.
Now include Rhoconnectrb::Resource in a model that you want to synchronize with your mobile application:
class Product < ActiveRecord::Base
include Rhoconnectrb::Resource
end
Or, if you are using DataMapper:
class Product
include DataMapper::Resource
include Rhoconnectrb::Resource
end
Next, your models will need to declare a partition key for rhoconnect-rb
. This partition key is used by rhoconnect-rb
to uniquely identify the model dataset when it is stored in a rhoconnect instance. It is typically an attribute on the model or related model. rhoconnect-rb
supports two types of partitions:
- :app - No unique key will be used, a shared dataset is synchronized for all users.
- lambda { some lambda } - Execute a lambda which returns the unique key string.
For example, the Product
model above might have a belongs_to :user
relationship. This provides us a simple way to organize the Product
dataset for rhoconnect by reusing this relationship. The partition identifying a username would be declared as:
class Product < ActiveRecord::Base
include Rhoconnectrb::Resource
belongs_to :user
def partition
lambda { self.user.username }
end
end
Now all of the Product
data synchronized by rhoconnect will organized by self.user.username
. Note: You can also used a fixed key if the dataset doesn't require a dynamic value:
def partition
:app
end
For more information about Rhoconnect partitions, please refer to the Rhoconnect docs.
rhoconnect-rb
installs a /rhoconnect/query
route in your application which the Rhoconnect instance invokes to query the dataset for the dataset you want to synchronize. This route is mapped to a rhoconnect_query
method in your model. This method should return a collection of objects:
class Product < ActiveRecord::Base
include Rhoconnectrb::Resource
belongs_to :user
def partition
lambda { self.user.username }
end
def self.rhoconnect_query(partition, attributes = nil)
Product.includes(:user).where("users.username = ?", partition)
end
end
In this example, self.rhoconnect_query
returns a list of products where the partition string (provided by the rhoconnect instance) matches the user_id
field in the products table.
Configure RhoConnect in an initializer like config/initializers/rhoconnect.rb
(for Rails), or directly in your application (i.e. Sinatra). Here you will setup the rhoconnect uri (the location of your RhoConnect instance), the api_token (see rhoconnect:get_token rake task), and app_endpoint (the location of your ruby app):
config.uri = "http://myrhoconnect.com"
config.token = "secrettoken"
config.app_endpoint = "http://myapp.heroku.com"
If app_endpoint
is defined, your Rhoconnect instance will be configured to query data from the endpoint using the rhoconnect_query method in your model. For example, if your app_endpoint
is defined as "http://myapp.heroku.com", RhoConnect will query data with:
POST http://myapp.heroku.com/rhoconnect/query
Example:
Rhoconnectrb.configure do |config|
config.uri = "http://localhost:8675"
config.token = "mydevtoken"
config.app_endpoint = "http://localhost:3000"
end
Example with authentication:
rhoconnect-rb
installs a /rhoconnect/authenticate
route into your application which will receive credentials from the client. Add block which handles the credentials:
Rhoconnectrb.configure do |config|
config.uri = "http://localhost:8675"
config.token = "mydevtoken"
config.app_endpoint = "http://localhost:3000"
config.authenticate = lambda { |credentials|
User.authenticate(credentials[:login], credentials[:password])
}
end
Using the RhoConnect Heroku Addon
If you're using the RhoConnect Heroku Addon, then you can omit the config.uri and config.token (they are managed for you):
Rhoconnectrb.configure do |config|
config.app_endpoint = "http://myapp.heroku.com"
config.authenticate = lambda { |credentials|
User.authenticate(credentials[:login], credentials[:password])
}
end
The Rhoconnectrb::API module contains routes to all the resources available in Rhoconnect. At a high level the API calls take on the following syntax:
Namespace::Resource.verb_action1_action2_...([resource_id,action1_id,action2_id],data)
Some of these values are not used for every API call such as resource_id and data.
data = {:login=>'username',:password=>'password'}
Rhoconnectrb::API::System.post_login(data)
Rhoconnectrb::API::System.get_license
Rhoconnectrb::API::System.post_reset({})
Rhoconnectrb::API::System.get_appserver
data = {:adapter_url=>'http://test.com'}
Rhoconnectrb::API::System.post_appserver(data)
data ={:names=>"*sources*"}
Rhoconnectrb::API::System.get_stats(data)
Rhoconnectrb::API::Store.get('docname')
data = {:data=>{:id=3},:append=>false}
Rhoconnectrb::API::Store.post('docname',data)
data = {:attributes=>{:login=>'login',:password=>'password'}}
Rhoconnectrb::API::Users.post(data)
Rhoconnectrb::API::Users.delete('user_id')
data = {:attributes=>{:a_user_specific_attribute => a_user_specific_attribute_value}}
Rhoconnectrb::API::Users.put('user_id',data)
Rhoconnectrb::API::Users.get
Rhoconnectrb::API::Users.get('user_id')
Rhoconnectrb::API::Users.get_clients(['user_id','client_id'])
Rhoconnectrb::API::Users.delete_clients(['user_id','client_id'])
Rhoconnectrb::API::Users.get_sources_docnames(['user_id','source_id'])
data = {
:api_token => token,
:user_id => [array_of_users],
:sources => source_name,
:message => 'hello world',
:vibrate => 2000,
:sound => 'hello.mp3'
}
Rhoconnectrb::API::Users.post_ping(data)
Rhoconnectrb::API::Users.get_sources_docs(['user_id','source_id','docname'])
data = {:data=>data,:append=>false}
Rhoconnectrb::API::Users.post_sources_docs(['user_id','source_id','docname'],data)
data = {:refresh_time => 100}
Rhconnectrb::API::ReadState.post_users_sources(['user_id','source_id'])
Rhoconnectrb::API::Sources.get_type('parition_type')
Rhoconnectrb::API::Sources.get('source_id')
data = {:user_name=>'username',:data=>{:poll_interval=>25}}
Rhoconnectrb::API::Sources.put('source_id',data)
Rhoconnectrb::API::Clients.get('client_id')
Rhoconnectrb::API::Clients.get_sources_docnames(['client_id','source_id'])
data = {:data=>data,:append=>false}
Rhoconnectrb::API::Clients.post_sources_docnames(['client_id','source_id'],data)
The Resource class is used for API calls to user defined dynamic or source adapters. The resource name is passed in as a parameter.
data = {:user_id=>'user_id',:objects=>data}
Rhoconnectrb::API::Resource.post_push_objects('source_name',data)
data = {:user_id => 'user_id',:objects=>'object_ids'}
Rhoconnectrb::API::Resource.post_push_deletes('source_name',data)
data = {:user_id=>'user_id',:objects=>data}
Rhoconnectrb::API::Resource.post_fast_insert('source_name',data)
data = {:user_id=>'user_id',:objects=>data}
Rhoconnectrb::API::Resource.post_fast_update('source_name',data)
data = {:user_id=>'user_id',:objects=>data}
Rhoconnectrb::API::Resource.post_fast_delete('source_name',data)
Created and maintained by Lucas Campbell-Rossen, Vladimir Tarasov and Lars Burgess.
Released under the MIT License.