Learn how to setup your first Ruby project powered by Appwrite.
Create project
Head to the Appwrite Console.
If this is your first time using Appwrite, create an account and create your first project.
Then, under Integrate with your server, add an API Key with the following scopes.
Category | Required scopes | Purpose |
Database | databases.write | Allows API key to create, update, and delete databases. |
collections.write | Allows API key to create, update, and delete collections. | |
attributes.write | Allows API key to create, update, and delete attributes. | |
documents.read | Allows API key to read documents. | |
documents.write | Allows API key to create, update, and delete documents. |
Other scopes are optional.
Create Ruby project
Create a Ruby CLI application.
mkdir my-app
cd my-app
bundle init
Install Appwrite
Install the Ruby Appwrite SDK. Make sure to lock your SDK to version 10.0.0 to avoid breaking changes.
bundle add appwrite
Import Appwrite
Find your project ID in the Settings page. Also, click on the View API Keys button to find the API key that was created earlier.
Create a new file app.rb and initialize the Appwrite Client. Replace <PROJECT_ID> with your project ID and <YOUR_API_KEY> with your API key.
# Initialize the Appwrite client
require 'appwrite'
include Appwrite
client = Client.new()
client
.set_endpoint('https://cloud.appwrite.io/v1') # Your Appwrite Endpoint
.set_project('<PROJECT_ID>') # Your project ID
.set_key('<YOUR_API_KEY>') # Your secret API key
Initialize database
Once the Appwrite Client is initialized, create a function to configure a todo collection.
databases = Databases.new(client)
todo_database = nil
todo_collection = nil
def prepare_database(databases)
todo_database = databases.create(
database_id: ID.unique(),
name: 'TodosDB'
)
todo_collection = databases.create_collection(
database_id: todo_database.id,
collection_id: ID.unique(),
name: 'Todos'
)
databases.create_string_attribute(
database_id: todo_database.id,
collection_id: todo_collection.id,
key: 'title',
size: 255,
required: true
)
databases.create_string_attribute(
database_id: todo_database.id,
collection_id: todo_collection.id,
key: 'description',
size: 255,
required: false
)
databases.create_boolean_attribute(
database_id: todo_database.id,
collection_id: todo_collection.id,
key: 'isComplete',
required: false,
default: false
)
return todo_database, todo_collection
end
Add documents
Create a function to add some mock data into your new collection.
def seed_database(databases, todo_database, todo_collection)
test_todo1 = {
title: 'Buy apples',
description: 'At least 2KGs',
isComplete: true
}
test_todo2 = {
title: 'Wash the apples',
isComplete: true
}
test_todo3 = {
title: 'Cut the apples',
description: 'Don\'t forget to pack them in a box',
isComplete: false
}
databases.create_document(
database_id: todo_database.id,
collection_id: todo_collection.id,
document_id: ID.unique(),
data: test_todo1
)
databases.create_document(
database_id: todo_database.id,
collection_id: todo_collection.id,
document_id: ID.unique(),
data: test_todo2
)
databases.create_document(
database_id: todo_database.id,
collection_id: todo_collection.id,
document_id: ID.unique(),
data: test_todo3
)
end
Retrieve documents
Create a function to retrieve the mock todo data and a function to execute the requests in order. Run the functions to by calling run_all_tasks().
def get_todos(databases, todo_database, todo_collection)
todos = databases.list_documents(
database_id: todo_database.id,
collection_id: todo_collection.id
)
todos.documents.each do |todo|
puts "Title: #{todo.data['title']}\nDescription: #{todo.data['description']}\nIs Todo Complete: #{todo.data['isComplete']}\n\n"
end
end
def run_all_tasks(databases)
todo_database, todo_collection = prepare_database(databases)
seed_database(databases, todo_database, todo_collection)
get_todos(databases, todo_database, todo_collection)
end
run_all_tasks(databases)
All set
Run your project with ruby app.rb and view the response in your console.