class Dgrid::API::Connection
Attributes
auth[RW]
rest_adapter[RW]
Public Class Methods
default_rest_adapter=(new_default)
click to toggle source
# File lib/dgrid/api/connection.rb, line 183 def self.default_rest_adapter=(new_default) @@default_rest_adapter = new_default end
new(options) { |self| ... }
click to toggle source
# 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
Public Instance Methods
attach_file_to_entity_in_workspace(entity,filename,workspace_id)
click to toggle source
# 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)
click to toggle source
argument :entity, Entity
# 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
create_link(entity1, entity2, workspace_id, options)
click to toggle source
argument :entity1, Entity
argument :entity2, Entity
argument :workspace_id, String option :link_type, String
# 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| ... }
click to toggle source
# 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)
click to toggle source
argument :entity, Entity
argument :workspace_id, String
# 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 = [] )
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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| ... }
click to toggle source
# 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)
click to toggle source
Make entity subordinate to another within the specified workspace argument :entity, Entity
argument :other, Entity
argument :workspace_id, String
# 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
workspaces()
click to toggle source
list of current workspace objects
# 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
Protected Instance Methods
confirm_authentication()
click to toggle source
# File lib/dgrid/api/connection.rb, line 409 def confirm_authentication rest_get('/profile') # confirm proper credentials by trying to do something end
determine_workspace_mode()
click to toggle source
# File lib/dgrid/api/connection.rb, line 418 def determine_workspace_mode begin workspaces_rest_call return :multi_workspace rescue NotFoundError => e return :single_workspace rescue RestClient::ResourceNotFound => e return :single_workspace end end
hack_to_work_around_lenses_items_index_change(returned_params)
click to toggle source
Transform data returned by one rest call back to its rightful form. There was a bug introduced in November 2013 which changed the structure of data returned by the /workspaces/id/lenses/id/items route. This method modifies the returned_params to look the way they should ( {‘items_ids’ => {.…} } )
# File lib/dgrid/api/connection.rb, line 435 def hack_to_work_around_lenses_items_index_change(returned_params) if returned_params.include?('lensings') && !returned_params.include?('item_ids') returned_params['item_ids'] = returned_params['lensings'].map {|lensing| lensing['item_id']} # STDERR.puts "transformed this: #{returned_params['lensings'].inspect} into this: #{returned_params['item_ids']}" end end
has_multi_workspace?()
click to toggle source
# File lib/dgrid/api/connection.rb, line 413 def has_multi_workspace? @workspace_mode ||= determine_workspace_mode return :multi_workspace == @workspace_mode end
post_form_with_file(post_form_params, filename)
click to toggle source
# File lib/dgrid/api/connection.rb, line 379 def post_form_with_file(post_form_params, filename) raise "File #{filename} not found" unless File.exists?(filename) post_params = post_form_params.clone post_action = post_params.delete('action') post_url = URI.parse(post_action) mime_type = 'application/octet-stream' post_params['file'] = UploadIO.new(File.new(filename), mime_type ,filename) req = Net::HTTP::Post::Multipart.new(post_url.path, post_params) n = Net::HTTP.new(post_url.host,post_url.port) n.use_ssl = ('https' == post_url.scheme.downcase ) response = n.start do |http| http.request(req) end end
rest_delete(path,params = {})
click to toggle source
# File lib/dgrid/api/connection.rb, line 404 def rest_delete(path,params = {}) full_params = params.merge(@auth) rest_adapter.rest_delete(path,full_params) end
rest_get(path,params = {})
click to toggle source
# File lib/dgrid/api/connection.rb, line 399 def rest_get(path,params = {}) full_params = params.merge(@auth) rest_adapter.rest_get(path,full_params) end
rest_post(path,params = {})
click to toggle source
# File lib/dgrid/api/connection.rb, line 394 def rest_post(path,params = {}) full_params = params.merge(@auth) rest_adapter.rest_post(path,full_params) end
workspaces_rest_call()
click to toggle source
# File lib/dgrid/api/connection.rb, line 371 def workspaces_rest_call if @workspaces_response.nil? weird_required_params = {as_selection: 1} @workspaces_response = rest_get('/workspaces', weird_required_params) end @workspaces_response end