Class: GitDB::Client
- Inherits:
-
Object
- Object
- GitDB::Client
- Defined in:
- lib/gitdb_client.rb
Overview
GitDB Client for Ruby A client library for interacting with GitDB - GitHub-backed NoSQL database
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#count(collection, query = {}) ⇒ Integer
Count documents in a collection.
-
#create_collection(name) ⇒ Boolean
Create a new collection.
-
#delete(collection, id) ⇒ Boolean
Delete a document by ID.
-
#delete_collection(name) ⇒ Boolean
Delete a collection.
-
#delete_many(collection, query) ⇒ Integer
Delete multiple documents.
-
#find(collection, query = {}) ⇒ Array<Hash>
Find documents in a collection.
-
#find_by_id(collection, id) ⇒ Hash
Find a document by ID.
-
#find_one(collection, query = {}) ⇒ Hash?
Find a single document in a collection.
-
#graphql(query, variables = nil) ⇒ Hash
Execute a GraphQL query.
-
#health ⇒ Boolean
Check if the GitDB server is healthy.
-
#initialize(token, owner, repo, base_url = 'http://localhost:7896') ⇒ Client
constructor
Create a new GitDB client.
-
#insert(collection, document) ⇒ String
Insert a document into a collection.
-
#list_collections ⇒ Array<Hash>
List all collections.
-
#set_base_url(url) ⇒ Object
Set the base URL for the client.
-
#update(collection, id, update) ⇒ Boolean
Update a document by ID.
-
#update_many(collection, query, update) ⇒ Integer
Update multiple documents.
Constructor Details
#initialize(token, owner, repo, base_url = 'http://localhost:7896') ⇒ Client
Create a new GitDB client
16 17 18 19 20 21 |
# File 'lib/gitdb_client.rb', line 16 def initialize(token, owner, repo, base_url = 'http://localhost:7896') @token = token @owner = owner @repo = repo @base_url = base_url end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
9 10 11 |
# File 'lib/gitdb_client.rb', line 9 def base_url @base_url end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
9 10 11 |
# File 'lib/gitdb_client.rb', line 9 def owner @owner end |
#repo ⇒ Object (readonly)
Returns the value of attribute repo.
9 10 11 |
# File 'lib/gitdb_client.rb', line 9 def repo @repo end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
9 10 11 |
# File 'lib/gitdb_client.rb', line 9 def token @token end |
Instance Method Details
#count(collection, query = {}) ⇒ Integer
Count documents in a collection
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/gitdb_client.rb', line 180 def count(collection, query = {}) response = make_request(:post, "/api/v1/collections/#{collection}/documents/count", query) unless response.code == '200' raise GitDBException, "Failed to count documents: #{response.code}" end result = JSON.parse(response.body) result['count'] || 0 end |
#create_collection(name) ⇒ Boolean
Create a new collection
41 42 43 44 45 |
# File 'lib/gitdb_client.rb', line 41 def create_collection(name) data = { name: name } response = make_request(:post, '/api/v1/collections', data) response.code == '201' end |
#delete(collection, id) ⇒ Boolean
Delete a document by ID
154 155 156 157 |
# File 'lib/gitdb_client.rb', line 154 def delete(collection, id) response = make_request(:delete, "/api/v1/collections/#{collection}/documents/#{id}") response.code == '200' end |
#delete_collection(name) ⇒ Boolean
Delete a collection
59 60 61 62 |
# File 'lib/gitdb_client.rb', line 59 def delete_collection(name) response = make_request(:delete, "/api/v1/collections/#{name}") response.code == '200' end |
#delete_many(collection, query) ⇒ Integer
Delete multiple documents
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/gitdb_client.rb', line 164 def delete_many(collection, query) response = make_request(:post, "/api/v1/collections/#{collection}/documents/delete-many", query) unless response.code == '200' raise GitDBException, "Failed to delete documents: #{response.code}" end result = JSON.parse(response.body) result['deletedCount'] || 0 end |
#find(collection, query = {}) ⇒ Array<Hash>
Find documents in a collection
85 86 87 88 89 90 91 92 93 |
# File 'lib/gitdb_client.rb', line 85 def find(collection, query = {}) response = make_request(:post, "/api/v1/collections/#{collection}/documents/find", query) unless response.code == '200' raise GitDBException, "Failed to find documents: #{response.code}" end JSON.parse(response.body) end |
#find_by_id(collection, id) ⇒ Hash
Find a document by ID
110 111 112 113 114 115 116 117 118 |
# File 'lib/gitdb_client.rb', line 110 def find_by_id(collection, id) response = make_request(:get, "/api/v1/collections/#{collection}/documents/#{id}") unless response.code == '200' raise GitDBException, "Failed to find document: #{response.code}" end JSON.parse(response.body) end |
#find_one(collection, query = {}) ⇒ Hash?
Find a single document in a collection
100 101 102 103 |
# File 'lib/gitdb_client.rb', line 100 def find_one(collection, query = {}) documents = find(collection, query) documents.first end |
#graphql(query, variables = nil) ⇒ Hash
Execute a GraphQL query
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/gitdb_client.rb', line 196 def graphql(query, variables = nil) request_data = { query: query } request_data[:variables] = variables if variables response = make_request(:post, '/graphql', request_data) unless response.code == '200' raise GitDBException, "Failed to execute GraphQL query: #{response.code}" end result = JSON.parse(response.body) if result['errors'] raise GitDBException, "GraphQL errors: #{result['errors']}" end result end |
#health ⇒ Boolean
Check if the GitDB server is healthy
32 33 34 35 |
# File 'lib/gitdb_client.rb', line 32 def health response = make_request(:get, '/health') response.code == '200' end |
#insert(collection, document) ⇒ String
Insert a document into a collection
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gitdb_client.rb', line 69 def insert(collection, document) response = make_request(:post, "/api/v1/collections/#{collection}/documents", document) unless response.code == '201' raise GitDBException, "Failed to insert document: #{response.code}" end result = JSON.parse(response.body) result['_id'] or raise GitDBException, 'No document ID returned' end |
#list_collections ⇒ Array<Hash>
List all collections
50 51 52 53 |
# File 'lib/gitdb_client.rb', line 50 def list_collections response = make_request(:get, '/api/v1/collections') JSON.parse(response.body) end |
#set_base_url(url) ⇒ Object
Set the base URL for the client
25 26 27 |
# File 'lib/gitdb_client.rb', line 25 def set_base_url(url) @base_url = url end |
#update(collection, id, update) ⇒ Boolean
Update a document by ID
126 127 128 129 |
# File 'lib/gitdb_client.rb', line 126 def update(collection, id, update) response = make_request(:put, "/api/v1/collections/#{collection}/documents/#{id}", update) response.code == '200' end |
#update_many(collection, query, update) ⇒ Integer
Update multiple documents
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/gitdb_client.rb', line 137 def update_many(collection, query, update) data = { query: query, update: update } response = make_request(:post, "/api/v1/collections/#{collection}/documents/update-many", data) unless response.code == '200' raise GitDBException, "Failed to update documents: #{response.code}" end result = JSON.parse(response.body) result['modifiedCount'] || 0 end |