Class: Cloudquery::Client
- Inherits:
-
Object
- Object
- Cloudquery::Client
- Defined in:
- lib/cloudquery.rb
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Returns the value of attribute account.
-
#secret ⇒ Object
writeonly
Sets the attribute secret.
Class Method Summary collapse
-
.get_secret(account, password) ⇒ Object
Retrieve the API secret for an
account
, using thepassword
(uses HTTPS).
Instance Method Summary collapse
-
#add_documents(index, docs, *schemas) ⇒ Object
Add documents to the specified
index
. -
#add_indexes(*indexes) ⇒ Object
Add one or more indexes to the account, by name or id.
-
#add_schema(xml) ⇒ Object
Add a schema to the account.
-
#count_documents(index, query, *schemas) ⇒ Object
Count documents matching
query
. -
#delete_account! ⇒ Object
Delete the account.
-
#delete_documents(index, query, *schemas) ⇒ Object
Delete documents in the
index
matchingquery
. -
#delete_indexes(*indexes) ⇒ Object
Delete one or more indexes from the account, by name or id
indexes = '*'
will delete all indexes. -
#delete_schema(schema_name) ⇒ Object
Delete a schema from the account, by name.
-
#get_account ⇒ Object
Get the account document.
-
#get_documents(index, query, options = {}, *schemas) ⇒ Object
Get documents matching
query
. -
#get_indexes ⇒ Object
Get the indexes from the account.
-
#get_schemas ⇒ Object
Get the schemas for the account.
-
#initialize(options = {}) ⇒ Client
constructor
Create a new instance of the client.
-
#modify_documents(index, query, modifications, *schemas) ⇒ Object
Modify documents in the
index
matchingquery
. -
#update_account(account_doc = {}) ⇒ Object
Update the account document.
-
#update_documents(index, docs, *schemas) ⇒ Object
index = name
orid
,docs = {}
orArray
of{}
.
Constructor Details
#initialize(options = {}) ⇒ Client
Create a new instance of the client
It’s highly recommended to set options :account
and :secret
. Creating a client without an account and secret isn’t very useful.
Acceptable options:
:account => <account name> (default => nil)
:secret => <API secret> (default => nil)
:document_id_method => <method name> (default => nil)
:secure => Boolean (use HTTPS, default => true)
If document_id_method
is set, it will be called on each document as a part of add_documents
and update_documents
which should inject an '#.id'
key-value pair as a simple way to tie app PKs to doc ids.
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/cloudquery.rb', line 145 def initialize(={}) # unless options[:account] && options[:secret] # raise "Client requires :account => <account name> and :secret => <secret>" # end @account = [:account] @secret = [:secret] @secure = [:secure] != false # must pass false for insecure @document_id_method = [:document_id_method] end |
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
126 127 128 |
# File 'lib/cloudquery.rb', line 126 def account @account end |
#secret=(value) ⇒ Object (writeonly)
Sets the attribute secret
127 128 129 |
# File 'lib/cloudquery.rb', line 127 def secret=(value) @secret = value end |
Class Method Details
.get_secret(account, password) ⇒ Object
Retrieve the API secret for an account
, using the password
(uses HTTPS)
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/cloudquery.rb', line 162 def self.get_secret(account, password) auth = Request.new(:path => "#{PATH}/auth") curl = Curl::Easy.new(auth.url) do |c| c. = true c. = COOKIE_JAR end params = Rack::Utils.build_query({"name" => account, "password" => password}) curl.http_post(params) if curl.response_code == 200 curl.url = Request.new(:path => "#{PATH}/#{API_PATHS[:account]}/#{account}").url curl.http_get response = JSON.parse(curl.body_str) response['result']['secret'] else STDERR.puts "Error: #{curl.response_code} #{Rack::Utils::HTTP_STATUS_CODES[curl.response_code]}" end end |
Instance Method Details
#add_documents(index, docs, *schemas) ⇒ Object
Add documents to the specified index
index = name
or id
, docs = {}
or Array
of {}
.
Documents with key '#.id'
and an existing value will be updated.
If schemas
is not nil
, ensures existence of the specified schemas on each document.
260 261 262 263 264 265 266 |
# File 'lib/cloudquery.rb', line 260 def add_documents(index, docs, *schemas) request = post( build_path(API_PATHS[:documents], index, url_pipe_join(schemas)), JSON.generate(identify_documents(docs)) ) send_request request end |
#add_indexes(*indexes) ⇒ Object
Add one or more indexes to the account, by name or id
232 233 234 235 |
# File 'lib/cloudquery.rb', line 232 def add_indexes(*indexes) body = JSON.generate(indexes.flatten) send_request post(build_path(API_PATHS[:indexes]), body) end |
#add_schema(xml) ⇒ Object
Add a schema to the account. xml
can be a String
or File
-like (responds to :read
)
207 208 209 210 211 |
# File 'lib/cloudquery.rb', line 207 def add_schema(xml) body = xml.respond_to?(:read) ? xml.read : xml request = post(build_path(API_PATHS[:schema]), body) send_request(request, CONTENT_TYPES[:xml]) end |
#count_documents(index, query, *schemas) ⇒ Object
Count documents matching query
query => defaults to '*'
index => may be an id, index name, or Array of ids or names.
Operates on all indexes if index
= nil
or '*'
If schemas
is not nil
, ensures existence of the specified schemas on each document.
366 367 368 |
# File 'lib/cloudquery.rb', line 366 def count_documents(index, query, *schemas) get_documents(index, query, {:fields => '@count'}, *schemas) end |
#delete_account! ⇒ Object
Delete the account.
BEWARE: THIS WILL ACTUALLY DELETE YOUR ACCOUNT.
198 199 200 |
# File 'lib/cloudquery.rb', line 198 def delete_account! send_request delete(account_path) end |
#delete_documents(index, query, *schemas) ⇒ Object
Delete documents in the index
matching query
query => defaults to '*'
index => may be an id, index name, or Array of ids or names.
Operates on all indexes if index
= nil
or '*'
BEWARE: If query
= nil
this will delete ALL documents in index
.
If schemas
is not nil
, ensures existence of the specified schemas on each document.
310 311 312 313 314 315 316 317 318 319 |
# File 'lib/cloudquery.rb', line 310 def delete_documents(index, query, *schemas) request = delete( build_path(API_PATHS[:documents], url_pipe_join(index), url_pipe_join(schemas), Rack::Utils.escape(query) ) ) send_request request end |
#delete_indexes(*indexes) ⇒ Object
Delete one or more indexes from the account, by name or id indexes = '*'
will delete all indexes
239 240 241 242 |
# File 'lib/cloudquery.rb', line 239 def delete_indexes(*indexes) indexes = url_pipe_join(indexes) send_request delete(build_path(API_PATHS[:indexes], indexes)) end |
#delete_schema(schema_name) ⇒ Object
Delete a schema from the account, by name
214 215 216 217 218 219 |
# File 'lib/cloudquery.rb', line 214 def delete_schema(schema_name) send_request delete(build_path( API_PATHS[:schema], Rack::Utils.escape("xfs.schema.name:\"#{schema_name}\"") )) end |
#get_account ⇒ Object
Get the account document
182 183 184 |
# File 'lib/cloudquery.rb', line 182 def get_account send_request get(account_path) end |
#get_documents(index, query, options = {}, *schemas) ⇒ Object
Get documents matching query
query => defaults to '*'
index => may be an id, index name, or Array of ids or names.
Operates on all indexes if index
= nil
or '*'
Acceptable options:
:fields => a field name, a prefix match (e.g. 'trans*'), or Array of fields (default => '*')
:sort => a string ("[+|-]schema.field"), or a list thereof (default => '+#.number')
:offset => integer offset into the result set (default => 0)
:limit => integer limit on number of documents returned per index (default => <no limit>)
If schemas
is not nil
, ensures existence of the specified schemas on each document.
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/cloudquery.rb', line 336 def get_documents(index, query, ={}, *schemas) if fields = .delete(:fields) fields = url_pipe_join(fields) end if [:sort] [:sort] = Array([:sort]).flatten.join(',') end request = get( build_path(API_PATHS[:documents], url_pipe_join(index), url_pipe_join(schemas), url_pipe_join(query), fields ), ) send_request request end |
#get_indexes ⇒ Object
Get the indexes from the account. Returns a list of ids
245 246 247 |
# File 'lib/cloudquery.rb', line 245 def get_indexes send_request get(build_path(API_PATHS[:indexes])) end |
#get_schemas ⇒ Object
Get the schemas for the account.
NOTE: returned format is not the same as accepted for input
224 225 226 |
# File 'lib/cloudquery.rb', line 224 def get_schemas send_request get(build_path(API_PATHS[:schema])) end |
#modify_documents(index, query, modifications, *schemas) ⇒ Object
Modify documents in the index
matching query
modifications = {...data...}
to update all matching documents.
If schemas
is not nil
, ensures existence of the specified schemas on each document.
291 292 293 294 295 296 297 |
# File 'lib/cloudquery.rb', line 291 def modify_documents(index, query, modifications, *schemas) request = put( build_path(API_PATHS[:documents], index, url_pipe_join(schemas), Rack::Utils.escape(query)), JSON.generate(modifications) ) send_request request end |
#update_account(account_doc = {}) ⇒ Object
Update the account document.
Use this method to change the API secret:
update_account({'secret' => 'your-new-secret'})
190 191 192 193 |
# File 'lib/cloudquery.rb', line 190 def update_account(account_doc={}) body = JSON.generate(account_doc) send_request put(account_path, body) end |
#update_documents(index, docs, *schemas) ⇒ Object
index = name
or id
, docs = {}
or Array
of {}
.
Documents lacking the key '#.id'
will be created.
If schemas
is not nil
, ensures existence of the specified schemas on each document.
276 277 278 279 280 281 282 |
# File 'lib/cloudquery.rb', line 276 def update_documents(index, docs, *schemas) request = put( build_path(API_PATHS[:documents], index, url_pipe_join(schemas)), JSON.generate(identify_documents(docs)) ) send_request request end |