Class: Dgrid::API::Connection

Inherits:
Object
  • Object
show all
Includes:
Dgrid::ArgumentValidation, SetMembersFromHash
Defined in:
lib/dgrid/api/connection.rb

Constant Summary collapse

@@default_rest_adapter =
ServerRestAdapter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SetMembersFromHash

#change_string_keys_to_symbol_keys, #set_members_from_hash, #split_hash

Methods included from Dgrid::ArgumentValidation

included

Constructor Details

#initialize(options) {|_self| ... } ⇒ Connection

Returns a new instance of Connection.

Yields:

  • (_self)

Yield Parameters:



190
191
192
193
194
195
196
197
# File 'lib/dgrid/api/connection.rb', line 190

def initialize(options, &block)
  @auth, other_members = split_hash(options,[:username,:password])
  set_members_from_hash(other_members)
  @rest_adapter ||= @@default_rest_adapter.new

  confirm_authentication
  yield self if block_given?
end

Class Method Details

.default_rest_adapter=(new_default) ⇒ Object



183
184
185
# File 'lib/dgrid/api/connection.rb', line 183

def self.default_rest_adapter=(new_default)
  @@default_rest_adapter = new_default
end

Instance Method Details

#attach_file_to_entity_in_workspace(entity, filename, workspace_id) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/dgrid/api/connection.rb', line 261

def attach_file_to_entity_in_workspace(entity,filename,workspace_id)
  # TODO Need to support other entity types than incident
  raise "Only attaching to incidents supported at this time" unless entity.is_a?(Incident)

  raise "File #{filename} not found" unless File.exists?(filename)
  raise "Cannot attach files to an unsaved #{entity.class.name} " if entity.new_record?

  # TODO Use workspace-independent route when it becomes available in the server
  new_attachment_path = "/workspaces/#{workspace_id}/"
  new_attachment_path += "#{entity.class.pluralized}/#{entity.id}/attachments/new"

  presigned_post = rest_get(new_attachment_path)
  post_response = post_form_with_file(presigned_post, filename)
  redirected_to = post_response.header['location']
  # Need to parse the redirect url so we can augment the params with
  # auth info in rest_get.
  redirected_url = AugmentedURI.new(redirected_to)
  redirection_path = redirected_url.path
  redirection_params = redirected_url.params
  redirection_response = rest_get(redirection_path,redirection_params)
end

#create_entity(entity, workspace_id) ⇒ Object

argument :entity, Entity



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/dgrid/api/connection.rb', line 215

def create_entity(entity, workspace_id)
  singular_name = entity.class.name.split('::').last.downcase # e.g Dgrid::API::Person => 'person'
  plural_name = entity.class.pluralized
  path_parts =[plural_name]
  if has_multi_workspace?
    path_parts = ['workspaces',workspace_id ] + path_parts
  end
  path = path_parts.join('/')
  params = entity.to_hash
  returned_params = rest_post(path,params)
  entity_params = returned_params[singular_name]
  raise "Did not get an id for new #{singular_name} #{entity.to_s} in #{entity_params.to_s}" unless entity_params.include?("id") && entity_params["id"]
  entity.id = entity_params["id"]
  entity
end

argument :entity1, Entity argument :entity2, Entity argument :workspace_id, String option :link_type, String

Raises:

  • (UnimplementedFunctionality)


249
250
251
252
253
254
255
256
257
258
259
# File 'lib/dgrid/api/connection.rb', line 249

def create_link(entity1, entity2, workspace_id, options)
  # FIXME  It is completely unknown if this is the correct url structure
  raise UnimplementedFunctionality
  path_parts =['links']
  params = {:left_guid => left_entity.id, :right_guid => right_entity.id, :description => options[:link_type]}
  if has_multi_workspace?
    path_parts = ['workspaces',workspace_id ] + path_parts
  end
  path = path_parts.join('/')
  returned_params = rest_post(path,params)
end

#create_workspace(name) {|workspace| ... } ⇒ Object

Yields:

  • (workspace)


201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/dgrid/api/connection.rb', line 201

def create_workspace(name, &block)
  workspace = Workspace.new(self,name)
  if has_multi_workspace?
    workspace_params = rest_post('/workspaces', :name => name)
    workspace.id = workspace_params['id']
    @workspaces_response = nil # clear cache of workspaces_response
  else
    workspace.id = '0'
  end
  yield workspace if block_given?
  workspace
end

#delete_entity_from_workspace(entity, workspace_id) ⇒ Object

argument :entity, Entity argument :workspace_id, String



233
234
235
236
237
238
239
240
241
242
# File 'lib/dgrid/api/connection.rb', line 233

def delete_entity_from_workspace(entity,workspace_id)
  raise "Entity must have id to be deleted" unless entity.id
  plural_name = entity.class.pluralized
  path_parts =[plural_name, entity.id]
  if has_multi_workspace?
    path_parts = ['workspaces',workspace_id ] + path_parts
  end
  path = path_parts.join('/')
  returned_params = rest_delete(path)
end

#get_in_workspace(workspace_id, type, base_path_parts = []) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/dgrid/api/connection.rb', line 327

def get_in_workspace(workspace_id, type, base_path_parts = [] )
  params = type == 'links'? {:flat => 1} : {}
  path_parts = base_path_parts + [type]
  if has_multi_workspace?
    path_parts = ['workspaces', workspace_id] + path_parts
  end
  path = path_parts.join('/')
  returned_params = rest_get(path, params)

  # FIXME Remove this once the production bug is fixed.
  # HACK
  # This is a workaround for a production bug that existed for
  # a few weeks in November of 2013
  hack_to_work_around_lenses_items_index_change(returned_params)


  # FIXME
  # HACK
  # This is an ugly hack to deal with inconsistency in REST results
  # We should probably fix the REST routes and undo this
  if returned_params.include?('item_ids') && base_path_parts.include?('lenses')
    type = 'item_ids'
  elsif returned_params.include?('incident_ids') && base_path_parts.include?('items')
    type = 'incident_ids'
  end
  returned_params[type]
end

#get_incidents_in_item(workspace_id, item_id) ⇒ Object



361
362
363
# File 'lib/dgrid/api/connection.rb', line 361

def get_incidents_in_item(workspace_id, item_id)
  get_in_workspace(workspace_id, 'incidents', ['items', item_id])
end

#get_items_in_lens(workspace_id, lens_id) ⇒ Object



365
366
367
# File 'lib/dgrid/api/connection.rb', line 365

def get_items_in_lens(workspace_id, lens_id)
  get_in_workspace(workspace_id, 'items', ['lenses',lens_id])
end

#get_workspace(name) {|workspace| ... } ⇒ Object

Yields:

  • (workspace)


321
322
323
324
325
# File 'lib/dgrid/api/connection.rb', line 321

def get_workspace(name)
  workspace = self.workspaces.detect {|ws| ws.name.downcase == name.downcase}
  yield workspace if block_given?
  workspace
end

#subordinate_entity_to_other_entity_in_workspace(entity, other, workspace_id) ⇒ Object

Make entity subordinate to another within the specified workspace argument :entity, Entity argument :other, Entity argument :workspace_id, String



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/dgrid/api/connection.rb', line 288

def subordinate_entity_to_other_entity_in_workspace(entity, other, workspace_id)
  raise "Cannot subordiante unsaved #{entity} to #{other.type} #{other}" if entity.new_record?
  raise "Cannot subordiante #{entity} to unsaved #{other.type} #{other}" if other.new_record?
  entity_type = entity.class.pluralized
  path_parts =[other.class.pluralized, other.id, entity_type, entity.id,'add']

  if has_multi_workspace?
    path_parts = ['workspaces',workspace_id ] + path_parts
  end

  path = path_parts.join('/')
  returned_params = rest_get(path)
end

#workspacesObject

list of current workspace objects



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/dgrid/api/connection.rb', line 303

def workspaces
  result = []
  if has_multi_workspace?
    workspaces_response = workspaces_rest_call
    workspaces_list = workspaces_response['workspaces']
    workspaces_list.each do |ws_params|
      ws = Workspace.new(self, ws_params['name'], ws_params['description'])
      ws.id = ws_params['id']
      result << ws
    end
  else
    ws =  Workspace.new(self,'Default Workspace');
    ws.id = '0';
    result << ws
  end
  result
end