Class: CloudQuery::Client
- Inherits:
-
Object
- Object
- CloudQuery::Client
- Defined in:
- lib/cloud_query/client.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
-
.change_password(account, old_password, new_password, new_secret = nil) ⇒ Object
change password optionally change the secret.
-
.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 = [], fieldmode = nil) ⇒ 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, options = {}, schemas = []) ⇒ Object
Delete documents in the
indexmatchingquery. -
#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, cascade = nil) ⇒ Object
Delete a schema from the account, by name if cascade is true, all documents with the specified schema will be deleted (use with care).
-
#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 = [], fieldmode = nil) ⇒ Object
Modify documents in the
indexmatchingquery. -
#update_account(account_doc = {}) ⇒ Object
Update the account document.
-
#update_documents(index, docs, schemas = [], fieldmode = nil) ⇒ Object
index = nameorid,docs = {}orArrayof{}.
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 '#.#' key-value pair as a simple way to tie app PKs to doc ids.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/cloud_query/client.rb', line 22 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.
3 4 5 |
# File 'lib/cloud_query/client.rb', line 3 def account @account end |
#secret=(value) ⇒ Object (writeonly)
Sets the attribute secret
4 5 6 |
# File 'lib/cloud_query/client.rb', line 4 def secret=(value) @secret = value end |
Class Method Details
.change_password(account, old_password, new_password, new_secret = nil) ⇒ Object
change password optionally change the secret
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cloud_query/client.rb', line 60 def self.change_password(account, old_password, new_password, new_secret=nil) secret = get_secret(account, old_password) c = Client.new(:account => account, :secret => secret, :secure => true) a = c.get_account()['result'] a['password'] = new_password a.delete('secret') if new_secret != nil a['secret'] = new_secret end c.update_account(a) return (new_secret != nil ? new_secret : secret); end |
.get_secret(account, password) ⇒ Object
Retrieve the API secret for an account, using the password (uses HTTPS)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cloud_query/client.rb', line 39 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/100) == 2 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 = [], fieldmode = nil) ⇒ Object
Add documents to the specified index
index = name or id, docs = {} or Array of {}.
Documents with key '#.#' and an existing value will be updated.
If schemas is not nil, ensures existence of the specified schemas on each document.
155 156 157 158 159 160 161 162 |
# File 'lib/cloud_query/client.rb', line 155 def add_documents(index, docs, schemas=[], fieldmode=nil) fm = fieldmode != nil ? {:fieldmode => fieldmode} : nil request = post( build_path(API_PATHS[:documents], index, url_pipe_join(schemas), fm), 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
127 128 129 130 |
# File 'lib/cloud_query/client.rb', line 127 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)
99 100 101 102 103 |
# File 'lib/cloud_query/client.rb', line 99 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.
276 277 278 |
# File 'lib/cloud_query/client.rb', line 276 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.
90 91 92 |
# File 'lib/cloud_query/client.rb', line 90 def delete_account! send_request delete(account_path) end |
#delete_documents(index, query, options = {}, 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.
Acceptable options:
:sort => a string ("[+|-]schema.field"), or a list thereof (default => index-order)
: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.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/cloud_query/client.rb', line 213 def delete_documents(index, query, ={}, schemas=[]) if [:sort] [:sort] = Array([:sort]).flatten.join(',') end 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
134 135 136 137 |
# File 'lib/cloud_query/client.rb', line 134 def delete_indexes(*indexes) indexes = url_pipe_join(indexes) send_request delete(build_path(API_PATHS[:indexes], indexes)) end |
#delete_schema(schema_name, cascade = nil) ⇒ Object
Delete a schema from the account, by name if cascade is true, all documents with the specified schema will be deleted (use with care)
108 109 110 111 112 113 114 |
# File 'lib/cloud_query/client.rb', line 108 def delete_schema(schema_name, cascade=nil) c = cascade ? {:cascade => 'true'} : nil send_request delete(build_path( API_PATHS[:schema], Rack::Utils.escape("$.name:\"#{schema_name}\"") ), c) end |
#get_account ⇒ Object
Get the account document
74 75 76 |
# File 'lib/cloud_query/client.rb', line 74 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 => index-order)
:offset => integer offset into the result set (default => 0)
:limit => integer limit on number of documents returned per index (default => <no limit>)
:fieldmode => 'short' or 'long'/nil, if 'short' then field names will be returned in their
short form (no schema name prefix), this can cause naming collisions depending
on use schema definition and data etc.
If schemas is not nil, ensures existence of the specified schemas on each document.
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/cloud_query/client.rb', line 246 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
140 141 142 |
# File 'lib/cloud_query/client.rb', line 140 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
119 120 121 |
# File 'lib/cloud_query/client.rb', line 119 def get_schemas send_request get(build_path(API_PATHS[:schema])) end |
#modify_documents(index, query, modifications, schemas = [], fieldmode = nil) ⇒ 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.
188 189 190 191 192 193 194 195 |
# File 'lib/cloud_query/client.rb', line 188 def modify_documents(index, query, modifications, schemas=[], fieldmode=nil) fm = fieldmode != nil ? "?fieldmode=#{fieldmode}" : "" request = put( build_path(API_PATHS[:documents], index, url_pipe_join(schemas), Rack::Utils.escape(query), fm), 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'})
82 83 84 85 |
# File 'lib/cloud_query/client.rb', line 82 def update_account(account_doc={}) body = JSON.generate(account_doc) send_request put(account_path, body) end |
#update_documents(index, docs, schemas = [], fieldmode = nil) ⇒ Object
index = name or id, docs = {} or Array of {}.
Documents lacking the key '#.#' will be created.
If schemas is not nil, ensures existence of the specified schemas on each document.
172 173 174 175 176 177 178 179 |
# File 'lib/cloud_query/client.rb', line 172 def update_documents(index, docs, schemas=[], fieldmode=nil) fm = fieldmode != nil ? {:fieldmode => fieldmode} : nil request = put( build_path(API_PATHS[:documents], index, url_pipe_join(schemas), fm), JSON.generate(identify_documents(docs)) ) send_request request end |