Cenit API
Provides a Ruby client to interact with a Cenit API using a DSL to route your resources and to build
queries and options for result rendering.
Installation
Add this line to your application's Gemfile:
gem 'cenit-api'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cenit-api
Usage
Querying Shared Collections from https://cenit.io
require 'cenit/api'
Cenit::API.v2.setup.cross_shared_collection.get
Or you can use a constant syntax for the API version
Cenit::API::V2.setup.cross_shared_collection.count
CRUD operations
To create objects or access non shared data you must configure your Cenit credentials:
Cenit.access_token 'XXXXXXXXXX'
Cenit.access_key 'KKKKKKKKKK'
If you want to use your own instance of Cenit then configure your host URL by:
Cenit.host 'my-cenit-host-url'
Lets create a JSON data type and some records
api = Cenit::API::V2
api.setup.json_data_type.create namespace: 'Test',
name: 'Object',
schema: {
type: 'object',
properties: {
id: { type: 'integer' },
label: { type: 'string' },
checked: { type: 'boolean' }
},
required: ['label']
}
api.test.object.create(id: 0, label: 'Label')
api.test.object.create((1..10).collect { |i| { id: i, label: "Label #{i}", checked: i.odd? } })
api.test.object.count
api.test.object.get
api.test.object(0).get
api.test.object(0).update(label: 'X')
api.test.object(10).delete
api.test.object(10).get
api.test.object(10).get.code
api.test.object.count
Query DSL
cenit-api
use the query DSL defined by origin
including the Cenit API parameters:
api.test.object.where(checked: false).collect { |obj| obj['id'] }
api.test.object.where(checked: true).collect { |obj| obj['label'] }
api.test.object.where(:checked.exists => false).collect { |obj| obj['label'] }
api.test.object.where(:id.gt => 4).first
api.test.object.where(:id.gt => 4).descending(:label).ignore(:checked).first
api.test.object.limit(2).page(3).get
Push API
Lets first create another test data type using the Push API
api.setup.push json_data_type: {
namespace: 'Test',
name: 'Record',
schema: {
type: 'object',
properties: {
name: { type: 'string' },
number: { type: 'integer' }
}
}
}
And then make a data type simultaneous push to the test
namespace
api.test.push objects: [
{ id: 10, label: "Label TEN" },
{ id: 11, label: "Label ELEVEN" }
],
records: [
{ name: 'Record 1', number: 1 },
{ name: 'Record 2', number: 2 }
]
The result should look like
{
"success": {
"objects": [
{"id": 10, "label": "Label TEN", "checked": false},
{"id": 11, "label": "Label ELEVEN"}
],
"records": [
{"id": "58fa517fce50760627000017", "name": "Record 1", "number": 1},
{"id": "58fa5180ce50760627000018", "name": "Record 2", "number": 2}
]
}
}
Development
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/cenit-io/cenit-api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.