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 options = {} Acceptable options: :account => <account name> (default => nil) :secret => <API secret> (default => nil)

:document_id_method => <method name> (default => nil) will call :document_id_method during add_documents and update_documents which should inject an ‘#.id’ key-value pair as a simple way to tie app PKs to doc ids.

:secure => Boolean (default => true, uses HTTPS) :secure => false will use HTTP



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cloudquery.rb', line 141

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)



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/cloudquery.rb', line 158

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.



251
252
253
254
255
256
257
# File 'lib/cloudquery.rb', line 251

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



224
225
226
227
# File 'lib/cloudquery.rb', line 224

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)



200
201
202
203
204
# File 'lib/cloudquery.rb', line 200

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.



352
353
354
# File 'lib/cloudquery.rb', line 352

def count_documents(index, query, *schemas)
  get_documents(index, query, {:fields => '@count'}, *schemas)
end

#delete_accountObject

Delete the account. BEWARE: THIS WILL ACTUALLY DELETE YOUR ACCOUNT.



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

def 
  send_request delete()
end

#delete_documents(index, query, *schemas) ⇒ Object

Delete documents in the index matching query

query defaults to ‘*’ BEWARE: If query = nil this will delete ALL documents in index.

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.



298
299
300
301
302
303
304
305
306
307
# File 'lib/cloudquery.rb', line 298

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



231
232
233
234
# File 'lib/cloudquery.rb', line 231

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



207
208
209
210
211
212
# File 'lib/cloudquery.rb', line 207

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



178
179
180
# File 'lib/cloudquery.rb', line 178

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 ‘*’

options = {} Acceptable options: :fields => a field name, a prefix match (e.g. ‘trans*’), or a list thereof (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.



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/cloudquery.rb', line 323

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



237
238
239
# File 'lib/cloudquery.rb', line 237

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



216
217
218
# File 'lib/cloudquery.rb', line 216

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 = {} to update all matching documents.

If schemas is not nil, ensures existence of the specified schemas on each document.



280
281
282
283
284
285
286
# File 'lib/cloudquery.rb', line 280

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. For example, you can use this method to change the API secret: update_account(=> ‘your-new-secret’)



185
186
187
188
# File 'lib/cloudquery.rb', line 185

def (={})
  body = JSON.generate()
  send_request put(, body)
end

#update_documents(index, docs, *schemas) ⇒ Object

Update documents in the specified index 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.



266
267
268
269
270
271
272
# File 'lib/cloudquery.rb', line 266

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