Sha256: 4e0258473433f688086e1d6f3de7c5e2864ad46064859b396c708599fbd0e8ef

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

# (c) Copyright 2020 Ribose Inc.
#

require "singleton"

module IEV
  # Relaton cach singleton.
  class RelatonDb
    include Singleton
    include CLI::UI

    def initialize
      info "Initializing Relaton..."
      @db = Relaton::Db.new "db", nil
    end

    # @param code [String] reference
    # @return [RelatonIso::IsoBibliongraphicItem]
    def fetch(code)
      retrying_on_failures do
        capture_output_streams do
          @db.fetch code
        end
      end
    end

    private

    def retrying_on_failures(attempts: 4)
      curr_attempt = 1

      begin
        yield

      rescue
        if curr_attempt <= attempts
          sleep(2 ** curr_attempt * 0.1)
          curr_attempt += 1
          retry
        else
          raise
        end
      end
    end

    def capture_output_streams
      original_stdout = $stdout
      original_stderr = $stderr
      $stderr = $stdout = fake_out = StringIO.new

      begin
        yield

      ensure
        $stdout = original_stdout
        $stderr = original_stderr
        debug(:relaton, fake_out.string) if fake_out.pos > 0
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iev-0.3.4 lib/iev/relaton_db.rb
iev-0.3.3 lib/iev/relaton_db.rb