Class: Cloudquery::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudquery.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options={})
  # unless options[:account] && options[:secret]
  #   raise "Client requires :account => <account name> and :secret => <secret>"
  # end
  
  @account = options[:account]
  @secret = options[:secret]
  
  @secure = options[:secure] != false # must pass false for insecure
  
  @document_id_method = options[:document_id_method]
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



126
127
128
# File 'lib/cloudquery.rb', line 126

def 
  @account
end

#secret=(value) ⇒ Object (writeonly)

Sets the attribute secret

Parameters:

  • value

    the value to set the attribute secret to.



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(, password)
  auth = Request.new(:path => "#{PATH}/auth")
  curl = Curl::Easy.new(auth.url) do |c|
    c.enable_cookies = true
    c.cookiejar = COOKIE_JAR
  end
  params = Rack::Utils.build_query({"name" => , "password" => password})
  curl.http_post(params)
  
  if curl.response_code == 200
    curl.url = Request.new(:path => "#{PATH}/#{API_PATHS[: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()
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_accountObject

Get the account document



182
183
184
# File 'lib/cloudquery.rb', line 182

def 
  send_request get()
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, options={}, *schemas)
  if fields = options.delete(:fields)
    fields = url_pipe_join(fields)
  end
  
  if options[:sort]
    options[:sort] = Array(options[:sort]).flatten.join(',')
  end
  
  request = get(
    build_path(API_PATHS[:documents], 
      url_pipe_join(index), 
      url_pipe_join(schemas),
      url_pipe_join(query),
      fields
    ),
    options
  )
  send_request request
end

#get_indexesObject

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_schemasObject

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:

({'secret' => 'your-new-secret'})


190
191
192
193
# File 'lib/cloudquery.rb', line 190

def (={})
  body = JSON.generate()
  send_request put(, 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