Sha256: bdd12f58bf8677f766ad58f2ca66001e8edf14891c800e16740ddc935ffeb36f

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module Relaton
  module Plateau
    module Bibliography
      extend self

      INDEXFILE = "index-v1"
      GHURL = "https://raw.githubusercontent.com/relaton/relaton-data-plateau/main/"

      def index
        Relaton::Index.find_or_create :plateau, url: "#{GHURL}#{INDEXFILE}.zip", file: "#{INDEXFILE}.yaml"
      end

      def get(code, year = nil, opts = {})
        Util.info "Fetching ...", key: code
        bib = search(code)
        if bib
          Util.info "Found `#{bib.docidentifier.first.id}`", key: code
          bib
        else
          Util.warn "Not found.", key: code
        end
      rescue StandardError => e
        raise RelatonBib::RequestError, e.message
      end

      def search(code)
        rows = index.search { |r| r[:id] ==  code }
        return unless rows.any?

        row = rows.sort_by { |r| r[:id] }.last
        fetch_doc code, **row
      end

      def fetch_doc(code, id:, file:)
        resp = Net::HTTP.get_response URI("#{GHURL}#{file}")
        return unless resp.is_a? Net::HTTPSuccess

        hash = YAML.load(resp.body)
        args = HashConverter.hash_to_bib hash
        BibItem.new(**args)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
relaton-plateau-1.19.3 lib/relaton/plateau/bibliography.rb
relaton-plateau-1.19.2 lib/relaton/plateau/bibliography.rb
relaton-plateau-1.19.1 lib/relaton/plateau/bibliography.rb