Sha256: f6c1ef18674654283b230704b163eceead37571edfa7b5949b94c78dce622aa1

Contents?: true

Size: 1.49 KB

Versions: 15

Compression:

Stored size: 1.49 KB

Contents

module RelatonBipm
  class Index
    #
    # Initialize index
    #
    def initialize
      read_index_file || get_index_from_gh
    end

    #
    # Search index entry
    #
    # @param [String] ref reference
    #
    # @return [String] path to document file
    #
    def search(ref)
      @index.detect { |key, _| key.include? ref }&.last
    end

    private

    #
    # Create dir if need and return path to index file
    #
    # @return [String] path to index file
    #
    def path
      @path ||= begin
        dir = File.join Dir.home, ".relaton", "bipm"
        FileUtils.mkdir_p dir
        File.join dir, "index.yaml"
      end
    end

    #
    # Read index from file if it exists and not outdated
    #
    # @return [Hash, nil] index content
    #
    def read_index_file
      return if !File.exist?(path) || File.ctime(path).to_date < Date.today

      @index = RelatonBib.parse_yaml File.read(path, encoding: "UTF-8")
    end

    #
    # Save index to file
    #
    # @return [<Type>] <description>
    #
    def save_index_file
      File.write path, @index.to_yaml, encoding: "UTF-8"
    end

    #
    # Get index from a GitHub repository
    #
    # @return [Hash] index content
    #
    def get_index_from_gh # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
      resp = Zip::InputStream.new URI("#{BipmBibliography::GH_ENDPOINT}index.zip").open
      zip = resp.get_next_entry
      @index = RelatonBib.parse_yaml zip.get_input_stream.read
      save_index_file
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
relaton-bipm-1.14.2 lib/relaton_bipm/index.rb
relaton-bipm-1.14.1 lib/relaton_bipm/index.rb
relaton-bipm-1.14.0 lib/relaton_bipm/index.rb
relaton-bipm-1.13.12 lib/relaton_bipm/index.rb
relaton-bipm-1.13.11 lib/relaton_bipm/index.rb
relaton-bipm-1.13.10 lib/relaton_bipm/index.rb
relaton-bipm-1.13.9 lib/relaton_bipm/index.rb
relaton-bipm-1.13.8 lib/relaton_bipm/index.rb
relaton-bipm-1.13.7 lib/relaton_bipm/index.rb
relaton-bipm-1.13.6 lib/relaton_bipm/index.rb
relaton-bipm-1.13.5 lib/relaton_bipm/index.rb
relaton-bipm-1.13.4 lib/relaton_bipm/index.rb
relaton-bipm-1.13.3 lib/relaton_bipm/index.rb
relaton-bipm-1.13.2 lib/relaton_bipm/index.rb
relaton-bipm-1.13.1 lib/relaton_bipm/index.rb