Sha256: 2e78977e1b58642cc68e9fc425e69660dba0c528d79bdcb1358f463fbb81760d

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 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/"

    class << self
      # @param text [String]
      # @return [RelatonW3c::W3cBibliographicItem]
      def search(text) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
        ref = DataParser.parse_identifier text.sub(/^W3C\s/, "")
        file = DataIndex.create_from_repo.search ref.gsub(" ", "-").squeeze("-")
        return unless file

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

        hash = YAML.safe_load resp.body
        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

9 entries across 9 versions & 1 rubygems

Version Path
relaton-w3c-1.13.0 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.12.4 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.12.3 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.12.2 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.12.1 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.12.0 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.11.6 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.11.5 lib/relaton_w3c/w3c_bibliography.rb
relaton-w3c-1.11.4 lib/relaton_w3c/w3c_bibliography.rb