app/models/maestrano/connector/rails/concerns/entity.rb in maestrano-connector-rails-1.0.3 vs app/models/maestrano/connector/rails/concerns/entity.rb in maestrano-connector-rails-1.0.4

- old
+ new

@@ -164,29 +164,28 @@ query_params[:$orderby] = @opts[:$orderby] if @opts[:$orderby] # Fetch first page page_number = 0 if last_synchronization.blank? || @opts[:full_sync] - Maestrano::Connector::Rails::ConnectorLogger.log('debug', @organization, "entity=#{self.class.connec_entity_name}, fetching all data") query_params[:$filter] = @opts[:$filter] if @opts[:$filter] else - Maestrano::Connector::Rails::ConnectorLogger.log('debug', @organization, "entity=#{self.class.connec_entity_name}, fetching data since #{last_synchronization.updated_at.iso8601}") query_params[:$filter] = "updated_at gt '#{last_synchronization.updated_at.iso8601}'" + (@opts[:$filter] ? " and #{@opts[:$filter]}" : '') end - uri = "/#{self.class.normalized_connec_entity_name}?#{query_params.to_query}" + Maestrano::Connector::Rails::ConnectorLogger.log('debug', @organization, "entity=#{self.class.connec_entity_name}, fetching data with #{query_params.to_query}") + 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) - response_hash = fetch_connec(uri, page_number) + response_hash = fetch_connec(next_page, page_number) entities << response_hash["#{self.class.normalized_connec_entity_name}"] end entities.flatten! Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Received data: Source=Connec!, Entity=#{self.class.connec_entity_name}, Data=#{entities}") @@ -323,11 +322,11 @@ entity = Maestrano::Connector::Rails::ConnecHelper.unfold_references(entity, references, @organization) next nil unless entity connec_id = entity.delete(:__connec_id) if entity['id'].blank? - idmap = self.class.create_idmap(organization_id: @organization.id, name: self.class.object_name_from_connec_entity_hash(entity), external_entity: external_entity_name.downcase, connec_id: connec_id) + idmap = self.class.find_or_create_idmap(organization_id: @organization.id, name: self.class.object_name_from_connec_entity_hash(entity), external_entity: external_entity_name.downcase, connec_id: connec_id) next map_connec_entity_with_idmap(entity, external_entity_name, idmap) end idmap = self.class.find_or_create_idmap(external_id: entity['id'], organization_id: @organization.id, external_entity: external_entity_name.downcase, connec_id: connec_id) idmap.update(name: self.class.object_name_from_connec_entity_hash(entity)) @@ -490,10 +489,10 @@ def fetch_connec(uri, page_number) response = @connec_client.get(uri) raise "No data received from Connec! when trying to fetch page #{page_number} of #{self.class.normalized_connec_entity_name}" unless response && !response.body.blank? response_hash = JSON.parse(response.body) - Maestrano::Connector::Rails::ConnectorLogger.log('debug', @organization, "received first page entity=#{self.class.connec_entity_name}, response=#{response_hash}") + Maestrano::Connector::Rails::ConnectorLogger.log('debug', @organization, "Received page #{page_number} for entity=#{self.class.connec_entity_name}, response=#{response_hash}") raise "Received unrecognized Connec! data when trying to fetch page #{page_number} of #{self.class.normalized_connec_entity_name}: #{response_hash}" unless response_hash["#{self.class.normalized_connec_entity_name}"] response_hash end \ No newline at end of file