Sha256: ff1773bb93cae2d7c583ddcc84b3ad5c6413e85ed1eb06813e212112f200c097

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 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,
             OpenSSL::SSL::SSLError, Errno::ETIMEDOUT
        raise RelatonBib::RequestError, "Could not access #{HitCollection::DOMAIN}"
      end

      # @param ref [String] document reference
      # @param year [String, NilClass]
      # @param opts [Hash] options
      # @return [RelatonUn::UnBibliographicItem]
      def get(ref, _year = nil, _opts = {})
        warn "[relaton-un] (\"#{ref}\") fetching..."
        /^(UN\s)?(?<code>.*)/ =~ ref
        result = isobib_search_filter(code)
        if result
          warn "[relaton-un] (\"#{ref}\") found #{result.fetch.docidentifier.first.id}"
          result.fetch
        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[:ref] == code }.first
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
relaton-un-0.2.0 lib/relaton_un/un_bibliography.rb
relaton-un-0.1.1 lib/relaton_un/un_bibliography.rb
relaton-un-0.1.0 lib/relaton_un/un_bibliography.rb