Created by Raul Mantilla Assia (2011)
Any comments or questions to [email protected]
Based on the rhomobile documentation
docs.rhomobile.com/rhosync/rest-api
rhosync_api
With this gem you can connect to the rhosync API very easy.
INTALLATION
gem install rhosync_api
HOW TO
require "rubygems"
require "rhosync_api"
RhoApi."method"
SIMULATING ACTIVERECORD (Beta version, not completed)
A new feature was added to this gem, after the login method, the gem will get all the source adapters
and simulate a rails active record.
that means that we can do simples find, create, delete methods like:
RhoApi.Contacts.find("Test_user",:conditions=>{:object=>"0001"})
RhoApi.Contacts.create("Test_user",{ "0001"=>{"name"=>"Raul","lastname"=>"Mantilla"} } )
RhoApi.Contacts.delete("Test_user","0003")
RhoApi.Contacts.delete_all("Test_user")
FIND
with this method you can search data inside a source adapter, right now is only avaliable to search by "object id"
or using AND conditionals, in the future will be OR, ORDER, and more conditions.
<source_name> = is the Source name, should be the same name in the rhosync setting.yml file
<user_id> = is the User name or login ID.
RhoApi.<source_name>.find("<user_id>",:conditions=>{:object=>"0001"})
RhoApi.<source_name>.find("<user_id>",:conditions=>{"field1"=>"value1","field2"=>"value2"})
CREATE
with the create method you can insert new records into a given source adapter. and user id
<source_name> = is the Source name, should be the same name in the rhosync setting.yml file
<user_id> = is the User name or login ID.
DATA : Data should be a hash of hashes. {"0001"=>{"field1"=>"value","field2"=>"value2"},.....}
you can insert multiple record in the same query.
RhoApi.<source_name>.create("<user_id>",data)
DELETE
with the delete method you can delete records from a given source adapter and user id
<source_name> = is the Source name, should be the same name in the rhosync setting.yml file
<user_id> = is the User name or login ID.
OBJECTS ID : Data should be an Array of objects IDs ["0001","0002"]
could be also a string "0001" if you want to delete only one value
RhoApi.<source_name>.delete("<user_id>",objects)
DELETE_ALL
with this method you can delete all the data in a given source adapter and user id
<source_name> = is the Source name, should be the same name in the rhosync setting.yml file
<user_id> = is the User name or login ID.
RhoApi.<source_name>.delete_all("<user_id>")
METHODS
- login
- logout
- get_api_token
- get_license_info
- reset
- ping
- list_users
- create_user
- update_user
- delete_user
- list_clients
- create_client
- delete_client
- get_client_params
- list_sources
- get_source_params
- list_source_docs
- get_db_doc
- set_db_doc
- list_client_docs
- get_db_doc_by_type
- set_db_doc_by_type
LOGIN
this method is used to authenticate with rhosync server
RhoApi.login("http://localhost:3000","rhoadmin","")
params:
- RHOSYNC URL
- USERNAME
- PASSWORD
return:
this method will return a token is the authentication is correct
this method will return nil if the authentication is incorrect
LOGOUT
This method set token variable to nil
RhoApi.logout
GET_API_TOKEN
RhoApi.get_api_token
return:
this method returns the rhosync token, with this token you can authenticate to rhosync
GET_LICENSE_INFO
RhoApi.get_license_info
return:
this method returns all the information about the rhosync license
RESET
RhoApi.reset
this method will reset all the redis information in rhosync
PING
this method is used to send alerts to devices using rhosync settings
RhoApi.ping(user_id,ping_params)
params
- USER_ID
the rhosync user id
- PING_PARAMS
ping_params => { :message => "this is a test", :sound=>"hello.mp3" }
# :message - message which will be used to display notification popup dialog on the device
# :badge - iphone specific badge
# :sound - name of the sound file to play upon receiving PUSH notification
# :vibrate - number of seconds to vibrate upon receiving PUSH notification
# :sources - list of data source names to be synced upon receiving PUSH notification
LIST_USERS
RhoApi.list_users
return:
this method returns an ARRAY of users ID.
CREATE_USER
with this method you can create a new user in rhosync
RhoApi.create_user("username","password")
params:
- USERNAME
- PASSWORD
return:
this method will return nil if it fails
UPDATE_USER
with this method you can update the user password, be careful this method will change the Admin password, so keep it in mind.
tested in rhosync 2.1.6: for some reason went i changed the password the admin password was changed too, i think is a bug in this rhosync version.
RhoApi.update_user("username","new password")
params:
- USERNAME : any user ID
- NEW PASSWORD : thi will be the new password
return:
this method will return nil if it fails
DELETE_USER
with this method you can delete an existing user in rhosync
RhoApi.delete_user("user_id")
params:
- USERID
return:
this method will return nil if it fails
LIST_CLIENTS
this method returns a list of devices for a given user ID
RhoApi.list_clients("user_id")
params:
- USERID
return:
this method will return nil if it fails
CREATE_CLIENT
this method create a device for a given user ID
RhoApi.create_client("user_id")
params:
- USERID
return:
this method will return nil if it fails
DELETE_CLIENT
this method deletes a device for a given user ID and device ID
RhoApi.delete_client("user_id","device_id")
params:
- USERID
- DEVICE_ID
return:
this method will return nil if it fails
GET_CLIENT_PARAMS
this method returns the attributes of a given device ID, such as device_type, device_pin, device_port.
RhoApi.get_client_params("device_id")
params:
- DEVICE_ID
return:
this method will return nil if it fails
LIST_SOURCES
this method returns a list rhosync source adapters for a given partition
RhoApi.list_sources("partition")
RhoApi.list_sources
params:
- PARTITION : this could be "user" or "app" by default if use "user" partition
return:
this method will return nil if it fails
GET_SOURCE_PARAMS
this method returns the attributes of a given source adapter
such as name, poll_interval, partition_type, sync_type, queue, query_queue, cud_queue
RhoApi.get_source_params("source name")
params:
- SOURCE NAME
return:
this method will return nil if it fails
LIST_SOURCE_DOCS
this method returns a list of document keys associated with given source and user.
such as md, md_size, md_copy, errors
RhoApi.list_source_docs("user_id","source_name")
params:
- USER ID
- SOURCE NAME
return:
this method will return nil if it fails
GET_DB_DOC
this method returns the content of a given document key.
RhoApi.get_db_doc("document key","data_type")
RhoApi.get_db_doc("document key")
params:
- DOCUMENT KEY : this is the document key listed in the LIST_SOURCE_DOCS method
- DATA TYPE: could be "string" or nil
return:
this method will return nil if it fails
SET_DB_DOC
this method set content of a given document key
RhoApi.set_db_doc("document key","data","data_type")
RhoApi.set_db_doc("document key","data")
params:
- DOCUMENT KEY : this is the document key listed in the LIST_SOURCE_DOCS method
- DATA : Data should be either a string or hash of hashes.
- DATA TYPE: could be type "string" or nil
return:
this method will return nil if it fails
LIST_CLIENT_DOC
this method returns a list of document keys associated with particular device.
RhoApi.list_client_docs("device_id","source_name")
params:
- DEVICE_ID :
- SOURCE NAME:
return:
this method will return nil if it fails
GET_DB_DOC_BY_TYPE
this method returns the content of a given document user and source name.
RhoApi.get_db_doc_by_type("user_id","source_name","type_doc")
params:
- USER ID :
- SOURCE NAME:
- TYPE DOC : such as md, md_size, md_copy, errors
return:
this method will return nil if it fails
SET_DB_DOC_BY_TYPE
this method set content of a given user id and source name
RhoApi.set_db_doc_by_type("user_id","source_name","data","type_doc")
params:
- USER ID :
- SOURCE NAME:
- DATA : Data should be a hash of hashes.
- TYPE DOC : such as md, md_size, md_copy, errors
return:
this method will return nil if it fails
PUSH_OBJECTS
this method save data into a given user id and source name
RhoApi.push_objects("user_id","source_name","data")
params:
- USER ID :
- SOURCE NAME:
- DATA : Data should be a hash of hashes. {"0001"=>{"field1"=>"value","field2"=>"value2"},.....}
return:
this method will return nil if it fails
PUSH_DELETES
this method deletes records in a given user id and source name
RhoApi.push_deletes("user_id","source_name","data")
params:
- USER ID :
- SOURCE NAME:
- DATA : Data should be an Array of objects IDs ["0001","0002"], could be also a string "0001"
return:
this method will return nil if it fails