app/models/maestrano/connector/rails/concerns/entity.rb in maestrano-connector-rails-1.1.2 vs app/models/maestrano/connector/rails/concerns/entity.rb in maestrano-connector-rails-1.2.0
- old
+ new
@@ -1,15 +1,8 @@
module Maestrano::Connector::Rails::Concerns::Entity
extend ActiveSupport::Concern
- def initialize(organization, connec_client, external_client, opts={})
- @organization = organization
- @connec_client = connec_client
- @external_client = external_client
- @opts = opts
- end
-
module ClassMethods
# ----------------------------------------------
# IdMap methods
# ----------------------------------------------
def names_hash
@@ -129,12 +122,25 @@
end
def can_update_external?
true
end
+
+ # ----------------------------------------------
+ # Helper methods
+ # ----------------------------------------------
+ def count_entities(entities)
+ entities.size
+ end
end
+ # ==============================================
+ # ==============================================
+ # Instance methods
+ # ==============================================
+ # ==============================================
+
# ----------------------------------------------
# Mapper methods
# ----------------------------------------------
# Map a Connec! entity to the external model
def map_to_external(entity)
@@ -157,20 +163,27 @@
# Supported options:
# * full_sync
# * $filter (see Connec! documentation)
# * $orderby (see Connec! documentation)
def get_connec_entities(last_synchronization_date=nil)
- return [] if @opts[:skip_connec] || !self.class.can_read_connec?
+ return [] if @opts[:__skip_connec] || !self.class.can_read_connec?
Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Fetching Connec! #{self.class.connec_entity_name}")
entities = []
query_params = {}
query_params[:$orderby] = @opts[:$orderby] if @opts[:$orderby]
# Fetch first page
page_number = 0
+
+ batched_fetch = @opts[:__limit] && @opts[:__skip]
+ if batched_fetch
+ query_params[:$top] = @opts[:__limit]
+ query_params[:$skip] = @opts[:__skip]
+ end
+
if last_synchronization_date.blank? || @opts[:full_sync]
query_params[:$filter] = @opts[:$filter] if @opts[:$filter]
else
query_params[:$filter] = "updated_at gt '#{last_synchronization_date.iso8601}'" + (@opts[:$filter] ? " and #{@opts[:$filter]}" : '')
end
@@ -179,18 +192,21 @@
uri = "#{self.class.normalized_connec_entity_name}?#{query_params.to_query}"
response_hash = fetch_connec(uri, 0)
entities = response_hash["#{self.class.normalized_connec_entity_name}"]
entities = [entities] if self.class.singleton?
- # Fetch subsequent pages
- while response_hash['pagination'] && response_hash['pagination']['next']
- page_number += 1
- # ugly way to convert https://api-connec/api/v2/group_id/organizations?next_page_params to /organizations?next_page_params
- next_page = response_hash['pagination']['next'].gsub(/^(.*)\/#{self.class.normalized_connec_entity_name}/, self.class.normalized_connec_entity_name)
+ # Only the first page if batched_fetch
+ unless batched_fetch
+ # Fetch subsequent pages
+ while response_hash['pagination'] && response_hash['pagination']['next']
+ page_number += 1
+ # ugly way to convert https://api-connec/api/v2/group_id/organizations?next_page_params to /organizations?next_page_params
+ next_page = response_hash['pagination']['next'].gsub(/^(.*)\/#{self.class.normalized_connec_entity_name}/, self.class.normalized_connec_entity_name)
- response_hash = fetch_connec(next_page, page_number)
- entities << response_hash["#{self.class.normalized_connec_entity_name}"]
+ response_hash = fetch_connec(next_page, page_number)
+ entities << response_hash["#{self.class.normalized_connec_entity_name}"]
+ end
end
entities.flatten!
Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Received data: Source=Connec!, Entity=#{self.class.connec_entity_name}, Data=#{entities}")
entities
@@ -222,11 +238,11 @@
# ----------------------------------------------
# External methods
# ----------------------------------------------
def get_external_entities_wrapper(last_synchronization_date=nil)
- return [] if @opts[:skip_external] || !self.class.can_read_external?
+ return [] if @opts[:__skip_external] || !self.class.can_read_external?
get_external_entities(last_synchronization_date)
end
def get_external_entities(last_synchronization_date=nil)
Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Fetching #{Maestrano::Connector::Rails::External.external_name} #{self.class.external_entity_name.pluralize}")
@@ -297,24 +313,19 @@
def update_external_entity(mapped_connec_entity, external_id, external_entity_name)
Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Sending update #{external_entity_name} (id=#{external_id}): #{mapped_connec_entity} to #{Maestrano::Connector::Rails::External.external_name}")
raise "Not implemented"
end
- # This method is called during the webhook workflow only. It should return the array of filtered entities
- # The aim is to have the same filtering as with the Connec! filters on API calls in the webhooks
- def filter_connec_entities(entities)
- entities
- end
-
# ----------------------------------------------
# General methods
# ----------------------------------------------
# * Discards entities that do not need to be pushed because they have not been updated since their last push
# * Discards entities from one of the two source in case of conflict
# * Maps not discarded entities and associates them with their idmap, or create one if there isn't any
# * Returns a hash {connec_entities: [], external_entities: []}
def consolidate_and_map_data(connec_entities, external_entities)
+ Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Consolidating and mapping #{self.class.external_entity_name}/#{self.class.connec_entity_name}")
return consolidate_and_map_singleton(connec_entities, external_entities) if self.class.singleton?
mapped_connec_entities = consolidate_and_map_connec_entities(connec_entities, external_entities, self.class.references, self.class.external_entity_name)
mapped_external_entities = consolidate_and_map_external_entities(external_entities, self.class.connec_entity_name)
@@ -392,21 +403,9 @@
idmap.update(name: self.class.object_name_from_connec_entity_hash(entity), connec_id: entity.delete(:__connec_id))
idmap.update(external_id: self.class.id_from_external_entity_hash(external_entities.first)) unless external_entities.empty?
return {connec_entities: [{entity: map_to_external(entity), idmap: idmap}], external_entities: []}
end
end
-
- # ----------------------------------------------
- # After and before sync
- # ----------------------------------------------
- def before_sync(last_synchronization_date)
- # Does nothing by default
- end
-
- def after_sync(last_synchronization_date)
- # Does nothing by default
- end
-
# ----------------------------------------------
# Internal helper methods
# ----------------------------------------------
private
\ No newline at end of file