Sha256: 3c3276a3657b869bd4fde31a520c7e0b37402445857feabdc6b2cf606ecd3ea1

Contents?: true

Size: 1.15 KB

Versions: 8

Compression:

Stored size: 1.15 KB

Contents

require "faraday"
require "csv"
require "study_engine/config"

module StudyEngine
  class StudyID
    class Sync
      def self.call
        new.call
      end

      def call
        raise "Cannot sync with REDCap without setting StudyEngine.redcap_token" if StudyEngine.redcap_token.blank?
        CSV.parse(response, headers: true) do |row|
          process row unless row.empty?
        end
        StudyID::Bank.where(["updated_at < ?", 30.minutes.ago]).delete_all
      end

      private

      def process row
        study_id = StudyID::Bank.find_or_create_from_full_study_id(row["study_id"])
        study_id.touch unless study_id.new_record?
      end

      def response
        conn = Faraday.new(url: "https://metrcdata.org")
        resp = conn.post "/redcap/api/",
          token: StudyEngine.redcap_token,
          content: "record",
          format: "csv"
        resp.body
      end

      prepend Module.new {
        def response
          retries = 0
          super
        rescue Faraday::TimeoutError
          retries += 1
          if retries < 5
            retry
          else
            raise
          end
        end
      }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
study_engine-0.3.1 app/models/study_engine/study_id/sync.rb
study_engine-0.3.0 app/models/study_engine/study_id/sync.rb
study_engine-0.2.2 app/models/study_engine/study_id/sync.rb
study_engine-0.2.1 app/models/study_engine/study_id/sync.rb
study_engine-0.2.0 app/models/study_engine/study_id/sync.rb
study_engine-0.1.1 app/models/study_engine/study_id/sync.rb
study_engine-0.1.0 app/models/study_engine/study_id/sync.rb
study_engine-0.0.2 app/models/study_engine/study_id/sync.rb