Class: CloudQuery::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_query/client.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 '#.#' 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(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.



3
4
5
# File 'lib/cloud_query/client.rb', line 3

def 
  @account
end

#secret=(value) ⇒ Object (writeonly)

Sets the attribute secret

Parameters:

  • value

    the value to set the attribute secret to.



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(, old_password, new_password, new_secret=nil)
  secret = get_secret(, old_password)
  c = Client.new(:account => , :secret => secret, :secure => true)
  a = c.()['result']
  a['password'] = new_password
  a.delete('secret')
  if new_secret != nil
    a['secret'] = new_secret
  end
  c.(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(, 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/100) == 2
    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 = [], 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()
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, options={}, schemas=[])
  if options[:sort]
    options[:sort] = Array(options[:sort]).flatten.join(',')
  end
  request = delete(
    build_path(API_PATHS[:documents],
      url_pipe_join(index),
      url_pipe_join(schemas),
      Rack::Utils.escape(query)
    ),
    options
  )
  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_accountObject

Get the account document



74
75
76
# File 'lib/cloud_query/client.rb', line 74

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  => 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, 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



140
141
142
# File 'lib/cloud_query/client.rb', line 140

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



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:

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


82
83
84
85
# File 'lib/cloud_query/client.rb', line 82

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