Sha256: b74855b0a05c4d139a8a3482b3181b748a9969812d3d1382092423e1d0d99294

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module RelatonUn
  # Class methods for search ISO standards.
  class UnBibliography
    class << self
      # @param text [String]
      # @return [RelatonUn::HitCollection]
      def search(text)
        HitCollection.new text
      rescue SocketError, Errno::EINVAL, Errno::ECONNRESET, EOFError,
             Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
             Net::ProtocolError, Net::ReadTimeout, Net::OpenTimeout,
             OpenSSL::SSL::SSLError, Errno::ETIMEDOUT => e
        raise RelatonBib::RequestError,
              "Could not access #{HitCollection::DOMAIN}: #{e.message}"
      end

      # @param ref [String] document reference
      # @param year [String, NilClass]
      # @param opts [Hash] options
      # @return [RelatonUn::UnBibliographicItem]
      def get(ref, _year = nil, _opts = {})
        Util.info "Fetching from documents.un.org ...", key: ref
        /^(?:UN\s)?(?<code>.*)/ =~ ref
        result = isobib_search_filter(code)
        if result
          Util.info "Found: `#{result.fetch.docidentifier[0].id}`", key: ref
          result.fetch
        else
          Util.info "Not found.", key: ref
          nil
        end
      end

      private

      # Search for hits.
      #
      # @param code [String] reference without correction
      # @return [RelatonUn::HitCollection]
      def isobib_search_filter(code)
        result = search(code)
        result.select { |i| i.hit[:symbol].include? code }.first
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
relaton-un-1.19.0 lib/relaton_un/un_bibliography.rb