module SelfCollectStations module Synchronizers class Base attr_reader :data, :country, :attribute_mapper_class, :provider_name attr_accessor :prepared_data def initialize(data, country:, attribute_mapper_class:, provider_name:) @data = data @country = country @attribute_mapper_class = attribute_mapper_class @provider_name = provider_name end def call self.prepared_data = prepare_data_for_sync self_collect_stations = SelfCollectStation.where(external_id: external_ids, provider: provider_name) prepared_data.map do |attributes| self_collect_stations.update_or_create(attributes) end end private def resources JSON.parse(data) end def prepare_data_for_sync resources.each_with_object({}) do |api_record, memo| record = attribute_mapper_class.new(record: api_record, country: country, provider_name: provider_name) memo[record.external_id] = record end end end end