Sha256: 1d94bfd8db803fbf8166945ab54e4559e70d66eca92bd7700fd29b4cc6e28b65

Contents?: true

Size: 1.9 KB

Versions: 5

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

require "net/http"

module RelatonW3c
  # Class methods for search W3C standards.
  class W3cBibliography
    SOURCE = "https://raw.githubusercontent.com/relaton/relaton-data-w3c/main/"
    INDEX = "index1"

    class << self
      # @param text [String]
      # @return [RelatonW3c::W3cBibliographicItem]
      def search(text) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
        pubid = PubId.parse text.sub(/^W3C\s/, "")
        index = Relaton::Index.find_or_create(
          :W3C, url: "#{SOURCE}#{INDEX}.zip", file: "#{INDEX}.yaml", id_keys: PubId::PARTS
        )
        row = index.search { |r| pubid == r[:id] }.sort_by{ |r| (r[:id][:date] || r[:id][:year]).to_i }.last
        return unless row

        url = "#{SOURCE}#{row[:file]}"
        resp = Net::HTTP.get_response(URI.parse(url))
        return unless resp.code == "200"

        hash = YAML.safe_load resp.body
        hash["fetched"] = Date.today.to_s
        item_hash = ::RelatonW3c::HashConverter.hash_to_bib(hash)
        ::RelatonW3c::W3cBibliographicItem.new(**item_hash)
      rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
             EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
             Net::ProtocolError, Errno::ETIMEDOUT
        raise RelatonBib::RequestError,
              "Could not access #{url}"
      end

      # @param ref [String] the W3C standard Code to look up
      # @param year [String, NilClass] not used
      # @param opts [Hash] options
      # @return [RelatonW3c::W3cBibliographicItem]
      def get(ref, _year = nil, _opts = {})
        Util.warn "(#{ref}) Fetching from Relaton repository ..."
        result = search(ref)
        unless result
          Util.warn "(#{ref}) Not found."
          return
        end

        found = result.docidentifier.first.id
        Util.warn "(#{ref}) Found: `#{found}`"
        result
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
relaton-w3c-1.18.1 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.18.0 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.17.3 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.17.2 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.17.1 lib/relaton_w3c/w3c_bibliography.rb