Sha256: 224da978fb17261bf9140f18ff678d987f90bffb60fc6174dd999d3baa36e1e7

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module RelatonIana
  # Methods for search IANA standards.
  module IanaBibliography
    SOURCE = "https://raw.githubusercontent.com/relaton/relaton-data-iana/main/"
    INDEX_FILE = "index-v1.yaml"

    # @param text [String]
    # @return [RelatonIana::IanaBibliographicItem]
    def search(text) # rubocop:disable Metrics/MethodLength
      # file = text.sub(/^IANA\s/, "").gsub(/[\s,:\/]/, "_").downcase
      # url = "#{SOURCE}#{file}.yaml"
      index = Relaton::Index.find_or_create :IANA, url: "#{SOURCE}index-v1.zip", file: INDEX_FILE
      id = text.sub(/^IANA\s/, "")
      row = index.search(id).min_by { |i| i[:id] }
      return unless row

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

      hash = YAML.safe_load resp.body
      hash["fetched"] = Date.today.to_s
      IanaBibliographicItem.from_hash hash
    rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
           EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
           Net::ProtocolError, Errno::ETIMEDOUT => e
      raise RelatonBib::RequestError, e.message
    end

    # @param ref [String] the W3C standard Code to look up
    # @param year [String, NilClass] not used
    # @param opts [Hash] options
    # @return [RelatonIana::IanaBibliographicItem]
    def get(ref, _year = nil, _opts = {})
      warn "[relaton-iana] (\"#{ref}\") fetching..."
      result = search(ref)
      return unless result

      warn "[relaton-iana] (\"#{ref}\") found #{result.docidentifier[0].id}"
      result
    end

    extend IanaBibliography
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
relaton-iana-1.16.0 lib/relaton_iana/iana_bibliography.rb
relaton-iana-1.14.3 lib/relaton_iana/iana_bibliography.rb