Sha256: 3b902c3a0887ea4b36d27986fa129dfa2b05872949a4da40a2499a8274926c7a

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 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"
        row = index.search { |r| pubid == r[:id] }.first
        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 = {})
        warn "[relaton-w3c] (\"#{ref}\") fetching..."
        result = search(ref)
        unless result
          warn "[relaton-w3c] (\"#{ref}\") not found."
          return
        end

        found = result.docnumber
        warn "[relaton-w3c] (\"#{ref}\") found #{found}"
        result
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
relaton-w3c-1.14.2 lib/relaton_w3c/w3c_bibliography.rb